diff --git a/pandas/_libs/lib.pyx b/pandas/_libs/lib.pyx index 4af02839d831b..5bf99301d9261 100644 --- a/pandas/_libs/lib.pyx +++ b/pandas/_libs/lib.pyx @@ -747,7 +747,7 @@ cpdef ndarray[object] ensure_string_array( if hasattr(arr, "to_numpy"): - if hasattr(arr, "dtype") and arr.dtype.kind in ["m", "M"]: + if hasattr(arr, "dtype") and arr.dtype.kind in "mM": # dtype check to exclude DataFrame # GH#41409 TODO: not a great place for this out = arr.astype(str).astype(object) @@ -2641,7 +2641,7 @@ def maybe_convert_objects(ndarray[object] objects, dtype = dtype_if_all_nat if cnp.PyArray_DescrCheck(dtype): # i.e. isinstance(dtype, np.dtype) - if dtype.kind not in ["m", "M"]: + if dtype.kind not in "mM": raise ValueError(dtype) else: res = np.empty((objects).shape, dtype=dtype) diff --git a/pandas/_libs/tslibs/np_datetime.pyx b/pandas/_libs/tslibs/np_datetime.pyx index 7e1a516e0d945..9980f28541201 100644 --- a/pandas/_libs/tslibs/np_datetime.pyx +++ b/pandas/_libs/tslibs/np_datetime.pyx @@ -419,7 +419,7 @@ def compare_mismatched_resolutions(ndarray left, ndarray right, op): array([ True]) """ - if left.dtype.kind != right.dtype.kind or left.dtype.kind not in ["m", "M"]: + if left.dtype.kind != right.dtype.kind or left.dtype.kind not in "mM": raise ValueError("left and right must both be timedelta64 or both datetime64") cdef: diff --git a/pandas/_libs/tslibs/timedeltas.pyx b/pandas/_libs/tslibs/timedeltas.pyx index f553a8c13bef8..563c2505a1812 100644 --- a/pandas/_libs/tslibs/timedeltas.pyx +++ b/pandas/_libs/tslibs/timedeltas.pyx @@ -775,7 +775,7 @@ def _binary_op_method_timedeltalike(op, name): item = cnp.PyArray_ToScalar(cnp.PyArray_DATA(other), other) return f(self, item) - elif other.dtype.kind in ["m", "M"]: + elif other.dtype.kind in "mM": return op(self.to_timedelta64(), other) elif other.dtype.kind == "O": return np.array([op(self, x) for x in other]) @@ -2028,7 +2028,7 @@ class Timedelta(_Timedelta): result[mask] = np.nan return result - elif other.dtype.kind in ["i", "u", "f"]: + elif other.dtype.kind in "iuf": if other.ndim == 0: return self // other.item() else: diff --git a/pandas/_libs/tslibs/timestamps.pyx b/pandas/_libs/tslibs/timestamps.pyx index cacebd5067651..fd89d0e795ecc 100644 --- a/pandas/_libs/tslibs/timestamps.pyx +++ b/pandas/_libs/tslibs/timestamps.pyx @@ -464,7 +464,7 @@ cdef class _Timestamp(ABCTimestamp): raise integer_op_not_supported(self) elif is_array(other): - if other.dtype.kind in ["i", "u"]: + if other.dtype.kind in "iu": raise integer_op_not_supported(self) if other.dtype.kind == "m": if self.tz is None: @@ -496,7 +496,7 @@ cdef class _Timestamp(ABCTimestamp): return self + neg_other elif is_array(other): - if other.dtype.kind in ["i", "u"]: + if other.dtype.kind in "iu": raise integer_op_not_supported(self) if other.dtype.kind == "m": if self.tz is None: diff --git a/pandas/_testing/asserters.py b/pandas/_testing/asserters.py index b0544a98e970f..3fa2150450452 100644 --- a/pandas/_testing/asserters.py +++ b/pandas/_testing/asserters.py @@ -530,7 +530,7 @@ def assert_interval_array_equal( _check_isinstance(left, right, IntervalArray) kwargs = {} - if left._left.dtype.kind in ["m", "M"]: + if left._left.dtype.kind in "mM": # We have a DatetimeArray or TimedeltaArray kwargs["check_freq"] = False diff --git a/pandas/core/algorithms.py b/pandas/core/algorithms.py index 7dbb0d260ce47..bf5c7beec7129 100644 --- a/pandas/core/algorithms.py +++ b/pandas/core/algorithms.py @@ -568,7 +568,7 @@ def factorize_array( uniques : ndarray """ original = values - if values.dtype.kind in ["m", "M"]: + if values.dtype.kind in "mM": # _get_hashtable_algo will cast dt64/td64 to i8 via _ensure_data, so we # need to do the same to na_value. We are assuming here that the passed # na_value is an appropriately-typed NaT. diff --git a/pandas/core/array_algos/datetimelike_accumulations.py b/pandas/core/array_algos/datetimelike_accumulations.py index d0c622742126b..825fe60ee6cf8 100644 --- a/pandas/core/array_algos/datetimelike_accumulations.py +++ b/pandas/core/array_algos/datetimelike_accumulations.py @@ -50,7 +50,7 @@ def _cum_func( result = func(y) result[mask] = iNaT - if values.dtype.kind in ["m", "M"]: + if values.dtype.kind in "mM": return result.view(values.dtype.base) return result diff --git a/pandas/core/array_algos/putmask.py b/pandas/core/array_algos/putmask.py index 3b9aad0790df9..06fb662ed0e2a 100644 --- a/pandas/core/array_algos/putmask.py +++ b/pandas/core/array_algos/putmask.py @@ -142,7 +142,7 @@ def setitem_datetimelike_compat(values: np.ndarray, num_set: int, other): if values.dtype == object: dtype, _ = infer_dtype_from(other, pandas_dtype=True) - if isinstance(dtype, np.dtype) and dtype.kind in ["m", "M"]: + if isinstance(dtype, np.dtype) and dtype.kind in "mM": # https://github.com/numpy/numpy/issues/12550 # timedelta64 will incorrectly cast to int if not is_list_like(other): diff --git a/pandas/core/array_algos/quantile.py b/pandas/core/array_algos/quantile.py index 7647f321e1d4b..a824359a2f6c8 100644 --- a/pandas/core/array_algos/quantile.py +++ b/pandas/core/array_algos/quantile.py @@ -180,7 +180,7 @@ def _nanpercentile( quantiles : scalar or array """ - if values.dtype.kind in ["m", "M"]: + if values.dtype.kind in "mM": # need to cast to integer to avoid rounding errors in numpy result = _nanpercentile( values.view("i8"), diff --git a/pandas/core/arrays/datetimelike.py b/pandas/core/arrays/datetimelike.py index 74c24f263c3da..3a76ad54a9c04 100644 --- a/pandas/core/arrays/datetimelike.py +++ b/pandas/core/arrays/datetimelike.py @@ -740,7 +740,7 @@ def isin(self, values) -> npt.NDArray[np.bool_]: if not hasattr(values, "dtype"): values = np.asarray(values) - if values.dtype.kind in ["f", "i", "u", "c"]: + if values.dtype.kind in "fiuc": # TODO: de-duplicate with equals, validate_comparison_value return np.zeros(self.shape, dtype=bool) @@ -769,7 +769,7 @@ def isin(self, values) -> npt.NDArray[np.bool_]: except ValueError: return isin(self.astype(object), values) - if self.dtype.kind in ["m", "M"]: + if self.dtype.kind in "mM": self = cast("DatetimeArray | TimedeltaArray", self) values = values.as_unit(self.unit) @@ -1194,7 +1194,7 @@ def _sub_nat(self): # For period dtype, timedelta64 is a close-enough return dtype. result = np.empty(self.shape, dtype=np.int64) result.fill(iNaT) - if self.dtype.kind in ["m", "M"]: + if self.dtype.kind in "mM": # We can retain unit in dtype self = cast("DatetimeArray| TimedeltaArray", self) return result.view(f"timedelta64[{self.unit}]") diff --git a/pandas/core/arrays/masked.py b/pandas/core/arrays/masked.py index f43bb08cbeffe..eb7dbc9756191 100644 --- a/pandas/core/arrays/masked.py +++ b/pandas/core/arrays/masked.py @@ -694,7 +694,7 @@ def _arith_method(self, other, op): else: # Make sure we do this before the "pow" mask checks # to get an expected exception message on shape mismatch. - if self.dtype.kind in ["i", "u"] and op_name in ["floordiv", "mod"]: + if self.dtype.kind in "iu" and op_name in ["floordiv", "mod"]: # TODO(GH#30188) ATM we don't match the behavior of non-masked # types with respect to floordiv-by-zero pd_op = op diff --git a/pandas/core/arrays/numeric.py b/pandas/core/arrays/numeric.py index b4b665d5264a7..8d629b88edd26 100644 --- a/pandas/core/arrays/numeric.py +++ b/pandas/core/arrays/numeric.py @@ -80,7 +80,7 @@ def __from_arrow__( # test_from_arrow_type_error raise for string, but allow # through itemsize conversion GH#31896 rt_dtype = pandas_dtype(array.type.to_pandas_dtype()) - if rt_dtype.kind not in ["i", "u", "f"]: + if rt_dtype.kind not in "iuf": # Could allow "c" or potentially disallow float<->int conversion, # but at the moment we specifically test that uint<->int works raise TypeError( diff --git a/pandas/core/arrays/numpy_.py b/pandas/core/arrays/numpy_.py index c0a7da8eece5f..ab2c3222e65f7 100644 --- a/pandas/core/arrays/numpy_.py +++ b/pandas/core/arrays/numpy_.py @@ -217,7 +217,7 @@ def _validate_scalar(self, fill_value): return fill_value def _values_for_factorize(self) -> tuple[np.ndarray, float | None]: - if self.dtype.kind in ["i", "u", "b"]: + if self.dtype.kind in "iub": fv = None else: fv = np.nan diff --git a/pandas/core/arrays/sparse/array.py b/pandas/core/arrays/sparse/array.py index 247d13fb86965..f78486fa7d84f 100644 --- a/pandas/core/arrays/sparse/array.py +++ b/pandas/core/arrays/sparse/array.py @@ -228,7 +228,7 @@ def _sparse_array_op( if ( name in ["floordiv", "mod"] and (right == 0).any() - and left.dtype.kind in ["i", "u"] + and left.dtype.kind in "iu" ): # Match the non-Sparse Series behavior opname = f"sparse_{name}_float64" diff --git a/pandas/core/construction.py b/pandas/core/construction.py index bc71804221ddf..2208ae07fe30f 100644 --- a/pandas/core/construction.py +++ b/pandas/core/construction.py @@ -756,7 +756,7 @@ def _try_cast( return ensure_wrapped_if_datetimelike(arr).astype(dtype, copy=copy) elif dtype.kind == "U": - # TODO: test cases with arr.dtype.kind in ["m", "M"] + # TODO: test cases with arr.dtype.kind in "mM" if is_ndarray: arr = cast(np.ndarray, arr) shape = arr.shape @@ -768,7 +768,7 @@ def _try_cast( shape ) - elif dtype.kind in ["m", "M"]: + elif dtype.kind in "mM": return maybe_cast_to_datetime(arr, dtype) # GH#15832: Check if we are requesting a numeric dtype and diff --git a/pandas/core/dtypes/astype.py b/pandas/core/dtypes/astype.py index 09e338d205bbc..f865968328286 100644 --- a/pandas/core/dtypes/astype.py +++ b/pandas/core/dtypes/astype.py @@ -86,7 +86,7 @@ def _astype_nansafe( elif not isinstance(dtype, np.dtype): # pragma: no cover raise ValueError("dtype must be np.dtype or ExtensionDtype") - if arr.dtype.kind in ["m", "M"]: + if arr.dtype.kind in "mM": from pandas.core.construction import ensure_wrapped_if_datetimelike arr = ensure_wrapped_if_datetimelike(arr) diff --git a/pandas/core/dtypes/cast.py b/pandas/core/dtypes/cast.py index b758c6cfc3c13..f1945a2eb32ab 100644 --- a/pandas/core/dtypes/cast.py +++ b/pandas/core/dtypes/cast.py @@ -210,7 +210,7 @@ def _maybe_unbox_datetimelike(value: Scalar, dtype: DtypeObj) -> Scalar: Notes ----- - Caller is responsible for checking dtype.kind in ["m", "M"] + Caller is responsible for checking dtype.kind in "mM" """ if is_valid_na_for_dtype(value, dtype): # 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 # a datetimelike # GH12821, iNaT is cast to float - if dtype.kind in ["M", "m"] and result.dtype.kind in ["i", "f"]: + if dtype.kind in "mM" and result.dtype.kind in "if": result = result.astype(dtype) elif dtype.kind == "m" and result.dtype == _dtype_obj: @@ -544,7 +544,7 @@ def ensure_dtype_can_hold_na(dtype: DtypeObj) -> DtypeObj: return _dtype_obj elif dtype.kind == "b": return _dtype_obj - elif dtype.kind in ["i", "u"]: + elif dtype.kind in "iu": return np.dtype(np.float64) return dtype @@ -623,8 +623,7 @@ def _maybe_promote(dtype: np.dtype, fill_value=np.nan): dtype = _dtype_obj return dtype, fill_value - kinds = ["i", "u", "f", "c", "m", "M"] - if is_valid_na_for_dtype(fill_value, dtype) and dtype.kind in kinds: + if is_valid_na_for_dtype(fill_value, dtype) and dtype.kind in "iufcmM": dtype = ensure_dtype_can_hold_na(dtype) fv = na_value_for_dtype(dtype) return dtype, fv @@ -1196,7 +1195,7 @@ def maybe_cast_to_datetime( from pandas.core.arrays.datetimes import DatetimeArray from pandas.core.arrays.timedeltas import TimedeltaArray - assert dtype.kind in ["m", "M"] + assert dtype.kind in "mM" if not is_list_like(value): raise TypeError("value must be listlike") @@ -1251,7 +1250,7 @@ def _ensure_nanosecond_dtype(dtype: DtypeObj) -> None: # i.e. datetime64tz pass - elif dtype.kind in ["m", "M"]: + elif dtype.kind in "mM": reso = get_unit_from_dtype(dtype) if not is_supported_unit(reso): # 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: if ( isinstance(left, np.ndarray) - and left.dtype.kind in ["i", "u", "c"] + and left.dtype.kind in "iuc" and (lib.is_integer(right) or lib.is_float(right)) ): # e.g. with int8 dtype and right=512, we want to end up with @@ -1335,7 +1334,7 @@ def common_dtype_categorical_compat( # GH#38240 # TODO: more generally, could do `not can_hold_na(dtype)` - if isinstance(dtype, np.dtype) and dtype.kind in ["i", "u"]: + if isinstance(dtype, np.dtype) and dtype.kind in "iu": for obj in objs: # We don't want to accientally allow e.g. "categorical" str here obj_dtype = getattr(obj, "dtype", None) @@ -1429,7 +1428,7 @@ def construct_2d_arraylike_from_scalar( ) -> np.ndarray: shape = (length, width) - if dtype.kind in ["m", "M"]: + if dtype.kind in "mM": value = _maybe_box_and_unbox_datetimelike(value, dtype) elif dtype == _dtype_obj: if isinstance(value, (np.timedelta64, np.datetime64)): @@ -1486,13 +1485,13 @@ def construct_1d_arraylike_from_scalar( if length and is_integer_dtype(dtype) and isna(value): # coerce if we have nan for an integer dtype dtype = np.dtype("float64") - elif isinstance(dtype, np.dtype) and dtype.kind in ("U", "S"): + elif isinstance(dtype, np.dtype) and dtype.kind in "US": # we need to coerce to object dtype to avoid # to allow numpy to take our string as a scalar value dtype = np.dtype("object") if not isna(value): value = ensure_str(value) - elif dtype.kind in ["M", "m"]: + elif dtype.kind in "mM": value = _maybe_box_and_unbox_datetimelike(value, dtype) subarr = np.empty(length, dtype=dtype) @@ -1504,7 +1503,7 @@ def construct_1d_arraylike_from_scalar( def _maybe_box_and_unbox_datetimelike(value: Scalar, dtype: DtypeObj): - # Caller is responsible for checking dtype.kind in ["m", "M"] + # Caller is responsible for checking dtype.kind in "mM" if isinstance(value, dt.datetime): # 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 f"To cast anyway, use pd.Series(values).astype({dtype})" ) - if arr.dtype.kind in ["m", "M"]: + if arr.dtype.kind in "mM": # test_constructor_maskedarray_nonfloat raise TypeError( f"Constructing a Series or DataFrame from {arr.dtype} values and " @@ -1667,7 +1666,7 @@ def can_hold_element(arr: ArrayLike, element: Any) -> bool: bool """ dtype = arr.dtype - if not isinstance(dtype, np.dtype) or dtype.kind in ["m", "M"]: + if not isinstance(dtype, np.dtype) or dtype.kind in "mM": if isinstance(dtype, (PeriodDtype, IntervalDtype, DatetimeTZDtype, np.dtype)): # np.dtype here catches datetime64ns and timedelta64ns; we assume # in this case that we have DatetimeArray/TimedeltaArray @@ -1715,7 +1714,7 @@ def np_can_hold_element(dtype: np.dtype, element: Any) -> Any: tipo = _maybe_infer_dtype_type(element) - if dtype.kind in ["i", "u"]: + if dtype.kind in "iu": if isinstance(element, range): if _dtype_can_hold_range(element, dtype): return element @@ -1731,7 +1730,7 @@ def np_can_hold_element(dtype: np.dtype, element: Any) -> Any: raise LossySetitemError if tipo is not None: - if tipo.kind not in ["i", "u"]: + if tipo.kind not in "iu": if isinstance(element, np.ndarray) and element.dtype.kind == "f": # If all can be losslessly cast to integers, then we can hold them with np.errstate(invalid="ignore"): @@ -1783,7 +1782,7 @@ def np_can_hold_element(dtype: np.dtype, element: Any) -> Any: if tipo is not None: # TODO: itemsize check? - if tipo.kind not in ["f", "i", "u"]: + if tipo.kind not in "iuf": # Anything other than float/integer we cannot hold raise LossySetitemError if not isinstance(tipo, np.dtype): @@ -1819,7 +1818,7 @@ def np_can_hold_element(dtype: np.dtype, element: Any) -> Any: raise LossySetitemError if tipo is not None: - if tipo.kind in ["c", "f", "i", "u"]: + if tipo.kind in "iufc": return element raise LossySetitemError raise LossySetitemError diff --git a/pandas/core/dtypes/common.py b/pandas/core/dtypes/common.py index 791208cab302b..bebd333871b9b 100644 --- a/pandas/core/dtypes/common.py +++ b/pandas/core/dtypes/common.py @@ -1402,7 +1402,7 @@ def is_ea_or_datetimelike_dtype(dtype: DtypeObj | None) -> bool: Checks only for dtype objects, not dtype-castable strings or types. """ return isinstance(dtype, ExtensionDtype) or ( - isinstance(dtype, np.dtype) and dtype.kind in ["m", "M"] + isinstance(dtype, np.dtype) and dtype.kind in "mM" ) diff --git a/pandas/core/dtypes/concat.py b/pandas/core/dtypes/concat.py index f80fb0320a209..087b1488010bf 100644 --- a/pandas/core/dtypes/concat.py +++ b/pandas/core/dtypes/concat.py @@ -155,7 +155,7 @@ def concat_compat( # "Sequence[Union[ExtensionArray, ndarray[Any, Any]]]"; expected # "Union[_SupportsArray[dtype[Any]], _NestedSequence[_SupportsArray[dtype[Any]]]]" result: np.ndarray = np.concatenate(to_concat, axis=axis) # type: ignore[arg-type] - if "b" in kinds and result.dtype.kind in ["i", "u", "f"]: + if "b" in kinds and result.dtype.kind in "iuf": # GH#39817 cast to object instead of casting bools to numeric result = result.astype(object, copy=False) return result diff --git a/pandas/core/dtypes/dtypes.py b/pandas/core/dtypes/dtypes.py index 620266602442f..ac52e43472cbe 100644 --- a/pandas/core/dtypes/dtypes.py +++ b/pandas/core/dtypes/dtypes.py @@ -1138,7 +1138,7 @@ def _can_hold_na(self) -> bool: raise NotImplementedError( "_can_hold_na is not defined for partially-initialized IntervalDtype" ) - if subtype.kind in ["i", "u"]: + if subtype.kind in "iu": return False return True @@ -1437,7 +1437,7 @@ def from_numpy_dtype(cls, dtype: np.dtype) -> BaseMaskedDtype: from pandas.core.arrays.boolean import BooleanDtype return BooleanDtype() - elif dtype.kind in ["i", "u"]: + elif dtype.kind in "iu": from pandas.core.arrays.integer import INT_STR_TO_DTYPE return INT_STR_TO_DTYPE[dtype.name] diff --git a/pandas/core/dtypes/missing.py b/pandas/core/dtypes/missing.py index 3978e5bf13fbe..718404f0799e4 100644 --- a/pandas/core/dtypes/missing.py +++ b/pandas/core/dtypes/missing.py @@ -500,7 +500,7 @@ def array_equivalent( if dtype_equal: # fastpath when we require that the dtypes match (Block.equals) - if left.dtype.kind in ["f", "c"]: + if left.dtype.kind in "fc": return _array_equivalent_float(left, right) elif needs_i8_conversion(left.dtype): return _array_equivalent_datetimelike(left, right) @@ -698,7 +698,7 @@ def is_valid_na_for_dtype(obj, dtype: DtypeObj) -> bool: return not isinstance(obj, (np.timedelta64, np.datetime64, Decimal)) elif dtype.kind == "m": return not isinstance(obj, (np.datetime64, Decimal)) - elif dtype.kind in ["i", "u", "f", "c"]: + elif dtype.kind in "iufc": # Numeric return obj is not NaT and not isinstance(obj, (np.datetime64, np.timedelta64)) elif dtype.kind == "b": @@ -741,7 +741,7 @@ def isna_all(arr: ArrayLike) -> bool: if dtype.kind == "f" and isinstance(dtype, np.dtype): checker = nan_checker - elif (isinstance(dtype, np.dtype) and dtype.kind in ["m", "M"]) or isinstance( + elif (isinstance(dtype, np.dtype) and dtype.kind in "mM") or isinstance( dtype, (DatetimeTZDtype, PeriodDtype) ): # error: Incompatible types in assignment (expression has type diff --git a/pandas/core/groupby/grouper.py b/pandas/core/groupby/grouper.py index a6d5b59d7c5d0..8879a46e9bbbf 100644 --- a/pandas/core/groupby/grouper.py +++ b/pandas/core/groupby/grouper.py @@ -612,7 +612,7 @@ def __init__( raise AssertionError(errmsg) if isinstance(grouping_vector, np.ndarray): - if grouping_vector.dtype.kind in ["m", "M"]: + if grouping_vector.dtype.kind in "mM": # if we have a date/time-like grouper, make sure that we have # Timestamps like # TODO 2022-10-08 we only have one test that gets here and diff --git a/pandas/core/groupby/ops.py b/pandas/core/groupby/ops.py index 55155ea41f430..cf4c48dc34200 100644 --- a/pandas/core/groupby/ops.py +++ b/pandas/core/groupby/ops.py @@ -220,7 +220,7 @@ def _get_cython_vals(self, values: np.ndarray) -> np.ndarray: # should raise in _get_cython_function values = ensure_float64(values) - elif values.dtype.kind in ["i", "u"]: + elif values.dtype.kind in "iu": if how in ["var", "mean"] or ( self.kind == "transform" and self.has_dropped_na ): diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index 572bd2bcdf814..b57a8f2afee32 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -505,7 +505,7 @@ def __new__( if isinstance(data, ABCMultiIndex): data = data._values - if data.dtype.kind not in ["i", "u", "f", "b", "c", "m", "M"]: + if data.dtype.kind not in "iufcbmM": # GH#11836 we need to avoid having numpy coerce # things that look like ints/floats to ints unless # they are actually ints, e.g. '0' and 0.0 @@ -673,7 +673,7 @@ def _with_infer(cls, *args, **kwargs): # "Union[ExtensionArray, ndarray[Any, Any]]"; expected # "ndarray[Any, Any]" values = lib.maybe_convert_objects(result._values) # type: ignore[arg-type] - if values.dtype.kind in ["i", "u", "f", "b"]: + if values.dtype.kind in "iufb": return Index(values, name=result.name) return result @@ -2119,7 +2119,7 @@ def _can_hold_na(self) -> bool: # in IntervalArray._cmp_method return True return self.dtype._can_hold_na - if self.dtype.kind in ["i", "u", "b"]: + if self.dtype.kind in "iub": return False return True @@ -2685,7 +2685,7 @@ def _na_value(self): """The expected NA value to use with this index.""" dtype = self.dtype if isinstance(dtype, np.dtype): - if dtype.kind in ["m", "M"]: + if dtype.kind in "mM": return NaT return np.nan return dtype.na_value @@ -5098,7 +5098,7 @@ def _validate_fill_value(self, value): If the value cannot be inserted into an array of this dtype. """ dtype = self.dtype - if isinstance(dtype, np.dtype) and dtype.kind not in ["m", "M"]: + if isinstance(dtype, np.dtype) and dtype.kind not in "mM": # return np_can_hold_element(dtype, value) try: return np_can_hold_element(dtype, value) @@ -5900,7 +5900,7 @@ def _get_indexer_strict(self, key, axis_name: str_t) -> tuple[Index, np.ndarray] if isinstance(key, Index): # GH 42790 - Preserve name from an Index keyarr.name = key.name - if keyarr.dtype.kind in ["m", "M"]: + if keyarr.dtype.kind in "mM": # DTI/TDI.take can infer a freq in some cases when we dont want one if isinstance(key, list) or ( isinstance(key, type(self)) diff --git a/pandas/core/indexes/datetimelike.py b/pandas/core/indexes/datetimelike.py index cd83d076028c0..c718f83d7b046 100644 --- a/pandas/core/indexes/datetimelike.py +++ b/pandas/core/indexes/datetimelike.py @@ -132,7 +132,7 @@ def equals(self, other: Any) -> bool: if not isinstance(other, Index): return False - elif other.dtype.kind in ["f", "i", "u", "c"]: + elif other.dtype.kind in "iufc": return False elif not isinstance(other, type(self)): should_try = False diff --git a/pandas/core/indexes/interval.py b/pandas/core/indexes/interval.py index 19bf24ac3746d..b9902572c5846 100644 --- a/pandas/core/indexes/interval.py +++ b/pandas/core/indexes/interval.py @@ -805,7 +805,7 @@ def _should_fallback_to_positional(self) -> bool: # positional in this case # error: Item "ExtensionDtype"/"dtype[Any]" of "Union[dtype[Any], # ExtensionDtype]" has no attribute "subtype" - return self.dtype.subtype.kind in ["m", "M"] # type: ignore[union-attr] + return self.dtype.subtype.kind in "mM" # type: ignore[union-attr] def _maybe_cast_slice_bound(self, label, side: str): return getattr(self, side)._maybe_cast_slice_bound(label, side) diff --git a/pandas/core/internals/array_manager.py b/pandas/core/internals/array_manager.py index a692ad6afe92c..5bbce86e68927 100644 --- a/pandas/core/internals/array_manager.py +++ b/pandas/core/internals/array_manager.py @@ -1227,7 +1227,7 @@ def array_values(self): @property def _can_hold_na(self) -> bool: if isinstance(self.array, np.ndarray): - return self.array.dtype.kind not in ["b", "i", "u"] + return self.array.dtype.kind not in "iub" else: # ExtensionArray return self.array._can_hold_na diff --git a/pandas/core/internals/blocks.py b/pandas/core/internals/blocks.py index 6dcb73f6793ad..719984883c6df 100644 --- a/pandas/core/internals/blocks.py +++ b/pandas/core/internals/blocks.py @@ -173,7 +173,7 @@ def _can_hold_na(self) -> bool: """ dtype = self.dtype if isinstance(dtype, np.dtype): - return dtype.kind not in ["b", "i", "u"] + return dtype.kind not in "iub" return dtype._can_hold_na @final diff --git a/pandas/core/internals/concat.py b/pandas/core/internals/concat.py index bfdb56147a807..fda445af5c0c4 100644 --- a/pandas/core/internals/concat.py +++ b/pandas/core/internals/concat.py @@ -143,7 +143,7 @@ def concat_arrays(to_concat: list) -> ArrayLike: if single_dtype: target_dtype = to_concat_no_proxy[0].dtype - elif all(x.kind in ["i", "u", "b"] and isinstance(x, np.dtype) for x in dtypes): + elif all(x.kind in "iub" and isinstance(x, np.dtype) for x in dtypes): # GH#42092 target_dtype = np.find_common_type(list(dtypes), []) else: @@ -626,14 +626,14 @@ def _dtype_to_na_value(dtype: DtypeObj, has_none_blocks: bool): """ if isinstance(dtype, ExtensionDtype): return dtype.na_value - elif dtype.kind in ["m", "M"]: + elif dtype.kind in "mM": return dtype.type("NaT") - elif dtype.kind in ["f", "c"]: + elif dtype.kind in "fc": return dtype.type("NaN") elif dtype.kind == "b": # different from missing.na_value_for_dtype return None - elif dtype.kind in ["i", "u"]: + elif dtype.kind in "iu": if not has_none_blocks: # different from missing.na_value_for_dtype return None @@ -692,7 +692,7 @@ def _is_uniform_join_units(join_units: list[JoinUnit]) -> bool: is_dtype_equal(ju.block.dtype, first.dtype) # GH#42092 we only want the dtype_equal check for non-numeric blocks # (for now, may change but that would need a deprecation) - or ju.block.dtype.kind in ["b", "i", "u"] + or ju.block.dtype.kind in "iub" for ju in join_units ) and diff --git a/pandas/core/internals/construction.py b/pandas/core/internals/construction.py index 5fa7901cf85e9..1108e6051d20b 100644 --- a/pandas/core/internals/construction.py +++ b/pandas/core/internals/construction.py @@ -1042,11 +1042,11 @@ def convert(arr): # core.construction functions cls = dtype.construct_array_type() arr = cls._from_sequence(arr, dtype=dtype, copy=False) - elif dtype.kind in ["m", "M"]: + elif dtype.kind in "mM": # This restriction is harmless bc these are the only cases # where maybe_cast_to_datetime is not a no-op. # Here we know: - # 1) dtype.kind in ["m", "M"] and + # 1) dtype.kind in "mM" and # 2) arr is either object or numeric dtype arr = maybe_cast_to_datetime(arr, dtype) diff --git a/pandas/core/internals/managers.py b/pandas/core/internals/managers.py index ed1a9b193b3e4..e740bd1fdf4ba 100644 --- a/pandas/core/internals/managers.py +++ b/pandas/core/internals/managers.py @@ -2216,7 +2216,7 @@ def _form_blocks(arrays: list[ArrayLike], consolidate: bool, refs: list) -> list block_type = get_block_type(dtype) if isinstance(dtype, np.dtype): - is_dtlike = dtype.kind in ["m", "M"] + is_dtlike = dtype.kind in "mM" if issubclass(dtype.type, (str, bytes)): dtype = np.dtype(object) diff --git a/pandas/core/nanops.py b/pandas/core/nanops.py index bf29dd0786ad9..34726feed25a1 100644 --- a/pandas/core/nanops.py +++ b/pandas/core/nanops.py @@ -401,7 +401,7 @@ def new_func( ): orig_values = values - datetimelike = values.dtype.kind in ["m", "M"] + datetimelike = values.dtype.kind in "mM" if datetimelike and mask is None: mask = isna(values) @@ -1739,7 +1739,7 @@ def na_accum_func(values: ArrayLike, accum_func, *, skipna: bool) -> ArrayLike: }[accum_func] # This should go through ea interface - assert values.dtype.kind not in ["m", "M"] + assert values.dtype.kind not in "mM" # We will be applying this function to block values if skipna and not issubclass(values.dtype.type, (np.integer, np.bool_)): diff --git a/pandas/core/ops/array_ops.py b/pandas/core/ops/array_ops.py index 13eb526cff209..5032db0cc2f48 100644 --- a/pandas/core/ops/array_ops.py +++ b/pandas/core/ops/array_ops.py @@ -402,7 +402,7 @@ def logical_op(left: ArrayLike, right: Any, op) -> ArrayLike: def fill_bool(x, left=None): # if `left` is specifically not-boolean, we do not cast to bool - if x.dtype.kind in ["c", "f", "O"]: + if x.dtype.kind in "cfO": # dtypes that can hold NA mask = isna(x) if mask.any(): diff --git a/pandas/core/reshape/merge.py b/pandas/core/reshape/merge.py index c11315527b3b5..bfaf403491801 100644 --- a/pandas/core/reshape/merge.py +++ b/pandas/core/reshape/merge.py @@ -2107,7 +2107,7 @@ def injection(obj): # TODO: we have no test cases with PeriodDtype here; probably # need to adjust tolerance for that case. - if left_values.dtype.kind in ["m", "M"]: + if left_values.dtype.kind in "mM": # Make sure the i8 representation for tolerance # matches that for left_values/right_values. lvs = ensure_wrapped_if_datetimelike(left_values) diff --git a/pandas/core/tools/datetimes.py b/pandas/core/tools/datetimes.py index 2f0a9f9d23364..ac0a014a3ccf6 100644 --- a/pandas/core/tools/datetimes.py +++ b/pandas/core/tools/datetimes.py @@ -502,7 +502,7 @@ def _to_datetime_with_unit(arg, unit, name, utc: bool, errors: str) -> Index: else: arg = np.asarray(arg) - if arg.dtype.kind in ["i", "u"]: + if arg.dtype.kind in "iu": # Note we can't do "f" here because that could induce unwanted # rounding GH#14156, GH#20445 arr = arg.astype(f"datetime64[{unit}]", copy=False) diff --git a/pandas/io/pytables.py b/pandas/io/pytables.py index 90bd6a03b9a34..e579166c002d3 100644 --- a/pandas/io/pytables.py +++ b/pandas/io/pytables.py @@ -5165,7 +5165,7 @@ def _get_data_and_dtype_name(data: ArrayLike): # For datetime64tz we need to drop the TZ in tests TODO: why? dtype_name = data.dtype.name.split("[")[0] - if data.dtype.kind in ["m", "M"]: + if data.dtype.kind in "mM": data = np.asarray(data.view("i8")) # TODO: we used to reshape for the dt64tz case, but no longer # doing that doesn't seem to break anything. why?