From 399743c2e37da929e430a823e9b83439f85b52fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torsten=20W=C3=B6rtwein?= Date: Fri, 25 Mar 2022 17:58:31 -0400 Subject: [PATCH 1/4] TYP: misc --- pandas/_typing.py | 2 ++ pandas/core/arrays/datetimelike.py | 4 +++- pandas/core/arrays/period.py | 2 +- pandas/core/arrays/timedeltas.py | 3 ++- pandas/core/indexes/base.py | 4 +++- pandas/core/indexes/interval.py | 4 +++- pandas/core/indexes/multi.py | 4 +++- pandas/core/indexes/numeric.py | 2 +- pandas/core/reshape/util.py | 3 ++- pandas/io/formats/csvs.py | 2 +- 10 files changed, 21 insertions(+), 9 deletions(-) diff --git a/pandas/_typing.py b/pandas/_typing.py index e3b3a4774f558..e943196e80908 100644 --- a/pandas/_typing.py +++ b/pandas/_typing.py @@ -104,6 +104,8 @@ # passed in, a DataFrame is always returned. NDFrameT = TypeVar("NDFrameT", bound="NDFrame") +NumpyIndexT = TypeVar("NumpyIndexT", np.ndarray, Index) + Axis = Union[str, int] IndexLabel = Union[Hashable, Sequence[Hashable]] Level = Union[Hashable, int] diff --git a/pandas/core/arrays/datetimelike.py b/pandas/core/arrays/datetimelike.py index 8f0516abe8bb3..27260f8ed62ca 100644 --- a/pandas/core/arrays/datetimelike.py +++ b/pandas/core/arrays/datetimelike.py @@ -295,7 +295,9 @@ def asi8(self) -> npt.NDArray[np.int64]: # ---------------------------------------------------------------- # Rendering Methods - def _format_native_types(self, *, na_rep="NaT", date_format=None): + def _format_native_types( + self, *, na_rep="NaT", date_format=None + ) -> npt.NDArray[np.object_]: """ Helper method for astype when converting to strings. diff --git a/pandas/core/arrays/period.py b/pandas/core/arrays/period.py index fa543f6773634..7d0b30a1abb60 100644 --- a/pandas/core/arrays/period.py +++ b/pandas/core/arrays/period.py @@ -635,7 +635,7 @@ def _formatter(self, boxed: bool = False): @dtl.ravel_compat def _format_native_types( self, *, na_rep="NaT", date_format=None, **kwargs - ) -> np.ndarray: + ) -> npt.NDArray[np.object_]: """ actually format my specific types """ diff --git a/pandas/core/arrays/timedeltas.py b/pandas/core/arrays/timedeltas.py index dc63cd92bbb2b..e4a9b156655e9 100644 --- a/pandas/core/arrays/timedeltas.py +++ b/pandas/core/arrays/timedeltas.py @@ -37,6 +37,7 @@ from pandas._typing import ( DtypeObj, NpDtype, + npt, ) from pandas.compat.numpy import function as nv from pandas.util._validators import validate_endpoints @@ -431,7 +432,7 @@ def _formatter(self, boxed: bool = False): @dtl.ravel_compat def _format_native_types( self, *, na_rep="NaT", date_format=None, **kwargs - ) -> np.ndarray: + ) -> npt.NDArray[np.object_]: from pandas.io.formats.format import get_format_timedelta64 formatter = get_format_timedelta64(self._ndarray, na_rep) diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index 8b9c537631d94..c34e99298dd0e 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -1511,7 +1511,9 @@ def to_native_types(self, slicer=None, **kwargs) -> np.ndarray: values = values[slicer] return values._format_native_types(**kwargs) - def _format_native_types(self, *, na_rep="", quoting=None, **kwargs): + def _format_native_types( + self, *, na_rep="", quoting=None, **kwargs + ) -> npt.NDArray[np.object_]: """ Actually format specific types of the index. """ diff --git a/pandas/core/indexes/interval.py b/pandas/core/indexes/interval.py index c1d7eb972e1f4..425e7d3f4432e 100644 --- a/pandas/core/indexes/interval.py +++ b/pandas/core/indexes/interval.py @@ -820,7 +820,9 @@ def _format_with_header(self, header: list[str], na_rep: str) -> list[str]: # matches base class except for whitespace padding return header + list(self._format_native_types(na_rep=na_rep)) - def _format_native_types(self, *, na_rep="NaN", quoting=None, **kwargs): + def _format_native_types( + self, *, na_rep="NaN", quoting=None, **kwargs + ) -> npt.NDArray[np.object_]: # GH 28210: use base method but with different default na_rep return super()._format_native_types(na_rep=na_rep, quoting=quoting, **kwargs) diff --git a/pandas/core/indexes/multi.py b/pandas/core/indexes/multi.py index 1105e7b8a274f..c55312026a893 100644 --- a/pandas/core/indexes/multi.py +++ b/pandas/core/indexes/multi.py @@ -1294,7 +1294,9 @@ def _formatter_func(self, tup): formatter_funcs = [level._formatter_func for level in self.levels] return tuple(func(val) for func, val in zip(formatter_funcs, tup)) - def _format_native_types(self, *, na_rep="nan", **kwargs): + def _format_native_types( + self, *, na_rep="nan", **kwargs + ) -> npt.NDArray[np.object_]: new_levels = [] new_codes = [] diff --git a/pandas/core/indexes/numeric.py b/pandas/core/indexes/numeric.py index be88b874908e8..174e0a7f81850 100644 --- a/pandas/core/indexes/numeric.py +++ b/pandas/core/indexes/numeric.py @@ -276,7 +276,7 @@ def _assert_safe_casting(cls, data: np.ndarray, subarr: np.ndarray) -> None: def _format_native_types( self, *, na_rep="", float_format=None, decimal=".", quoting=None, **kwargs - ): + ) -> npt.NDArray[np.object_]: from pandas.io.formats.format import FloatArrayFormatter if is_float_dtype(self.dtype): diff --git a/pandas/core/reshape/util.py b/pandas/core/reshape/util.py index d2c08712abacd..07cebbfcd159d 100644 --- a/pandas/core/reshape/util.py +++ b/pandas/core/reshape/util.py @@ -2,6 +2,7 @@ from pandas.core.dtypes.common import is_list_like +from pandas._typing import NumpyIndexT def cartesian_product(X): """ @@ -54,7 +55,7 @@ def cartesian_product(X): return [tile_compat(np.repeat(x, b[i]), np.product(a[i])) for i, x in enumerate(X)] -def tile_compat(arr, num: int): +def tile_compat(arr:NumpyIndexT, num: int) -> NumpyIndexT: """ Index compat for np.tile. diff --git a/pandas/io/formats/csvs.py b/pandas/io/formats/csvs.py index 3fd2a5e2bca32..c577acfaeba8e 100644 --- a/pandas/io/formats/csvs.py +++ b/pandas/io/formats/csvs.py @@ -73,7 +73,7 @@ def __init__( self.filepath_or_buffer = path_or_buf self.encoding = encoding - self.compression = compression + self.compression: CompressionOptions = compression self.mode = mode self.storage_options = storage_options From 78d69cf7a786cc4f72e17a08b488323da9a85a11 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torsten=20W=C3=B6rtwein?= Date: Fri, 25 Mar 2022 18:29:43 -0400 Subject: [PATCH 2/4] added space after : --- pandas/core/reshape/util.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/reshape/util.py b/pandas/core/reshape/util.py index 07cebbfcd159d..873520553d8d0 100644 --- a/pandas/core/reshape/util.py +++ b/pandas/core/reshape/util.py @@ -55,7 +55,7 @@ def cartesian_product(X): return [tile_compat(np.repeat(x, b[i]), np.product(a[i])) for i, x in enumerate(X)] -def tile_compat(arr:NumpyIndexT, num: int) -> NumpyIndexT: +def tile_compat(arr: NumpyIndexT, num: int) -> NumpyIndexT: """ Index compat for np.tile. From 31fac2dcccc1045d696744c281f716e484091c4b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torsten=20W=C3=B6rtwein?= Date: Fri, 25 Mar 2022 19:14:11 -0400 Subject: [PATCH 3/4] and isort --- pandas/core/reshape/util.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pandas/core/reshape/util.py b/pandas/core/reshape/util.py index 873520553d8d0..9f9143f4aaa60 100644 --- a/pandas/core/reshape/util.py +++ b/pandas/core/reshape/util.py @@ -1,8 +1,9 @@ import numpy as np +from pandas._typing import NumpyIndexT + from pandas.core.dtypes.common import is_list_like -from pandas._typing import NumpyIndexT def cartesian_product(X): """ From 71fa82fd8dd85d950e810430bba58fbd40d34ea8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torsten=20W=C3=B6rtwein?= Date: Fri, 25 Mar 2022 22:32:02 -0400 Subject: [PATCH 4/4] Index is imported within if --- pandas/_typing.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/_typing.py b/pandas/_typing.py index e943196e80908..30244e025e430 100644 --- a/pandas/_typing.py +++ b/pandas/_typing.py @@ -104,7 +104,7 @@ # passed in, a DataFrame is always returned. NDFrameT = TypeVar("NDFrameT", bound="NDFrame") -NumpyIndexT = TypeVar("NumpyIndexT", np.ndarray, Index) +NumpyIndexT = TypeVar("NumpyIndexT", np.ndarray, "Index") Axis = Union[str, int] IndexLabel = Union[Hashable, Sequence[Hashable]]