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 all 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
17 changes: 10 additions & 7 deletions pandas/io/formats/printing.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,19 +111,22 @@ def _pprint_seq(
fmt = "[{body}]" if hasattr(seq, "__setitem__") else "({body})"

if max_seq_items is False:
nitems = len(seq)
max_items = None
else:
nitems = max_seq_items or get_option("max_seq_items") or len(seq)
max_items = max_seq_items or get_option("max_seq_items") or len(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_items_reached = False
for i, item in enumerate(s):
if (max_items is not None) and (i >= max_items):
max_items_reached = 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_items_reached:
body += ", ..."
elif isinstance(seq, tuple) and len(seq) == 1:
body += ","
Expand Down
7 changes: 7 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,13 @@ 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}]})
result = df2.to_string()
expected = " level3\n0 {'level2': ['level1']}"
assert result == expected


class TestSeriesToString:
def test_to_string_without_index(self):
Expand Down