@@ -210,7 +210,7 @@ def _maybe_unbox_datetimelike(value: Scalar, dtype: DtypeObj) -> Scalar:
210
210
211
211
Notes
212
212
-----
213
- Caller is responsible for checking dtype.kind in ["m", "M"]
213
+ Caller is responsible for checking dtype.kind in "mM"
214
214
"""
215
215
if is_valid_na_for_dtype (value , dtype ):
216
216
# GH#36541: can't fill array directly with pd.NaT
@@ -295,7 +295,7 @@ def maybe_downcast_to_dtype(result: ArrayLike, dtype: str | np.dtype) -> ArrayLi
295
295
296
296
# a datetimelike
297
297
# GH12821, iNaT is cast to float
298
- if dtype .kind in [ "M" , "m" ] and result .dtype .kind in [ "i" , "f" ] :
298
+ if dtype .kind in "mM" and result .dtype .kind in "if" :
299
299
result = result .astype (dtype )
300
300
301
301
elif dtype .kind == "m" and result .dtype == _dtype_obj :
@@ -544,7 +544,7 @@ def ensure_dtype_can_hold_na(dtype: DtypeObj) -> DtypeObj:
544
544
return _dtype_obj
545
545
elif dtype .kind == "b" :
546
546
return _dtype_obj
547
- elif dtype .kind in [ "i" , "u" ] :
547
+ elif dtype .kind in "iu" :
548
548
return np .dtype (np .float64 )
549
549
return dtype
550
550
@@ -623,8 +623,7 @@ def _maybe_promote(dtype: np.dtype, fill_value=np.nan):
623
623
dtype = _dtype_obj
624
624
return dtype , fill_value
625
625
626
- kinds = ["i" , "u" , "f" , "c" , "m" , "M" ]
627
- if is_valid_na_for_dtype (fill_value , dtype ) and dtype .kind in kinds :
626
+ if is_valid_na_for_dtype (fill_value , dtype ) and dtype .kind in "iufcmM" :
628
627
dtype = ensure_dtype_can_hold_na (dtype )
629
628
fv = na_value_for_dtype (dtype )
630
629
return dtype , fv
@@ -1196,7 +1195,7 @@ def maybe_cast_to_datetime(
1196
1195
from pandas .core .arrays .datetimes import DatetimeArray
1197
1196
from pandas .core .arrays .timedeltas import TimedeltaArray
1198
1197
1199
- assert dtype .kind in [ "m" , "M" ]
1198
+ assert dtype .kind in "mM"
1200
1199
if not is_list_like (value ):
1201
1200
raise TypeError ("value must be listlike" )
1202
1201
@@ -1251,7 +1250,7 @@ def _ensure_nanosecond_dtype(dtype: DtypeObj) -> None:
1251
1250
# i.e. datetime64tz
1252
1251
pass
1253
1252
1254
- elif dtype .kind in [ "m" , "M" ] :
1253
+ elif dtype .kind in "mM" :
1255
1254
reso = get_unit_from_dtype (dtype )
1256
1255
if not is_supported_unit (reso ):
1257
1256
# pre-2.0 we would silently swap in nanos for lower-resolutions,
@@ -1294,7 +1293,7 @@ def find_result_type(left: ArrayLike, right: Any) -> DtypeObj:
1294
1293
1295
1294
if (
1296
1295
isinstance (left , np .ndarray )
1297
- and left .dtype .kind in [ "i" , "u" , "c" ]
1296
+ and left .dtype .kind in "iuc"
1298
1297
and (lib .is_integer (right ) or lib .is_float (right ))
1299
1298
):
1300
1299
# e.g. with int8 dtype and right=512, we want to end up with
@@ -1335,7 +1334,7 @@ def common_dtype_categorical_compat(
1335
1334
# GH#38240
1336
1335
1337
1336
# TODO: more generally, could do `not can_hold_na(dtype)`
1338
- if isinstance (dtype , np .dtype ) and dtype .kind in [ "i" , "u" ] :
1337
+ if isinstance (dtype , np .dtype ) and dtype .kind in "iu" :
1339
1338
for obj in objs :
1340
1339
# We don't want to accientally allow e.g. "categorical" str here
1341
1340
obj_dtype = getattr (obj , "dtype" , None )
@@ -1429,7 +1428,7 @@ def construct_2d_arraylike_from_scalar(
1429
1428
) -> np .ndarray :
1430
1429
shape = (length , width )
1431
1430
1432
- if dtype .kind in [ "m" , "M" ] :
1431
+ if dtype .kind in "mM" :
1433
1432
value = _maybe_box_and_unbox_datetimelike (value , dtype )
1434
1433
elif dtype == _dtype_obj :
1435
1434
if isinstance (value , (np .timedelta64 , np .datetime64 )):
@@ -1486,13 +1485,13 @@ def construct_1d_arraylike_from_scalar(
1486
1485
if length and is_integer_dtype (dtype ) and isna (value ):
1487
1486
# coerce if we have nan for an integer dtype
1488
1487
dtype = np .dtype ("float64" )
1489
- elif isinstance (dtype , np .dtype ) and dtype .kind in ( "U" , "S" ) :
1488
+ elif isinstance (dtype , np .dtype ) and dtype .kind in "US" :
1490
1489
# we need to coerce to object dtype to avoid
1491
1490
# to allow numpy to take our string as a scalar value
1492
1491
dtype = np .dtype ("object" )
1493
1492
if not isna (value ):
1494
1493
value = ensure_str (value )
1495
- elif dtype .kind in [ "M" , "m" ] :
1494
+ elif dtype .kind in "mM" :
1496
1495
value = _maybe_box_and_unbox_datetimelike (value , dtype )
1497
1496
1498
1497
subarr = np .empty (length , dtype = dtype )
@@ -1504,7 +1503,7 @@ def construct_1d_arraylike_from_scalar(
1504
1503
1505
1504
1506
1505
def _maybe_box_and_unbox_datetimelike (value : Scalar , dtype : DtypeObj ):
1507
- # Caller is responsible for checking dtype.kind in ["m", "M"]
1506
+ # Caller is responsible for checking dtype.kind in "mM"
1508
1507
1509
1508
if isinstance (value , dt .datetime ):
1510
1509
# we dont want to box dt64, in particular datetime64("NaT")
@@ -1642,7 +1641,7 @@ def maybe_cast_to_integer_array(arr: list | np.ndarray, dtype: np.dtype) -> np.n
1642
1641
f"To cast anyway, use pd.Series(values).astype({ dtype } )"
1643
1642
)
1644
1643
1645
- if arr .dtype .kind in [ "m" , "M" ] :
1644
+ if arr .dtype .kind in "mM" :
1646
1645
# test_constructor_maskedarray_nonfloat
1647
1646
raise TypeError (
1648
1647
f"Constructing a Series or DataFrame from { arr .dtype } values and "
@@ -1667,7 +1666,7 @@ def can_hold_element(arr: ArrayLike, element: Any) -> bool:
1667
1666
bool
1668
1667
"""
1669
1668
dtype = arr .dtype
1670
- if not isinstance (dtype , np .dtype ) or dtype .kind in [ "m" , "M" ] :
1669
+ if not isinstance (dtype , np .dtype ) or dtype .kind in "mM" :
1671
1670
if isinstance (dtype , (PeriodDtype , IntervalDtype , DatetimeTZDtype , np .dtype )):
1672
1671
# np.dtype here catches datetime64ns and timedelta64ns; we assume
1673
1672
# in this case that we have DatetimeArray/TimedeltaArray
@@ -1715,7 +1714,7 @@ def np_can_hold_element(dtype: np.dtype, element: Any) -> Any:
1715
1714
1716
1715
tipo = _maybe_infer_dtype_type (element )
1717
1716
1718
- if dtype .kind in [ "i" , "u" ] :
1717
+ if dtype .kind in "iu" :
1719
1718
if isinstance (element , range ):
1720
1719
if _dtype_can_hold_range (element , dtype ):
1721
1720
return element
@@ -1731,7 +1730,7 @@ def np_can_hold_element(dtype: np.dtype, element: Any) -> Any:
1731
1730
raise LossySetitemError
1732
1731
1733
1732
if tipo is not None :
1734
- if tipo .kind not in [ "i" , "u" ] :
1733
+ if tipo .kind not in "iu" :
1735
1734
if isinstance (element , np .ndarray ) and element .dtype .kind == "f" :
1736
1735
# If all can be losslessly cast to integers, then we can hold them
1737
1736
with np .errstate (invalid = "ignore" ):
@@ -1783,7 +1782,7 @@ def np_can_hold_element(dtype: np.dtype, element: Any) -> Any:
1783
1782
1784
1783
if tipo is not None :
1785
1784
# TODO: itemsize check?
1786
- if tipo .kind not in [ "f" , "i" , "u" ] :
1785
+ if tipo .kind not in "iuf" :
1787
1786
# Anything other than float/integer we cannot hold
1788
1787
raise LossySetitemError
1789
1788
if not isinstance (tipo , np .dtype ):
@@ -1819,7 +1818,7 @@ def np_can_hold_element(dtype: np.dtype, element: Any) -> Any:
1819
1818
raise LossySetitemError
1820
1819
1821
1820
if tipo is not None :
1822
- if tipo .kind in [ "c" , "f" , "i" , "u" ] :
1821
+ if tipo .kind in "iufc" :
1823
1822
return element
1824
1823
raise LossySetitemError
1825
1824
raise LossySetitemError
0 commit comments