Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TYP: add Shape alias to pandas._typing #37128

Merged
merged 20 commits into from
Nov 4, 2020
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
5e4f0e8
ENH: add Shape alias to _typing
arw2019 Oct 15, 2020
e55300c
TYP: use Shape alias in core/arrays/_mixins.py
arw2019 Oct 15, 2020
c88296c
TYP: use Shape alias in core/arrays/base.py
arw2019 Oct 15, 2020
6ab0461
TYP: use Shape alias in core/dtypes/cast.py
arw2019 Oct 15, 2020
1874f1f
TYP: use Shape alias in core/groupby/ops.py
arw2019 Oct 15, 2020
bd44ab7
TYP: use Shape alias in core/indexes/base.py
arw2019 Oct 15, 2020
cce1fc5
TYP: use Shape alias in core/indexes/multi.py
arw2019 Oct 15, 2020
0270d8d
TYP: use Shape alias in core/internals/blocks.py
arw2019 Oct 15, 2020
fab97b4
TYP: use Shape alias in core/internals/concat.py
arw2019 Oct 15, 2020
82ee79b
TYP: use Shape alias in core/internals/managers.py
arw2019 Oct 15, 2020
be11b55
TYP: use Shape alias in core/internals/managers.py
arw2019 Oct 15, 2020
c1faa9f
TYP: use Shape alias in core/io/pytables.py
arw2019 Oct 15, 2020
759e04f
use Shape = Tuple[int]
arw2019 Oct 15, 2020
1bdc0f3
revert change to _maybe_upcast_for_op docstring
arw2019 Oct 16, 2020
5a11286
Merge remote-tracking branch 'upstream/master' into TYP_add_shape_alias
arw2019 Oct 29, 2020
9f97162
Merge remote-tracking branch 'upstream/master' into TYP_add_shape_alias
arw2019 Oct 30, 2020
3c1679e
Merge remote-tracking branch 'upstream/master' into TYP_add_shape_alias
arw2019 Nov 1, 2020
ef2dd72
merge with master
arw2019 Nov 1, 2020
7f8a811
Merge remote-tracking branch 'upstream/master' into TYP_add_shape_alias
arw2019 Nov 4, 2020
b6c93ee
Merge remote-tracking branch 'upstream/master' into TYP_add_shape_alias
arw2019 Nov 4, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions pandas/_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
Mapping,
Optional,
Sequence,
Tuple,
Type,
TypeVar,
Union,
Expand Down Expand Up @@ -85,6 +86,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
Expand Down
5 changes: 3 additions & 2 deletions pandas/core/arrays/_mixins.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions pandas/core/arrays/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
"""
Expand Down
4 changes: 2 additions & 2 deletions pandas/core/dtypes/cast.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions pandas/core/groupby/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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):
Expand Down
4 changes: 2 additions & 2 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
"""
Expand Down
4 changes: 2 additions & 2 deletions pandas/core/indexes/multi.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
"""
Expand Down
4 changes: 2 additions & 2 deletions pandas/core/internals/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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):
Expand Down
4 changes: 2 additions & 2 deletions pandas/core/internals/concat.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 = {}
Expand Down
6 changes: 3 additions & 3 deletions pandas/core/internals/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down
6 changes: 3 additions & 3 deletions pandas/core/ops/array_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions pandas/io/pytables.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -3087,7 +3087,7 @@ class BlockManagerFixed(GenericFixed):
nblocks: int

@property
def shape(self):
def shape(self) -> Optional[Shape]:
try:
ndim = self.ndim

Expand Down