Skip to content

Commit 5eb98e4

Browse files
author
Marco Gorelli
committed
rewrite tests, typing
1 parent 44a68e5 commit 5eb98e4

File tree

3 files changed

+15
-11
lines changed

3 files changed

+15
-11
lines changed

pandas/core/indexes/base.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from datetime import datetime
22
import operator
33
from textwrap import dedent
4-
from typing import Any, Dict, FrozenSet, Hashable, Optional, Union
4+
from typing import Any, FrozenSet, Hashable, Optional, Union
55
import warnings
66

77
import numpy as np
@@ -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) -> Dict[Hashable, np.ndarray]:
4769+
def groupby(self, values) -> PrettyDict:
47704770
"""
47714771
Group the index labels by a given array of values.
47724772

pandas/io/formats/printing.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@
66
from typing import (
77
Any,
88
Callable,
9+
Dict,
910
Iterable,
1011
List,
1112
Mapping,
1213
Optional,
1314
Sequence,
1415
Tuple,
16+
TypeVar,
1517
Union,
1618
)
1719

@@ -20,6 +22,8 @@
2022
from pandas.core.dtypes.inference import is_sequence
2123

2224
EscapeChars = Union[Mapping[str, str], Iterable[str]]
25+
_KT = TypeVar("_KT")
26+
_VT = TypeVar("_VT")
2327

2428

2529
def adjoin(space: int, *lists: List[str], **kwargs) -> str:
@@ -530,8 +534,8 @@ def format_object_attrs(
530534
return attrs
531535

532536

533-
class PrettyDict(dict):
537+
class PrettyDict(Dict[_KT, _VT]):
534538
"""Dict extension to support abbreviated __repr__"""
535539

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

pandas/tests/groupby/test_groupby.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -2041,12 +2041,12 @@ def test_groupby_list_level():
20412041

20422042
def test_groups_repr_truncates():
20432043
# GH 1135
2044-
df = pd.DataFrame({"a": [1, 1, 1, 2, 2, 3], "b": [1, 2, 3, 4, 5, 6]})
2044+
df = pd.DataFrame(np.random.randn(61, 1))
2045+
df["a"] = df.index
20452046

2046-
with pd.option_context("display.max_rows", 2):
2047-
x = df.groupby("a").groups
2048-
assert x.__repr__().endswith("...}")
2047+
result = df.groupby("a").groups.__repr__()
2048+
expected = str({i: [i] for i in range(60)})[:-1] + ", ...}"
2049+
assert result == expected
20492050

2050-
with pd.option_context("display.max_rows", 5):
2051-
x = df.groupby(np.array(df.a)).groups
2052-
assert not x.__repr__().endswith("...}")
2051+
result = df.groupby(np.array(df.a)).groups.__repr__()
2052+
assert result == expected

0 commit comments

Comments
 (0)