9
9
TYPE_CHECKING ,
10
10
Any ,
11
11
Callable ,
12
- Optional ,
13
12
Sequence ,
14
- Tuple ,
15
- Type ,
16
13
TypeVar ,
17
14
Union ,
18
15
cast ,
@@ -156,25 +153,25 @@ class DatetimeLikeArrayMixin(OpsMixin, NDArrayBackedExtensionArray):
156
153
"""
157
154
158
155
# _infer_matches -> which infer_dtype strings are close enough to our own
159
- _infer_matches : Tuple [str , ...]
156
+ _infer_matches : tuple [str , ...]
160
157
_is_recognized_dtype : Callable [[DtypeObj ], bool ]
161
- _recognized_scalars : Tuple [ Type , ...]
158
+ _recognized_scalars : tuple [ type , ...]
162
159
_ndarray : np .ndarray
163
160
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 ):
165
162
raise AbstractMethodError (self )
166
163
167
164
@classmethod
168
165
def _simple_new (
169
- cls : Type [DatetimeLikeArrayT ],
166
+ cls : type [DatetimeLikeArrayT ],
170
167
values : np .ndarray ,
171
- freq : Optional [ BaseOffset ] = None ,
172
- dtype : Optional [ Dtype ] = None ,
168
+ freq : BaseOffset | None = None ,
169
+ dtype : Dtype | None = None ,
173
170
) -> DatetimeLikeArrayT :
174
171
raise AbstractMethodError (cls )
175
172
176
173
@property
177
- def _scalar_type (self ) -> Type [DatetimeLikeScalar ]:
174
+ def _scalar_type (self ) -> type [DatetimeLikeScalar ]:
178
175
"""
179
176
The scalar associated with this datelike
180
177
@@ -206,7 +203,7 @@ def _scalar_from_string(self, value: str) -> DTScalarOrNaT:
206
203
207
204
def _unbox_scalar (
208
205
self , value : DTScalarOrNaT , setitem : bool = False
209
- ) -> Union [ np .int64 , np .datetime64 , np .timedelta64 ] :
206
+ ) -> np .int64 | np .datetime64 | np .timedelta64 :
210
207
"""
211
208
Unbox the integer value of a scalar `value`.
212
209
@@ -334,15 +331,15 @@ def _formatter(self, boxed: bool = False):
334
331
# ----------------------------------------------------------------
335
332
# Array-Like / EA-Interface Methods
336
333
337
- def __array__ (self , dtype : Optional [ NpDtype ] = None ) -> np .ndarray :
334
+ def __array__ (self , dtype : NpDtype | None = None ) -> np .ndarray :
338
335
# used for Timedelta/DatetimeArray, overwritten by PeriodArray
339
336
if is_object_dtype (dtype ):
340
337
return np .array (list (self ), dtype = object )
341
338
return self ._ndarray
342
339
343
340
def __getitem__ (
344
- self , key : Union [ int , slice , np .ndarray ]
345
- ) -> Union [ DatetimeLikeArrayMixin , DTScalarOrNaT ] :
341
+ self , key : int | slice | np .ndarray
342
+ ) -> DatetimeLikeArrayMixin | DTScalarOrNaT :
346
343
"""
347
344
This getitem defers to the underlying array, which by-definition can
348
345
only handle list-likes, slices, and integer scalars
@@ -354,7 +351,7 @@ def __getitem__(
354
351
result ._freq = self ._get_getitem_freq (key )
355
352
return result
356
353
357
- def _get_getitem_freq (self , key ) -> Optional [ BaseOffset ] :
354
+ def _get_getitem_freq (self , key ) -> BaseOffset | None :
358
355
"""
359
356
Find the `freq` attribute to assign to the result of a __getitem__ lookup.
360
357
"""
@@ -386,8 +383,8 @@ def _get_getitem_freq(self, key) -> Optional[BaseOffset]:
386
383
# ndarray]"
387
384
def __setitem__ ( # type: ignore[override]
388
385
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 ],
391
388
) -> None :
392
389
# I'm fudging the types a bit here. "Any" above really depends
393
390
# on type(self). For PeriodArray, it's Period (or stuff coercible
@@ -469,10 +466,10 @@ def view(self, dtype: Literal["m8[ns]"]) -> TimedeltaArray:
469
466
...
470
467
471
468
@overload
472
- def view (self , dtype : Optional [ Dtype ] = ...) -> ArrayLike :
469
+ def view (self , dtype : Dtype | None = ...) -> ArrayLike :
473
470
...
474
471
475
- def view (self , dtype : Optional [ Dtype ] = None ) -> ArrayLike :
472
+ def view (self , dtype : Dtype | None = None ) -> ArrayLike :
476
473
# We handle datetime64, datetime64tz, timedelta64, and period
477
474
# dtypes here. Everything else we pass through to the underlying
478
475
# ndarray.
@@ -509,7 +506,7 @@ def view(self, dtype: Optional[Dtype] = None) -> ArrayLike:
509
506
510
507
@classmethod
511
508
def _concat_same_type (
512
- cls : Type [DatetimeLikeArrayT ],
509
+ cls : type [DatetimeLikeArrayT ],
513
510
to_concat : Sequence [DatetimeLikeArrayT ],
514
511
axis : int = 0 ,
515
512
) -> DatetimeLikeArrayT :
@@ -545,7 +542,7 @@ def _values_for_factorize(self):
545
542
546
543
@classmethod
547
544
def _from_factorized (
548
- cls : Type [DatetimeLikeArrayT ], values , original : DatetimeLikeArrayT
545
+ cls : type [DatetimeLikeArrayT ], values , original : DatetimeLikeArrayT
549
546
) -> DatetimeLikeArrayT :
550
547
return cls (values , dtype = original .dtype )
551
548
@@ -789,7 +786,7 @@ def _validate_setitem_value(self, value):
789
786
790
787
def _unbox (
791
788
self , other , setitem : bool = False
792
- ) -> Union [ np .int64 , np .datetime64 , np .timedelta64 , np .ndarray ] :
789
+ ) -> np .int64 | np .datetime64 | np .timedelta64 | np .ndarray :
793
790
"""
794
791
Unbox either a scalar with _unbox_scalar or an instance of our own type.
795
792
"""
@@ -939,7 +936,7 @@ def freq(self, value):
939
936
self ._freq = value
940
937
941
938
@property
942
- def freqstr (self ) -> Optional [ str ] :
939
+ def freqstr (self ) -> str | None :
943
940
"""
944
941
Return the frequency object as a string if its set, otherwise None.
945
942
"""
@@ -948,7 +945,7 @@ def freqstr(self) -> Optional[str]:
948
945
return self .freq .freqstr
949
946
950
947
@property # NB: override with cache_readonly in immutable subclasses
951
- def inferred_freq (self ) -> Optional [ str ] :
948
+ def inferred_freq (self ) -> str | None :
952
949
"""
953
950
Tries to return a string representing a frequency guess,
954
951
generated by infer_freq. Returns None if it can't autodetect the
@@ -962,7 +959,7 @@ def inferred_freq(self) -> Optional[str]:
962
959
return None
963
960
964
961
@property # NB: override with cache_readonly in immutable subclasses
965
- def _resolution_obj (self ) -> Optional [ Resolution ] :
962
+ def _resolution_obj (self ) -> Resolution | None :
966
963
freqstr = self .freqstr
967
964
if freqstr is None :
968
965
return None
@@ -1020,7 +1017,7 @@ def _validate_frequency(cls, index, freq, **kwargs):
1020
1017
1021
1018
@classmethod
1022
1019
def _generate_range (
1023
- cls : Type [DatetimeLikeArrayT ], start , end , periods , freq , * args , ** kwargs
1020
+ cls : type [DatetimeLikeArrayT ], start , end , periods , freq , * args , ** kwargs
1024
1021
) -> DatetimeLikeArrayT :
1025
1022
raise AbstractMethodError (cls )
1026
1023
@@ -1443,7 +1440,7 @@ def __isub__(self, other):
1443
1440
# --------------------------------------------------------------
1444
1441
# Reductions
1445
1442
1446
- def min (self , * , axis : Optional [ int ] = None , skipna : bool = True , ** kwargs ):
1443
+ def min (self , * , axis : int | None = None , skipna : bool = True , ** kwargs ):
1447
1444
"""
1448
1445
Return the minimum value of the Array or minimum along
1449
1446
an axis.
@@ -1472,7 +1469,7 @@ def min(self, *, axis: Optional[int] = None, skipna: bool = True, **kwargs):
1472
1469
result = nanops .nanmin (self ._ndarray , axis = axis , skipna = skipna )
1473
1470
return self ._wrap_reduction_result (axis , result )
1474
1471
1475
- def max (self , * , axis : Optional [ int ] = None , skipna : bool = True , ** kwargs ):
1472
+ def max (self , * , axis : int | None = None , skipna : bool = True , ** kwargs ):
1476
1473
"""
1477
1474
Return the maximum value of the Array or maximum along
1478
1475
an axis.
@@ -1503,7 +1500,7 @@ def max(self, *, axis: Optional[int] = None, skipna: bool = True, **kwargs):
1503
1500
result = nanops .nanmax (self ._ndarray , axis = axis , skipna = skipna )
1504
1501
return self ._wrap_reduction_result (axis , result )
1505
1502
1506
- def mean (self , * , skipna : bool = True , axis : Optional [ int ] = 0 ):
1503
+ def mean (self , * , skipna : bool = True , axis : int | None = 0 ):
1507
1504
"""
1508
1505
Return the mean value of the Array.
1509
1506
@@ -1542,7 +1539,7 @@ def mean(self, *, skipna: bool = True, axis: Optional[int] = 0):
1542
1539
)
1543
1540
return self ._wrap_reduction_result (axis , result )
1544
1541
1545
- def median (self , * , axis : Optional [ int ] = None , skipna : bool = True , ** kwargs ):
1542
+ def median (self , * , axis : int | None = None , skipna : bool = True , ** kwargs ):
1546
1543
nv .validate_median ((), kwargs )
1547
1544
1548
1545
if axis is not None and abs (axis ) >= self .ndim :
@@ -1752,11 +1749,11 @@ def ceil(self, freq, ambiguous="raise", nonexistent="raise"):
1752
1749
# --------------------------------------------------------------
1753
1750
# Reductions
1754
1751
1755
- def any (self , * , axis : Optional [ int ] = None , skipna : bool = True ):
1752
+ def any (self , * , axis : int | None = None , skipna : bool = True ):
1756
1753
# GH#34479 discussion of desired behavior long-term
1757
1754
return nanops .nanany (self ._ndarray , axis = axis , skipna = skipna , mask = self .isna ())
1758
1755
1759
- def all (self , * , axis : Optional [ int ] = None , skipna : bool = True ):
1756
+ def all (self , * , axis : int | None = None , skipna : bool = True ):
1760
1757
# GH#34479 discussion of desired behavior long-term
1761
1758
return nanops .nanall (self ._ndarray , axis = axis , skipna = skipna , mask = self .isna ())
1762
1759
0 commit comments