Skip to content

BUG: Updated _pprint_seq to use enumerate instead of next #57295

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

Merged
merged 3 commits into from
Feb 9, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v3.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ Performance improvements
Bug fixes
~~~~~~~~~
- Fixed bug in :meth:`DataFrame.join` inconsistently setting result index name (:issue:`55815`)
- Fixed bug in :meth:`DataFrame.to_string` that raised ``StopIteration`` with nested DataFrames. (:issue:`16098`)
- Fixed bug in :meth:`Series.diff` allowing non-integer values for the ``periods`` argument. (:issue:`56607`)

Categorical
Expand Down
13 changes: 8 additions & 5 deletions pandas/io/formats/printing.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,13 +117,16 @@ def _pprint_seq(

s = iter(seq)
# handle sets, no slicing
r = [
pprint_thing(next(s), _nest_lvl + 1, max_seq_items=max_seq_items, **kwds)
for i in range(min(nitems, len(seq)))
]
r = []
max_item_limit = False
for i, item in enumerate(s):
if i >= nitems:
max_item_limit = True
break
r.append(pprint_thing(item, _nest_lvl + 1, max_seq_items=max_seq_items, **kwds))
body = ", ".join(r)

if nitems < len(seq):
if max_item_limit:
body += ", ..."
elif isinstance(seq, tuple) and len(seq) == 1:
body += ","
Expand Down
5 changes: 5 additions & 0 deletions pandas/tests/io/formats/test_to_string.py
Original file line number Diff line number Diff line change
Expand Up @@ -945,6 +945,11 @@ def test_to_string_repr_unicode(self):
finally:
sys.stdin = _stdin

def test_nested_dataframe(self):
df1 = DataFrame({"level1": [["row1"], ["row2"]]})
df2 = DataFrame({"level3": [{"level2": df1}]})
df2.to_string()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you assert the result of this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!



class TestSeriesToString:
def test_to_string_without_index(self):
Expand Down