Skip to content

Commit bf37560

Browse files
BUG: pd.NA being cast to NaN during output formatting (#55754)
1 parent 31fde44 commit bf37560

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

pandas/io/formats/format.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -696,9 +696,9 @@ def _truncate_vertically(self) -> None:
696696
assert self.max_rows_fitted is not None
697697
row_num = self.max_rows_fitted // 2
698698
if row_num >= 1:
699-
head = self.tr_frame.iloc[:row_num, :]
700-
tail = self.tr_frame.iloc[-row_num:, :]
701-
self.tr_frame = concat((head, tail))
699+
_len = len(self.tr_frame)
700+
_slice = np.hstack([np.arange(row_num), np.arange(_len - row_num, _len)])
701+
self.tr_frame = self.tr_frame.iloc[_slice]
702702
else:
703703
row_num = cast(int, self.max_rows)
704704
self.tr_frame = self.tr_frame.iloc[:row_num, :]

pandas/tests/io/formats/test_format.py

+6
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,12 @@ def test_repr_truncation(self):
171171
with option_context("display.max_colwidth", max_len + 2):
172172
assert "..." not in repr(df)
173173

174+
def test_repr_truncation_preserves_na(self):
175+
# https://github.com/pandas-dev/pandas/issues/55630
176+
df = DataFrame({"a": [pd.NA for _ in range(10)]})
177+
with option_context("display.max_rows", 2, "display.show_dimensions", False):
178+
assert repr(df) == " a\n0 <NA>\n.. ...\n9 <NA>"
179+
174180
def test_max_colwidth_negative_int_raises(self):
175181
# Deprecation enforced from:
176182
# https://github.com/pandas-dev/pandas/issues/31532

0 commit comments

Comments
 (0)