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

fixed bug where pd.NA was being cast to NaN during formatting #55754

Merged
merged 13 commits into from
Nov 7, 2023
8 changes: 5 additions & 3 deletions pandas/io/formats/format.py
Original file line number Diff line number Diff line change
Expand Up @@ -696,9 +696,11 @@ def _truncate_vertically(self) -> None:
assert self.max_rows_fitted is not None
row_num = self.max_rows_fitted // 2
if row_num >= 1:
head = self.tr_frame.iloc[:row_num, :]
tail = self.tr_frame.iloc[-row_num:, :]
self.tr_frame = concat((head, tail))
_len = len(self.tr_frame)
rows = np.arange(_len)
self.tr_frame = self.tr_frame.iloc[
(rows < row_num) | (rows >= _len - row_num)
dominiquegarmier marked this conversation as resolved.
Show resolved Hide resolved
]
else:
row_num = cast(int, self.max_rows)
self.tr_frame = self.tr_frame.iloc[:row_num, :]
Expand Down
9 changes: 9 additions & 0 deletions pandas/tests/io/formats/test_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,15 @@ def test_repr_truncation(self):
with option_context("display.max_colwidth", max_len + 2):
assert "..." not in repr(df)

def test_repr_truncation_preserves_na(self):
# https://github.com/pandas-dev/pandas/issues/55630
with option_context("display.max_rows", 10):
Copy link
Member

Choose a reason for hiding this comment

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

If you set this to 2 instead of 10, I think we could then test for the entire string.

df = DataFrame({"A": [pd.NA for _ in range(100)]})

r = repr(df)
for row in r.split("\n")[1:-2]:
assert row.endswith(("<NA>", "..."))

def test_max_colwidth_negative_int_raises(self):
# Deprecation enforced from:
# https://github.com/pandas-dev/pandas/issues/31532
Expand Down
Loading