Skip to content

Commit b966657

Browse files
authored
REGR: Notebook (html) repr of DataFrame no longer follows min_rows/max_rows settings (#37363)
* TST: add failing test * FIX: fix display logic * TST: add test cases * TYP: type max_rows on top of the method
1 parent 191f859 commit b966657

File tree

2 files changed

+49
-9
lines changed

2 files changed

+49
-9
lines changed

pandas/io/formats/format.py

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -639,20 +639,31 @@ def _calc_max_cols_fitted(self) -> Optional[int]:
639639

640640
def _calc_max_rows_fitted(self) -> Optional[int]:
641641
"""Number of rows with data fitting the screen."""
642-
if not self._is_in_terminal():
643-
return self.max_rows
642+
max_rows: Optional[int]
644643

645-
_, height = get_terminal_size()
646-
if self.max_rows == 0:
647-
# rows available to fill with actual data
648-
return height - self._get_number_of_auxillary_rows()
644+
if self._is_in_terminal():
645+
_, height = get_terminal_size()
646+
if self.max_rows == 0:
647+
# rows available to fill with actual data
648+
return height - self._get_number_of_auxillary_rows()
649649

650-
max_rows: Optional[int]
651-
if self._is_screen_short(height):
652-
max_rows = height
650+
if self._is_screen_short(height):
651+
max_rows = height
652+
else:
653+
max_rows = self.max_rows
653654
else:
654655
max_rows = self.max_rows
655656

657+
return self._adjust_max_rows(max_rows)
658+
659+
def _adjust_max_rows(self, max_rows: Optional[int]) -> Optional[int]:
660+
"""Adjust max_rows using display logic.
661+
662+
See description here:
663+
https://pandas.pydata.org/docs/dev/user_guide/options.html#frequently-used-options
664+
665+
GH #37359
666+
"""
656667
if max_rows:
657668
if (len(self.frame) > max_rows) and self.min_rows:
658669
# if truncated, set max_rows showed to min_rows

pandas/tests/io/formats/test_format.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2029,6 +2029,35 @@ def test_period(self):
20292029
)
20302030
assert str(df) == exp
20312031

2032+
@pytest.mark.parametrize(
2033+
"length, max_rows, min_rows, expected",
2034+
[
2035+
(10, 10, 10, 10),
2036+
(10, 10, None, 10),
2037+
(10, 8, None, 8),
2038+
(20, 30, 10, 30), # max_rows > len(frame), hence max_rows
2039+
(50, 30, 10, 10), # max_rows < len(frame), hence min_rows
2040+
(100, 60, 10, 10), # same
2041+
(60, 60, 10, 60), # edge case
2042+
(61, 60, 10, 10), # edge case
2043+
],
2044+
)
2045+
def test_max_rows_fitted(self, length, min_rows, max_rows, expected):
2046+
"""Check that display logic is correct.
2047+
2048+
GH #37359
2049+
2050+
See description here:
2051+
https://pandas.pydata.org/docs/dev/user_guide/options.html#frequently-used-options
2052+
"""
2053+
formatter = fmt.DataFrameFormatter(
2054+
DataFrame(np.random.rand(length, 3)),
2055+
max_rows=max_rows,
2056+
min_rows=min_rows,
2057+
)
2058+
result = formatter.max_rows_fitted
2059+
assert result == expected
2060+
20322061

20332062
def gen_series_formatting():
20342063
s1 = Series(["a"] * 100)

0 commit comments

Comments
 (0)