Skip to content

Commit f8f7f8b

Browse files
fathomerJulianWgs
authored andcommitted
STY: remove --keep-runtime-typing from pyupgrade pandas-dev#40759 Part-2 (pandas-dev#40802)
1 parent c7853d6 commit f8f7f8b

20 files changed

+191
-274
lines changed

pandas/_typing.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
Optional,
2626
Sequence,
2727
Tuple,
28-
Type,
28+
Type as type_t,
2929
TypeVar,
3030
Union,
3131
)
@@ -119,7 +119,7 @@
119119
# dtypes
120120
NpDtype = Union[str, np.dtype]
121121
Dtype = Union[
122-
"ExtensionDtype", NpDtype, Type[Union[str, float, int, complex, bool, object]]
122+
"ExtensionDtype", NpDtype, type_t[Union[str, float, int, complex, bool, object]]
123123
]
124124
# DtypeArg specifies all allowable dtypes in a functions its dtype argument
125125
DtypeArg = Union[Dtype, Dict[Hashable, Dtype]]

pandas/core/arrays/categorical.py

+10-15
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,8 @@
66
from shutil import get_terminal_size
77
from typing import (
88
TYPE_CHECKING,
9-
Dict,
109
Hashable,
11-
List,
12-
Optional,
1310
Sequence,
14-
Tuple,
15-
Type,
1611
TypeVar,
1712
Union,
1813
cast,
@@ -359,7 +354,7 @@ def __init__(
359354
values,
360355
categories=None,
361356
ordered=None,
362-
dtype: Optional[Dtype] = None,
357+
dtype: Dtype | None = None,
363358
fastpath=False,
364359
copy: bool = True,
365360
):
@@ -473,11 +468,11 @@ def dtype(self) -> CategoricalDtype:
473468
return self._dtype
474469

475470
@property
476-
def _constructor(self) -> Type[Categorical]:
471+
def _constructor(self) -> type[Categorical]:
477472
return Categorical
478473

479474
@classmethod
480-
def _from_sequence(cls, scalars, *, dtype: Optional[Dtype] = None, copy=False):
475+
def _from_sequence(cls, scalars, *, dtype: Dtype | None = None, copy=False):
481476
return Categorical(scalars, dtype=dtype, copy=copy)
482477

483478
def astype(self, dtype: Dtype, copy: bool = True) -> ArrayLike:
@@ -547,7 +542,7 @@ def itemsize(self) -> int:
547542
"""
548543
return self.categories.itemsize
549544

550-
def tolist(self) -> List[Scalar]:
545+
def tolist(self) -> list[Scalar]:
551546
"""
552547
Return a list of the values.
553548
@@ -630,7 +625,7 @@ def _from_inferred_categories(
630625

631626
@classmethod
632627
def from_codes(
633-
cls, codes, categories=None, ordered=None, dtype: Optional[Dtype] = None
628+
cls, codes, categories=None, ordered=None, dtype: Dtype | None = None
634629
):
635630
"""
636631
Make a Categorical type from codes and categories or dtype.
@@ -1369,7 +1364,7 @@ def _validate_fill_value(self, fill_value):
13691364

13701365
# -------------------------------------------------------------
13711366

1372-
def __array__(self, dtype: Optional[NpDtype] = None) -> np.ndarray:
1367+
def __array__(self, dtype: NpDtype | None = None) -> np.ndarray:
13731368
"""
13741369
The numpy array interface.
13751370
@@ -1960,7 +1955,7 @@ def _validate_setitem_value(self, value):
19601955
codes = self.categories.get_indexer(rvalue)
19611956
return codes.astype(self._ndarray.dtype, copy=False)
19621957

1963-
def _reverse_indexer(self) -> Dict[Hashable, np.ndarray]:
1958+
def _reverse_indexer(self) -> dict[Hashable, np.ndarray]:
19641959
"""
19651960
Compute the inverse of a categorical, returning
19661961
a dict of categories -> indexers.
@@ -2187,7 +2182,7 @@ def equals(self, other: object) -> bool:
21872182

21882183
@classmethod
21892184
def _concat_same_type(
2190-
cls: Type[CategoricalT], to_concat: Sequence[CategoricalT], axis: int = 0
2185+
cls: type[CategoricalT], to_concat: Sequence[CategoricalT], axis: int = 0
21912186
) -> CategoricalT:
21922187
from pandas.core.dtypes.concat import union_categoricals
21932188

@@ -2643,7 +2638,7 @@ def recode_for_categories(
26432638
return new_codes
26442639

26452640

2646-
def factorize_from_iterable(values) -> Tuple[np.ndarray, Index]:
2641+
def factorize_from_iterable(values) -> tuple[np.ndarray, Index]:
26472642
"""
26482643
Factorize an input `values` into `categories` and `codes`. Preserves
26492644
categorical dtype in `categories`.
@@ -2682,7 +2677,7 @@ def factorize_from_iterable(values) -> Tuple[np.ndarray, Index]:
26822677
return codes, categories
26832678

26842679

2685-
def factorize_from_iterables(iterables) -> Tuple[List[np.ndarray], List[Index]]:
2680+
def factorize_from_iterables(iterables) -> tuple[list[np.ndarray], list[Index]]:
26862681
"""
26872682
A higher-level wrapper over `factorize_from_iterable`.
26882683

pandas/core/arrays/datetimelike.py

+29-32
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,7 @@
99
TYPE_CHECKING,
1010
Any,
1111
Callable,
12-
Optional,
1312
Sequence,
14-
Tuple,
15-
Type,
1613
TypeVar,
1714
Union,
1815
cast,
@@ -156,25 +153,25 @@ class DatetimeLikeArrayMixin(OpsMixin, NDArrayBackedExtensionArray):
156153
"""
157154

158155
# _infer_matches -> which infer_dtype strings are close enough to our own
159-
_infer_matches: Tuple[str, ...]
156+
_infer_matches: tuple[str, ...]
160157
_is_recognized_dtype: Callable[[DtypeObj], bool]
161-
_recognized_scalars: Tuple[Type, ...]
158+
_recognized_scalars: tuple[type, ...]
162159
_ndarray: np.ndarray
163160

164-
def __init__(self, data, dtype: Optional[Dtype] = None, freq=None, copy=False):
161+
def __init__(self, data, dtype: Dtype | None = None, freq=None, copy=False):
165162
raise AbstractMethodError(self)
166163

167164
@classmethod
168165
def _simple_new(
169-
cls: Type[DatetimeLikeArrayT],
166+
cls: type[DatetimeLikeArrayT],
170167
values: np.ndarray,
171-
freq: Optional[BaseOffset] = None,
172-
dtype: Optional[Dtype] = None,
168+
freq: BaseOffset | None = None,
169+
dtype: Dtype | None = None,
173170
) -> DatetimeLikeArrayT:
174171
raise AbstractMethodError(cls)
175172

176173
@property
177-
def _scalar_type(self) -> Type[DatetimeLikeScalar]:
174+
def _scalar_type(self) -> type[DatetimeLikeScalar]:
178175
"""
179176
The scalar associated with this datelike
180177
@@ -206,7 +203,7 @@ def _scalar_from_string(self, value: str) -> DTScalarOrNaT:
206203

207204
def _unbox_scalar(
208205
self, value: DTScalarOrNaT, setitem: bool = False
209-
) -> Union[np.int64, np.datetime64, np.timedelta64]:
206+
) -> np.int64 | np.datetime64 | np.timedelta64:
210207
"""
211208
Unbox the integer value of a scalar `value`.
212209
@@ -334,15 +331,15 @@ def _formatter(self, boxed: bool = False):
334331
# ----------------------------------------------------------------
335332
# Array-Like / EA-Interface Methods
336333

337-
def __array__(self, dtype: Optional[NpDtype] = None) -> np.ndarray:
334+
def __array__(self, dtype: NpDtype | None = None) -> np.ndarray:
338335
# used for Timedelta/DatetimeArray, overwritten by PeriodArray
339336
if is_object_dtype(dtype):
340337
return np.array(list(self), dtype=object)
341338
return self._ndarray
342339

343340
def __getitem__(
344-
self, key: Union[int, slice, np.ndarray]
345-
) -> Union[DatetimeLikeArrayMixin, DTScalarOrNaT]:
341+
self, key: int | slice | np.ndarray
342+
) -> DatetimeLikeArrayMixin | DTScalarOrNaT:
346343
"""
347344
This getitem defers to the underlying array, which by-definition can
348345
only handle list-likes, slices, and integer scalars
@@ -354,7 +351,7 @@ def __getitem__(
354351
result._freq = self._get_getitem_freq(key)
355352
return result
356353

357-
def _get_getitem_freq(self, key) -> Optional[BaseOffset]:
354+
def _get_getitem_freq(self, key) -> BaseOffset | None:
358355
"""
359356
Find the `freq` attribute to assign to the result of a __getitem__ lookup.
360357
"""
@@ -386,8 +383,8 @@ def _get_getitem_freq(self, key) -> Optional[BaseOffset]:
386383
# ndarray]"
387384
def __setitem__( # type: ignore[override]
388385
self,
389-
key: Union[int, Sequence[int], Sequence[bool], slice],
390-
value: Union[NaTType, Any, Sequence[Any]],
386+
key: int | Sequence[int] | Sequence[bool] | slice,
387+
value: NaTType | Any | Sequence[Any],
391388
) -> None:
392389
# I'm fudging the types a bit here. "Any" above really depends
393390
# on type(self). For PeriodArray, it's Period (or stuff coercible
@@ -469,10 +466,10 @@ def view(self, dtype: Literal["m8[ns]"]) -> TimedeltaArray:
469466
...
470467

471468
@overload
472-
def view(self, dtype: Optional[Dtype] = ...) -> ArrayLike:
469+
def view(self, dtype: Dtype | None = ...) -> ArrayLike:
473470
...
474471

475-
def view(self, dtype: Optional[Dtype] = None) -> ArrayLike:
472+
def view(self, dtype: Dtype | None = None) -> ArrayLike:
476473
# We handle datetime64, datetime64tz, timedelta64, and period
477474
# dtypes here. Everything else we pass through to the underlying
478475
# ndarray.
@@ -509,7 +506,7 @@ def view(self, dtype: Optional[Dtype] = None) -> ArrayLike:
509506

510507
@classmethod
511508
def _concat_same_type(
512-
cls: Type[DatetimeLikeArrayT],
509+
cls: type[DatetimeLikeArrayT],
513510
to_concat: Sequence[DatetimeLikeArrayT],
514511
axis: int = 0,
515512
) -> DatetimeLikeArrayT:
@@ -545,7 +542,7 @@ def _values_for_factorize(self):
545542

546543
@classmethod
547544
def _from_factorized(
548-
cls: Type[DatetimeLikeArrayT], values, original: DatetimeLikeArrayT
545+
cls: type[DatetimeLikeArrayT], values, original: DatetimeLikeArrayT
549546
) -> DatetimeLikeArrayT:
550547
return cls(values, dtype=original.dtype)
551548

@@ -789,7 +786,7 @@ def _validate_setitem_value(self, value):
789786

790787
def _unbox(
791788
self, other, setitem: bool = False
792-
) -> Union[np.int64, np.datetime64, np.timedelta64, np.ndarray]:
789+
) -> np.int64 | np.datetime64 | np.timedelta64 | np.ndarray:
793790
"""
794791
Unbox either a scalar with _unbox_scalar or an instance of our own type.
795792
"""
@@ -939,7 +936,7 @@ def freq(self, value):
939936
self._freq = value
940937

941938
@property
942-
def freqstr(self) -> Optional[str]:
939+
def freqstr(self) -> str | None:
943940
"""
944941
Return the frequency object as a string if its set, otherwise None.
945942
"""
@@ -948,7 +945,7 @@ def freqstr(self) -> Optional[str]:
948945
return self.freq.freqstr
949946

950947
@property # NB: override with cache_readonly in immutable subclasses
951-
def inferred_freq(self) -> Optional[str]:
948+
def inferred_freq(self) -> str | None:
952949
"""
953950
Tries to return a string representing a frequency guess,
954951
generated by infer_freq. Returns None if it can't autodetect the
@@ -962,7 +959,7 @@ def inferred_freq(self) -> Optional[str]:
962959
return None
963960

964961
@property # NB: override with cache_readonly in immutable subclasses
965-
def _resolution_obj(self) -> Optional[Resolution]:
962+
def _resolution_obj(self) -> Resolution | None:
966963
freqstr = self.freqstr
967964
if freqstr is None:
968965
return None
@@ -1020,7 +1017,7 @@ def _validate_frequency(cls, index, freq, **kwargs):
10201017

10211018
@classmethod
10221019
def _generate_range(
1023-
cls: Type[DatetimeLikeArrayT], start, end, periods, freq, *args, **kwargs
1020+
cls: type[DatetimeLikeArrayT], start, end, periods, freq, *args, **kwargs
10241021
) -> DatetimeLikeArrayT:
10251022
raise AbstractMethodError(cls)
10261023

@@ -1443,7 +1440,7 @@ def __isub__(self, other):
14431440
# --------------------------------------------------------------
14441441
# Reductions
14451442

1446-
def min(self, *, axis: Optional[int] = None, skipna: bool = True, **kwargs):
1443+
def min(self, *, axis: int | None = None, skipna: bool = True, **kwargs):
14471444
"""
14481445
Return the minimum value of the Array or minimum along
14491446
an axis.
@@ -1472,7 +1469,7 @@ def min(self, *, axis: Optional[int] = None, skipna: bool = True, **kwargs):
14721469
result = nanops.nanmin(self._ndarray, axis=axis, skipna=skipna)
14731470
return self._wrap_reduction_result(axis, result)
14741471

1475-
def max(self, *, axis: Optional[int] = None, skipna: bool = True, **kwargs):
1472+
def max(self, *, axis: int | None = None, skipna: bool = True, **kwargs):
14761473
"""
14771474
Return the maximum value of the Array or maximum along
14781475
an axis.
@@ -1503,7 +1500,7 @@ def max(self, *, axis: Optional[int] = None, skipna: bool = True, **kwargs):
15031500
result = nanops.nanmax(self._ndarray, axis=axis, skipna=skipna)
15041501
return self._wrap_reduction_result(axis, result)
15051502

1506-
def mean(self, *, skipna: bool = True, axis: Optional[int] = 0):
1503+
def mean(self, *, skipna: bool = True, axis: int | None = 0):
15071504
"""
15081505
Return the mean value of the Array.
15091506
@@ -1542,7 +1539,7 @@ def mean(self, *, skipna: bool = True, axis: Optional[int] = 0):
15421539
)
15431540
return self._wrap_reduction_result(axis, result)
15441541

1545-
def median(self, *, axis: Optional[int] = None, skipna: bool = True, **kwargs):
1542+
def median(self, *, axis: int | None = None, skipna: bool = True, **kwargs):
15461543
nv.validate_median((), kwargs)
15471544

15481545
if axis is not None and abs(axis) >= self.ndim:
@@ -1752,11 +1749,11 @@ def ceil(self, freq, ambiguous="raise", nonexistent="raise"):
17521749
# --------------------------------------------------------------
17531750
# Reductions
17541751

1755-
def any(self, *, axis: Optional[int] = None, skipna: bool = True):
1752+
def any(self, *, axis: int | None = None, skipna: bool = True):
17561753
# GH#34479 discussion of desired behavior long-term
17571754
return nanops.nanany(self._ndarray, axis=axis, skipna=skipna, mask=self.isna())
17581755

1759-
def all(self, *, axis: Optional[int] = None, skipna: bool = True):
1756+
def all(self, *, axis: int | None = None, skipna: bool = True):
17601757
# GH#34479 discussion of desired behavior long-term
17611758
return nanops.nanall(self._ndarray, axis=axis, skipna=skipna, mask=self.isna())
17621759

0 commit comments

Comments
 (0)