Skip to content

Commit b6b9751

Browse files
author
Marco Gorelli
committed
parametrize test, allow max_seq_items to determine truncation
1 parent 5eb98e4 commit b6b9751

File tree

3 files changed

+18
-11
lines changed

3 files changed

+18
-11
lines changed

pandas/core/indexes/base.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -4766,7 +4766,7 @@ def _maybe_promote(self, other):
47664766
return self.astype("object"), other.astype("object")
47674767
return self, other
47684768

4769-
def groupby(self, values) -> PrettyDict:
4769+
def groupby(self, values) -> PrettyDict[Hashable, np.ndarray]:
47704770
"""
47714771
Group the index labels by a given array of values.
47724772

pandas/io/formats/printing.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ def _pprint_seq(
102102
) -> str:
103103
"""
104104
internal. pprinter for iterables. you should probably use pprint_thing()
105-
rather than calling this directly.
105+
rather then calling this directly.
106106
107107
bounds length of printed sequence, depending on options
108108
"""
@@ -137,7 +137,7 @@ def _pprint_dict(
137137
) -> str:
138138
"""
139139
internal. pprinter for iterables. you should probably use pprint_thing()
140-
rather than calling this directly.
140+
rather then calling this directly.
141141
"""
142142
fmt = "{{{things}}}"
143143
pairs = []
@@ -538,4 +538,4 @@ class PrettyDict(Dict[_KT, _VT]):
538538
"""Dict extension to support abbreviated __repr__"""
539539

540540
def __repr__(self) -> str:
541-
return pprint_thing(self, max_seq_items=get_option("display.max_rows"))
541+
return pprint_thing(self)

pandas/tests/groupby/test_groupby.py

+14-7
Original file line numberDiff line numberDiff line change
@@ -2039,14 +2039,21 @@ def test_groupby_list_level():
20392039
tm.assert_frame_equal(result, expected)
20402040

20412041

2042-
def test_groups_repr_truncates():
2042+
@pytest.mark.parametrize(
2043+
"max_seq_items, expected",
2044+
[
2045+
(5, "{0: [0], 1: [1], 2: [2], 3: [3], 4: [4]}"),
2046+
(4, "{0: [0], 1: [1], 2: [2], 3: [3], ...}"),
2047+
],
2048+
)
2049+
def test_groups_repr_truncates(max_seq_items, expected):
20432050
# GH 1135
2044-
df = pd.DataFrame(np.random.randn(61, 1))
2051+
df = pd.DataFrame(np.random.randn(5, 1))
20452052
df["a"] = df.index
20462053

2047-
result = df.groupby("a").groups.__repr__()
2048-
expected = str({i: [i] for i in range(60)})[:-1] + ", ...}"
2049-
assert result == expected
2054+
with pd.option_context("display.max_seq_items", max_seq_items):
2055+
result = df.groupby("a").groups.__repr__()
2056+
assert result == expected
20502057

2051-
result = df.groupby(np.array(df.a)).groups.__repr__()
2052-
assert result == expected
2058+
result = df.groupby(np.array(df.a)).groups.__repr__()
2059+
assert result == expected

0 commit comments

Comments
 (0)