diff --git a/pandas/_typing.py b/pandas/_typing.py index 3e89cf24632e2..55a1c17b0aa53 100644 --- a/pandas/_typing.py +++ b/pandas/_typing.py @@ -15,6 +15,7 @@ Mapping, Optional, Sequence, + Tuple, Type, TypeVar, Union, @@ -93,6 +94,7 @@ Label = Optional[Hashable] IndexLabel = Union[Label, Sequence[Label]] Level = Union[Label, int] +Shape = Tuple[int, ...] Ordered = Optional[bool] JSONSerializable = Optional[Union[PythonScalar, List, Dict]] Axes = Collection diff --git a/pandas/core/arrays/_mixins.py b/pandas/core/arrays/_mixins.py index a2371a39a0efa..67ac2a3688214 100644 --- a/pandas/core/arrays/_mixins.py +++ b/pandas/core/arrays/_mixins.py @@ -1,8 +1,9 @@ -from typing import Any, Sequence, Tuple, TypeVar +from typing import Any, Sequence, TypeVar import numpy as np from pandas._libs import lib +from pandas._typing import Shape from pandas.compat.numpy import function as nv from pandas.errors import AbstractMethodError from pandas.util._decorators import cache_readonly, doc @@ -93,7 +94,7 @@ def _validate_fill_value(self, fill_value): # TODO: make this a cache_readonly; for that to work we need to remove # the _index_data kludge in libreduction @property - def shape(self) -> Tuple[int, ...]: + def shape(self) -> Shape: return self._ndarray.shape def __len__(self) -> int: diff --git a/pandas/core/arrays/base.py b/pandas/core/arrays/base.py index 82d79cc47a4ae..be105fd1f2a46 100644 --- a/pandas/core/arrays/base.py +++ b/pandas/core/arrays/base.py @@ -12,7 +12,7 @@ import numpy as np from pandas._libs import lib -from pandas._typing import ArrayLike +from pandas._typing import ArrayLike, Shape from pandas.compat import set_function_name from pandas.compat.numpy import function as nv from pandas.errors import AbstractMethodError @@ -403,7 +403,7 @@ def dtype(self) -> ExtensionDtype: raise AbstractMethodError(self) @property - def shape(self) -> Tuple[int, ...]: + def shape(self) -> Shape: """ Return a tuple of the array dimensions. """ diff --git a/pandas/core/dtypes/cast.py b/pandas/core/dtypes/cast.py index 692da8f8e021e..aded0af6aca0e 100644 --- a/pandas/core/dtypes/cast.py +++ b/pandas/core/dtypes/cast.py @@ -32,7 +32,7 @@ ints_to_pytimedelta, ) from pandas._libs.tslibs.timezones import tz_compare -from pandas._typing import AnyArrayLike, ArrayLike, Dtype, DtypeObj, Scalar +from pandas._typing import AnyArrayLike, ArrayLike, Dtype, DtypeObj, Scalar, Shape from pandas.util._validators import validate_bool_kwarg from pandas.core.dtypes.common import ( @@ -1591,7 +1591,7 @@ def find_common_type(types: List[DtypeObj]) -> DtypeObj: def cast_scalar_to_array( - shape: Tuple, value: Scalar, dtype: Optional[DtypeObj] = None + shape: Shape, value: Scalar, dtype: Optional[DtypeObj] = None ) -> np.ndarray: """ Create np.ndarray of specified shape and dtype, filled with values. diff --git a/pandas/core/groupby/ops.py b/pandas/core/groupby/ops.py index f807b740abaf2..15725230d850a 100644 --- a/pandas/core/groupby/ops.py +++ b/pandas/core/groupby/ops.py @@ -24,7 +24,7 @@ from pandas._libs import NaT, iNaT, lib import pandas._libs.groupby as libgroupby import pandas._libs.reduction as libreduction -from pandas._typing import F, FrameOrSeries, Label +from pandas._typing import F, FrameOrSeries, Label, Shape from pandas.errors import AbstractMethodError from pandas.util._decorators import cache_readonly @@ -116,7 +116,7 @@ def groupings(self) -> List["grouper.Grouping"]: return self._groupings @property - def shape(self) -> Tuple[int, ...]: + def shape(self) -> Shape: return tuple(ping.ngroups for ping in self.groupings) def __iter__(self): diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index b220756a24f9f..98ec3b55e65d9 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -27,7 +27,7 @@ from pandas._libs.lib import is_datetime_array, no_default from pandas._libs.tslibs import IncompatibleFrequency, OutOfBoundsDatetime, Timestamp from pandas._libs.tslibs.timezones import tz_compare -from pandas._typing import AnyArrayLike, Dtype, DtypeObj, Label, final +from pandas._typing import AnyArrayLike, Dtype, DtypeObj, Label, Shape, final from pandas.compat.numpy import function as nv from pandas.errors import DuplicateLabelError, InvalidIndexError from pandas.util._decorators import Appender, cache_readonly, doc @@ -5644,7 +5644,7 @@ def _maybe_disable_logical_methods(self, opname: str_t): make_invalid_op(opname)(self) @property - def shape(self): + def shape(self) -> Shape: """ Return a tuple of the shape of the underlying data. """ diff --git a/pandas/core/indexes/multi.py b/pandas/core/indexes/multi.py index dc5e6877a6bf5..65e71a6109a5a 100644 --- a/pandas/core/indexes/multi.py +++ b/pandas/core/indexes/multi.py @@ -20,7 +20,7 @@ from pandas._libs import algos as libalgos, index as libindex, lib from pandas._libs.hashtable import duplicated_int64 -from pandas._typing import AnyArrayLike, Label, Scalar +from pandas._typing import AnyArrayLike, Label, Scalar, Shape from pandas.compat.numpy import function as nv from pandas.errors import InvalidIndexError, PerformanceWarning, UnsortedIndexError from pandas.util._decorators import Appender, cache_readonly, doc @@ -702,7 +702,7 @@ def array(self): ) @property - def shape(self): + def shape(self) -> Shape: """ Return a tuple of the shape of the underlying data. """ diff --git a/pandas/core/internals/blocks.py b/pandas/core/internals/blocks.py index 24b00199611bf..ee630909cb990 100644 --- a/pandas/core/internals/blocks.py +++ b/pandas/core/internals/blocks.py @@ -10,7 +10,7 @@ from pandas._libs.internals import BlockPlacement from pandas._libs.tslibs import conversion from pandas._libs.tslibs.timezones import tz_compare -from pandas._typing import ArrayLike, Scalar +from pandas._typing import ArrayLike, Scalar, Shape from pandas.util._validators import validate_bool_kwarg from pandas.core.dtypes.cast import ( @@ -2762,7 +2762,7 @@ def _block_shape(values: ArrayLike, ndim: int = 1) -> ArrayLike: return values -def safe_reshape(arr, new_shape): +def safe_reshape(arr, new_shape: Shape): """ If possible, reshape `arr` to have shape `new_shape`, with a couple of exceptions (see gh-13012): diff --git a/pandas/core/internals/concat.py b/pandas/core/internals/concat.py index 8559fe72972b8..8efba87b14ce5 100644 --- a/pandas/core/internals/concat.py +++ b/pandas/core/internals/concat.py @@ -5,7 +5,7 @@ import numpy as np from pandas._libs import NaT, internals as libinternals -from pandas._typing import DtypeObj +from pandas._typing import DtypeObj, Shape from pandas.util._decorators import cache_readonly from pandas.core.dtypes.cast import maybe_promote @@ -175,7 +175,7 @@ def _get_mgr_concatenation_plan(mgr, indexers): class JoinUnit: - def __init__(self, block, shape, indexers=None): + def __init__(self, block, shape: Shape, indexers=None): # Passing shape explicitly is required for cases when block is None. if indexers is None: indexers = {} diff --git a/pandas/core/internals/managers.py b/pandas/core/internals/managers.py index 49ca8f9ad55e9..a06d57e268fe2 100644 --- a/pandas/core/internals/managers.py +++ b/pandas/core/internals/managers.py @@ -17,7 +17,7 @@ import numpy as np from pandas._libs import internals as libinternals, lib -from pandas._typing import ArrayLike, DtypeObj, Label +from pandas._typing import ArrayLike, DtypeObj, Label, Shape from pandas.util._validators import validate_bool_kwarg from pandas.core.dtypes.cast import ( @@ -204,7 +204,7 @@ def __nonzero__(self) -> bool: __bool__ = __nonzero__ @property - def shape(self) -> Tuple[int, ...]: + def shape(self) -> Shape: return tuple(len(ax) for ax in self.axes) @property @@ -1825,7 +1825,7 @@ def _asarray_compat(x): else: return np.asarray(x) - def _shape_compat(x): + def _shape_compat(x) -> Shape: if isinstance(x, ABCSeries): return (len(x),) else: diff --git a/pandas/core/ops/array_ops.py b/pandas/core/ops/array_ops.py index 97fa7988c1774..8142fc3e695a3 100644 --- a/pandas/core/ops/array_ops.py +++ b/pandas/core/ops/array_ops.py @@ -5,13 +5,13 @@ from datetime import timedelta from functools import partial import operator -from typing import Any, Tuple +from typing import Any import warnings import numpy as np from pandas._libs import Timedelta, Timestamp, lib, ops as libops -from pandas._typing import ArrayLike +from pandas._typing import ArrayLike, Shape from pandas.core.dtypes.cast import ( construct_1d_object_array_from_listlike, @@ -427,7 +427,7 @@ def maybe_upcast_datetimelike_array(obj: ArrayLike) -> ArrayLike: return obj -def _maybe_upcast_for_op(obj, shape: Tuple[int, ...]): +def _maybe_upcast_for_op(obj, shape: Shape): """ Cast non-pandas objects to pandas types to unify behavior of arithmetic and comparison operations. diff --git a/pandas/io/pytables.py b/pandas/io/pytables.py index bf21a8fe2fc74..890195688b1cb 100644 --- a/pandas/io/pytables.py +++ b/pandas/io/pytables.py @@ -28,7 +28,7 @@ from pandas._libs import lib, writers as libwriters from pandas._libs.tslibs import timezones -from pandas._typing import ArrayLike, FrameOrSeries, FrameOrSeriesUnion, Label +from pandas._typing import ArrayLike, FrameOrSeries, FrameOrSeriesUnion, Label, Shape from pandas.compat._optional import import_optional_dependency from pandas.compat.pickle_compat import patch_pickle from pandas.errors import PerformanceWarning @@ -3091,7 +3091,7 @@ class BlockManagerFixed(GenericFixed): nblocks: int @property - def shape(self): + def shape(self) -> Optional[Shape]: try: ndim = self.ndim