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