Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Default string output of TableSet with multiple layers of nesting throws AttributeError: 'TableSet' object has no attribute 'rows' #765

Closed
vergenzt opened this issue Mar 31, 2022 · 4 comments · Fixed by #776
Labels

Comments

@vergenzt
Copy link

Example below. I encountered this while exploring a dataset in an ipdb debugging session (debugging a dbt macro). For some reason ipdb wasn't showing me the full stack traces for errors -- just the exception type and description.

So the only output I got looked like this:

ipdb> mytable.group_by('col1').group_by('col2')
*** AttributeError: 'TableSet' object has no attribute 'rows'
ipdb> _

making this issue more confusing than it may have otherwise been.

Note, now that I understand this is simply an issue with the string output on the REPL and not an issue with the underlying data (and I can still save it to disk), this issue does not block me.

Example to reproduce

Input:

>>> import agate
>>> mytable = agate.Table([('foo', 'FOO', 1), ('foo', 'FOO', 2), ('bar', 'BOZ', 1), ('bar', 'BAZ', 2), ('bar', 'BIZ')])
<stdin>:1: RuntimeWarning: Column names not specified. "('a', 'b', 'c')" will be used as names.

Actual string output:

>>> mytable.group_by('a').group_by('b')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File ".../.venv/lib/python3.8/site-packages/agate/mapped_sequence.py", line 87, in __repr__
    return self.__str__()
  File ".../.venv/lib/python3.8/site-packages/agate/tableset/__init__.py", line 91, in __str__
    self.print_structure(output=structure)
  File ".../.venv/lib/python3.8/site-packages/agate/tableset/print_structure.py", line 24, in print_structure
    type_column = [str(len(table.rows)) for key, table in self.items()[0:max_length]]
  File ".../.venv/lib/python3.8/site-packages/agate/tableset/print_structure.py", line 24, in <listcomp>
    type_column = [str(len(table.rows)) for key, table in self.items()[0:max_length]]
AttributeError: 'TableSet' object has no attribute 'rows'

JSON output for clarity:

>>> mytable.group_by('a').group_by('b').to_json('/dev/stdout', nested=True)
{"foo": {"FOO": [{"a": "foo", "b": "FOO", "c": 1.0}, {"a": "foo", "b": "FOO", "c": 2.0}]}, "bar": {"BOZ": [{"a": "bar", "b": "BOZ", "c": 1.0}], "BAZ": [{"a": "bar", "b": "BAZ", "c": 2.0}], "BIZ": [{"a": "bar", "b": "BIZ", "c": null}]}}

Expected string output:

???

(I have no ideas here other than "probably not an exception". 🙂 Figured I'd still go ahead and share.)

@jpmckinney
Copy link
Member

Thank you for reporting!

The issue was that group_by – uniquely – returns a TableSet instead of a Table. Calling group_by a second time yielded a TableSet containing TableSets (instead of directly containing Tables), causing the error.

@jpmckinney
Copy link
Member

Ah, my solution worked for group_by, but it messed up pivot (which calls group_by and aggregate) and aggregate (which contains logic for nested table sets).

@jpmckinney jpmckinney reopened this Oct 4, 2023
@jpmckinney
Copy link
Member

Does this make any sense for that input?

| table   | rows |
| ------- | ---- |
| foo.FOO | 2    |
| bar.BOZ | 1    |
| bar.BAZ | 1    |
| bar.BIZ | 1    |

@jpmckinney
Copy link
Member

jpmckinney commented Oct 4, 2023

The patch: #776

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants