Skip to content

Commit 03829ce

Browse files
committed
Merge remote-tracking branch 'upstream/master' into docfix-multiindex-set_levels
2 parents 6ab5843 + ee42275 commit 03829ce

File tree

18 files changed

+180
-32
lines changed

18 files changed

+180
-32
lines changed

pandas/_typing.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,21 +23,29 @@
2323
from pandas.core.indexes.base import Index # noqa: F401
2424
from pandas.core.series import Series # noqa: F401
2525
from pandas.core.generic import NDFrame # noqa: F401
26+
from pandas import Interval # noqa: F401
2627

28+
# array-like
2729

2830
AnyArrayLike = TypeVar("AnyArrayLike", "ExtensionArray", "Index", "Series", np.ndarray)
2931
ArrayLike = TypeVar("ArrayLike", "ExtensionArray", np.ndarray)
32+
33+
# scalars
34+
35+
PythonScalar = Union[str, int, float, bool]
3036
DatetimeLikeScalar = TypeVar("DatetimeLikeScalar", "Period", "Timestamp", "Timedelta")
37+
PandasScalar = Union["Period", "Timestamp", "Timedelta", "Interval"]
38+
Scalar = Union[PythonScalar, PandasScalar]
39+
40+
# other
41+
3142
Dtype = Union[str, np.dtype, "ExtensionDtype"]
3243
FilePathOrBuffer = Union[str, Path, IO[AnyStr]]
33-
3444
FrameOrSeries = TypeVar("FrameOrSeries", bound="NDFrame")
35-
Scalar = Union[str, int, float, bool]
3645
Axis = Union[str, int]
3746
Ordered = Optional[bool]
38-
JSONSerializable = Union[Scalar, List, Dict]
39-
47+
JSONSerializable = Union[PythonScalar, List, Dict]
4048
Axes = Collection
4149

4250
# to maintain type information across generic functions and parametrization
43-
_T = TypeVar("_T")
51+
T = TypeVar("T")

pandas/core/arrays/categorical.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import operator
22
from shutil import get_terminal_size
3-
from typing import Type, Union, cast
3+
from typing import Dict, Hashable, List, Type, Union, cast
44
from warnings import warn
55

66
import numpy as np
77

88
from pandas._config import get_option
99

1010
from pandas._libs import algos as libalgos, hashtable as htable
11-
from pandas._typing import ArrayLike, Dtype, Ordered
11+
from pandas._typing import ArrayLike, Dtype, Ordered, Scalar
1212
from pandas.compat.numpy import function as nv
1313
from pandas.util._decorators import (
1414
Appender,
@@ -511,7 +511,7 @@ def itemsize(self) -> int:
511511
"""
512512
return self.categories.itemsize
513513

514-
def tolist(self) -> list:
514+
def tolist(self) -> List[Scalar]:
515515
"""
516516
Return a list of the values.
517517
@@ -2067,7 +2067,7 @@ def __setitem__(self, key, value):
20672067
lindexer = self._maybe_coerce_indexer(lindexer)
20682068
self._codes[key] = lindexer
20692069

2070-
def _reverse_indexer(self):
2070+
def _reverse_indexer(self) -> Dict[Hashable, np.ndarray]:
20712071
"""
20722072
Compute the inverse of a categorical, returning
20732073
a dict of categories -> indexers.
@@ -2097,8 +2097,8 @@ def _reverse_indexer(self):
20972097
self.codes.astype("int64"), categories.size
20982098
)
20992099
counts = counts.cumsum()
2100-
result = (r[start:end] for start, end in zip(counts, counts[1:]))
2101-
result = dict(zip(categories, result))
2100+
_result = (r[start:end] for start, end in zip(counts, counts[1:]))
2101+
result = dict(zip(categories, _result))
21022102
return result
21032103

21042104
# reduction ops #

pandas/core/common.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,12 @@
99
from datetime import datetime, timedelta
1010
from functools import partial
1111
import inspect
12-
from typing import Any, Iterable, Union
12+
from typing import Any, Collection, Iterable, Union
1313

1414
import numpy as np
1515

1616
from pandas._libs import lib, tslibs
17+
from pandas._typing import T
1718

1819
from pandas.core.dtypes.cast import construct_1d_object_array_from_listlike
1920
from pandas.core.dtypes.common import (
@@ -270,7 +271,7 @@ def maybe_make_list(obj):
270271
return obj
271272

272273

273-
def maybe_iterable_to_list(obj: Union[Iterable, Any]) -> Union[list, Any]:
274+
def maybe_iterable_to_list(obj: Union[Iterable[T], T]) -> Union[Collection[T], T]:
274275
"""
275276
If obj is Iterable but not list-like, consume into list.
276277
"""

pandas/core/dtypes/base.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -276,10 +276,12 @@ def is_dtype(cls, dtype) -> bool:
276276
return False
277277
elif isinstance(dtype, cls):
278278
return True
279-
try:
280-
return cls.construct_from_string(dtype) is not None
281-
except TypeError:
282-
return False
279+
if isinstance(dtype, str):
280+
try:
281+
return cls.construct_from_string(dtype) is not None
282+
except TypeError:
283+
return False
284+
return False
283285

284286
@property
285287
def _is_numeric(self) -> bool:

pandas/core/dtypes/dtypes.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -882,7 +882,11 @@ def construct_from_string(cls, string):
882882
return cls(freq=string)
883883
except ValueError:
884884
pass
885-
raise TypeError(f"Cannot construct a 'PeriodDtype' from '{string}'")
885+
if isinstance(string, str):
886+
msg = f"Cannot construct a 'PeriodDtype' from '{string}'"
887+
else:
888+
msg = f"'construct_from_string' expects a string, got {type(string)}"
889+
raise TypeError(msg)
886890

887891
def __str__(self) -> str_type:
888892
return self.name

pandas/core/groupby/grouper.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
split-apply-combine paradigm.
44
"""
55

6-
from typing import Hashable, List, Optional, Tuple
6+
from typing import Dict, Hashable, List, Optional, Tuple
77

88
import numpy as np
99

@@ -419,7 +419,7 @@ def _make_codes(self) -> None:
419419
self._group_index = uniques
420420

421421
@cache_readonly
422-
def groups(self) -> dict:
422+
def groups(self) -> Dict[Hashable, np.ndarray]:
423423
return self.index.groupby(Categorical.from_codes(self.codes, self.group_index))
424424

425425

pandas/core/indexes/base.py

Lines changed: 3 additions & 3 deletions
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 FrozenSet, Hashable, Optional, Union
4+
from typing import Dict, FrozenSet, Hashable, Optional, Union
55
import warnings
66

77
import numpy as np
@@ -4594,7 +4594,7 @@ def _maybe_promote(self, other):
45944594
return self.astype("object"), other.astype("object")
45954595
return self, other
45964596

4597-
def groupby(self, values):
4597+
def groupby(self, values) -> Dict[Hashable, np.ndarray]:
45984598
"""
45994599
Group the index labels by a given array of values.
46004600
@@ -4605,7 +4605,7 @@ def groupby(self, values):
46054605
46064606
Returns
46074607
-------
4608-
groups : dict
4608+
dict
46094609
{group name -> group labels}
46104610
"""
46114611

pandas/core/indexing.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Tuple
1+
from typing import Hashable, List, Tuple, Union
22

33
import numpy as np
44

@@ -2224,7 +2224,7 @@ def _convert_key(self, key, is_setter: bool = False):
22242224
return key
22252225

22262226

2227-
def _tuplify(ndim: int, loc) -> tuple:
2227+
def _tuplify(ndim: int, loc: Hashable) -> Tuple[Union[Hashable, slice], ...]:
22282228
"""
22292229
Given an indexer for the first dimension, create an equivalent tuple
22302230
for indexing over all dimensions.
@@ -2238,9 +2238,10 @@ def _tuplify(ndim: int, loc) -> tuple:
22382238
-------
22392239
tuple
22402240
"""
2241-
tup = [slice(None, None) for _ in range(ndim)]
2242-
tup[0] = loc
2243-
return tuple(tup)
2241+
_tup: List[Union[Hashable, slice]]
2242+
_tup = [slice(None, None) for _ in range(ndim)]
2243+
_tup[0] = loc
2244+
return tuple(_tup)
22442245

22452246

22462247
def convert_to_index_sliceable(obj, key):

pandas/io/pytables.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1452,7 +1452,7 @@ def copy(
14521452
data = self.select(k)
14531453
if isinstance(s, Table):
14541454

1455-
index: Union[bool, list] = False
1455+
index: Union[bool, List[str]] = False
14561456
if propindexes:
14571457
index = [a.name for a in s.axes if a.is_indexed]
14581458
new_store.append(

pandas/tests/dtypes/test_dtypes.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,9 @@ def test_construction_from_string(self):
408408
with pytest.raises(TypeError):
409409
PeriodDtype.construct_from_string("datetime64[ns, US/Eastern]")
410410

411+
with pytest.raises(TypeError, match="list"):
412+
PeriodDtype.construct_from_string([1, 2, 3])
413+
411414
def test_is_dtype(self):
412415
assert PeriodDtype.is_dtype(self.dtype)
413416
assert PeriodDtype.is_dtype("period[D]")

0 commit comments

Comments
 (0)