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
6 changes: 3 additions & 3 deletions pandas/io/formats/format.py
Original file line number Diff line number Diff line change
Expand Up @@ -696,9 +696,9 @@ 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)
_slice = np.hstack([np.arange(row_num), np.arange(_len - row_num, _len)])
self.tr_frame = self.tr_frame.iloc[_slice]
else:
row_num = cast(int, self.max_rows)
self.tr_frame = self.tr_frame.iloc[:row_num, :]
Expand Down
6 changes: 6 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,12 @@ 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
df = DataFrame({"a": [pd.NA for _ in range(10)]})
with option_context("display.max_rows", 2, "display.show_dimensions", False):
assert repr(df) == " a\n0 <NA>\n.. ...\n9 <NA>"

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