From 5e4f0e893750324917e9a44bb0ca7788e11fe5b6 Mon Sep 17 00:00:00 2001 From: arw2019 Date: Thu, 15 Oct 2020 03:56:43 +0000 Subject: [PATCH 01/14] ENH: add Shape alias to _typing --- pandas/_typing.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/pandas/_typing.py b/pandas/_typing.py index 7678d1bf12d8b..643e4e8cf3b9b 100644 --- a/pandas/_typing.py +++ b/pandas/_typing.py @@ -16,6 +16,7 @@ Mapping, Optional, Sequence, + Tuple, Type, TypeVar, Union, @@ -85,6 +86,7 @@ Label = Optional[Hashable] IndexLabel = Union[Label, Sequence[Label]] Level = Union[Label, int] +Shape = Union[int, Tuple[int, ...]] Ordered = Optional[bool] JSONSerializable = Optional[Union[PythonScalar, List, Dict]] Axes = Collection From e55300ccbb46a2e9ee5bdfe4623ab1d0b9630823 Mon Sep 17 00:00:00 2001 From: arw2019 Date: Thu, 15 Oct 2020 04:02:49 +0000 Subject: [PATCH 02/14] TYP: use Shape alias in core/arrays/_mixins.py --- pandas/core/arrays/_mixins.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/pandas/core/arrays/_mixins.py b/pandas/core/arrays/_mixins.py index 948ffdc1f7c01..5b5d606e8d912 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 @@ -84,7 +85,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: From c88296cfacc8da3423a995b7460fa0a11b2def4d Mon Sep 17 00:00:00 2001 From: arw2019 Date: Thu, 15 Oct 2020 04:03:26 +0000 Subject: [PATCH 03/14] TYP: use Shape alias in core/arrays/base.py --- pandas/core/arrays/base.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/core/arrays/base.py b/pandas/core/arrays/base.py index debfb50caeeaa..a11d7bf134073 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. """ From 6ab0461a5dd6924de4eca5ed74a20de38c79499c Mon Sep 17 00:00:00 2001 From: arw2019 Date: Thu, 15 Oct 2020 04:04:00 +0000 Subject: [PATCH 04/14] TYP: use Shape alias in core/dtypes/cast.py --- pandas/core/dtypes/cast.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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. From 1874f1f68a6438009aac577eae50278b3ee3710d Mon Sep 17 00:00:00 2001 From: arw2019 Date: Thu, 15 Oct 2020 04:04:19 +0000 Subject: [PATCH 05/14] TYP: use Shape alias in core/groupby/ops.py --- pandas/core/groupby/ops.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/core/groupby/ops.py b/pandas/core/groupby/ops.py index bca71b5c9646b..0859865477457 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): From bd44ab7fec9857bd36bdee929dcb6453476811b4 Mon Sep 17 00:00:00 2001 From: arw2019 Date: Thu, 15 Oct 2020 04:04:37 +0000 Subject: [PATCH 06/14] TYP: use Shape alias in core/indexes/base.py --- pandas/core/indexes/base.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index 2ebf2389823e9..e7bd87b7532f9 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -26,7 +26,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 +from pandas._typing import AnyArrayLike, Dtype, DtypeObj, Label, Shape from pandas.compat.numpy import function as nv from pandas.errors import DuplicateLabelError, InvalidIndexError from pandas.util._decorators import Appender, cache_readonly, doc @@ -5542,7 +5542,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. """ From cce1fc5c8bcf080e951d948dfe23018b95ff445c Mon Sep 17 00:00:00 2001 From: arw2019 Date: Thu, 15 Oct 2020 04:04:50 +0000 Subject: [PATCH 07/14] TYP: use Shape alias in core/indexes/multi.py --- pandas/core/indexes/multi.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/core/indexes/multi.py b/pandas/core/indexes/multi.py index 4ba7dc58a3527..9e650d78c339f 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. """ From 0270d8de2d0da1bcb94fd5938b3fd421a4b0a2b2 Mon Sep 17 00:00:00 2001 From: arw2019 Date: Thu, 15 Oct 2020 04:05:31 +0000 Subject: [PATCH 08/14] TYP: use Shape alias in core/internals/blocks.py --- pandas/core/internals/blocks.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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): From fab97b489335b3a70527f2bdf54c74b8285c1261 Mon Sep 17 00:00:00 2001 From: arw2019 Date: Thu, 15 Oct 2020 04:06:08 +0000 Subject: [PATCH 09/14] TYP: use Shape alias in core/internals/concat.py --- pandas/core/internals/concat.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/core/internals/concat.py b/pandas/core/internals/concat.py index 02c9a76b70cd3..b9911e5d4842c 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 = {} From 82ee79bd3446ac45a0d826c645de115a1fcef7ed Mon Sep 17 00:00:00 2001 From: arw2019 Date: Thu, 15 Oct 2020 04:06:23 +0000 Subject: [PATCH 10/14] TYP: use Shape alias in core/internals/managers.py --- pandas/core/internals/managers.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pandas/core/internals/managers.py b/pandas/core/internals/managers.py index 8b67788b1a1a1..967af6f4304a3 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: From be11b556361bae74de1a90b6e5641f596a79da11 Mon Sep 17 00:00:00 2001 From: arw2019 Date: Thu, 15 Oct 2020 04:06:54 +0000 Subject: [PATCH 11/14] TYP: use Shape alias in core/internals/managers.py --- pandas/core/ops/array_ops.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/pandas/core/ops/array_ops.py b/pandas/core/ops/array_ops.py index 97fa7988c1774..8ad2ce850058d 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. @@ -435,7 +435,7 @@ def _maybe_upcast_for_op(obj, shape: Tuple[int, ...]): Parameters ---------- obj: object - shape : tuple[int] + shape : tuple[int] or int Returns ------- From c1faa9fd43726d3c04d8453252dc59af61fedd80 Mon Sep 17 00:00:00 2001 From: arw2019 Date: Thu, 15 Oct 2020 04:07:35 +0000 Subject: [PATCH 12/14] TYP: use Shape alias in core/io/pytables.py --- pandas/io/pytables.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/io/pytables.py b/pandas/io/pytables.py index ffc3a4501470f..92242f57bee84 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 @@ -3087,7 +3087,7 @@ class BlockManagerFixed(GenericFixed): nblocks: int @property - def shape(self): + def shape(self) -> Optional[Shape]: try: ndim = self.ndim From 759e04f7a31a0e3154efe2e3a49613737e204286 Mon Sep 17 00:00:00 2001 From: arw2019 Date: Thu, 15 Oct 2020 12:11:10 +0000 Subject: [PATCH 13/14] use Shape = Tuple[int] --- pandas/_typing.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/_typing.py b/pandas/_typing.py index 643e4e8cf3b9b..f035bf13d5e8b 100644 --- a/pandas/_typing.py +++ b/pandas/_typing.py @@ -86,7 +86,7 @@ Label = Optional[Hashable] IndexLabel = Union[Label, Sequence[Label]] Level = Union[Label, int] -Shape = Union[int, Tuple[int, ...]] +Shape = Tuple[int, ...] Ordered = Optional[bool] JSONSerializable = Optional[Union[PythonScalar, List, Dict]] Axes = Collection From 1bdc0f33e21470198505c4510aae9c8e256478af Mon Sep 17 00:00:00 2001 From: arw2019 Date: Fri, 16 Oct 2020 18:13:52 +0000 Subject: [PATCH 14/14] revert change to _maybe_upcast_for_op docstring --- pandas/core/ops/array_ops.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/ops/array_ops.py b/pandas/core/ops/array_ops.py index 8ad2ce850058d..8142fc3e695a3 100644 --- a/pandas/core/ops/array_ops.py +++ b/pandas/core/ops/array_ops.py @@ -435,7 +435,7 @@ def _maybe_upcast_for_op(obj, shape: Shape): Parameters ---------- obj: object - shape : tuple[int] or int + shape : tuple[int] Returns -------