diff --git a/pandas/_libs/parsers.pyx b/pandas/_libs/parsers.pyx index 19a121253e29a..ae1d20ca4e225 100644 --- a/pandas/_libs/parsers.pyx +++ b/pandas/_libs/parsers.pyx @@ -10,7 +10,6 @@ import sys import time import warnings -from pandas.errors import ParserError from pandas.util._exceptions import find_stack_level from pandas import StringDtype @@ -106,15 +105,10 @@ from pandas.errors import ( ParserWarning, ) -from pandas.core.dtypes.common import ( - is_bool_dtype, - is_datetime64_dtype, - is_extension_array_dtype, - is_float_dtype, - is_integer_dtype, - is_object_dtype, +from pandas.core.dtypes.dtypes import ( + CategoricalDtype, + ExtensionDtype, ) -from pandas.core.dtypes.dtypes import CategoricalDtype from pandas.core.dtypes.inference import is_dict_like cdef: @@ -1077,7 +1071,7 @@ cdef class TextReader: # don't try to upcast EAs if ( - na_count > 0 and not is_extension_array_dtype(col_dtype) + na_count > 0 and not isinstance(col_dtype, ExtensionDtype) or self.dtype_backend != "numpy" ): use_dtype_backend = self.dtype_backend != "numpy" and col_dtype is None @@ -1142,14 +1136,14 @@ cdef class TextReader: # (see _try_bool_flex()). Usually this would be taken care of using # _maybe_upcast(), but if col_dtype is a floating type we should just # take care of that cast here. - if col_res.dtype == np.bool_ and is_float_dtype(col_dtype): + if col_res.dtype == np.bool_ and col_dtype.kind == "f": mask = col_res.view(np.uint8) == na_values[np.uint8] col_res = col_res.astype(col_dtype) np.putmask(col_res, mask, np.nan) return col_res, na_count # NaNs are already cast to True here, so can not use astype - if col_res.dtype == np.bool_ and is_integer_dtype(col_dtype): + if col_res.dtype == np.bool_ and col_dtype.kind in "iu": if na_count > 0: raise ValueError( f"cannot safely convert passed user dtype of " @@ -1193,14 +1187,14 @@ cdef class TextReader: cats, codes, dtype, true_values=true_values) return cat, na_count - elif is_extension_array_dtype(dtype): + elif isinstance(dtype, ExtensionDtype): result, na_count = self._string_convert(i, start, end, na_filter, na_hashset) array_type = dtype.construct_array_type() try: # use _from_sequence_of_strings if the class defines it - if is_bool_dtype(dtype): + if dtype.kind == "b": true_values = [x.decode() for x in self.true_values] false_values = [x.decode() for x in self.false_values] result = array_type._from_sequence_of_strings( @@ -1216,7 +1210,7 @@ cdef class TextReader: return result, na_count - elif is_integer_dtype(dtype): + elif dtype.kind in "iu": try: result, na_count = _try_int64(self.parser, i, start, end, na_filter, na_hashset) @@ -1233,14 +1227,14 @@ cdef class TextReader: return result, na_count - elif is_float_dtype(dtype): + elif dtype.kind == "f": result, na_count = _try_double(self.parser, i, start, end, na_filter, na_hashset, na_flist) if result is not None and dtype != "float64": result = result.astype(dtype) return result, na_count - elif is_bool_dtype(dtype): + elif dtype.kind == "b": result, na_count = _try_bool_flex(self.parser, i, start, end, na_filter, na_hashset, self.true_set, self.false_set) @@ -1267,10 +1261,10 @@ cdef class TextReader: # unicode variable width return self._string_convert(i, start, end, na_filter, na_hashset) - elif is_object_dtype(dtype): + elif dtype == object: return self._string_convert(i, start, end, na_filter, na_hashset) - elif is_datetime64_dtype(dtype): + elif dtype.kind == "M": raise TypeError(f"the dtype {dtype} is not supported " f"for parsing, pass this column " f"using parse_dates instead") @@ -1438,7 +1432,7 @@ def _maybe_upcast( ------- The casted array. """ - if is_extension_array_dtype(arr.dtype): + if isinstance(arr.dtype, ExtensionDtype): # TODO: the docstring says arr is an ndarray, in which case this cannot # be reached. Is that incorrect? return arr diff --git a/pandas/_testing/asserters.py b/pandas/_testing/asserters.py index e25e8388bc4cd..2c0d75bcf2250 100644 --- a/pandas/_testing/asserters.py +++ b/pandas/_testing/asserters.py @@ -13,10 +13,8 @@ from pandas.core.dtypes.common import ( is_bool, - is_categorical_dtype, is_extension_array_dtype, is_integer_dtype, - is_interval_dtype, is_number, is_numeric_dtype, needs_i8_conversion, @@ -33,6 +31,7 @@ DataFrame, DatetimeIndex, Index, + IntervalDtype, IntervalIndex, MultiIndex, PeriodIndex, @@ -238,7 +237,9 @@ def _check_types(left, right, obj: str = "Index") -> None: assert_attr_equal("inferred_type", left, right, obj=obj) # Skip exact dtype checking when `check_categorical` is False - if is_categorical_dtype(left.dtype) and is_categorical_dtype(right.dtype): + if isinstance(left.dtype, CategoricalDtype) and isinstance( + right.dtype, CategoricalDtype + ): if check_categorical: assert_attr_equal("dtype", left, right, obj=obj) assert_index_equal(left.categories, right.categories, exact=exact) @@ -335,7 +336,9 @@ def _get_ilevel_values(index, level): assert_interval_array_equal(left._values, right._values) if check_categorical: - if is_categorical_dtype(left.dtype) or is_categorical_dtype(right.dtype): + if isinstance(left.dtype, CategoricalDtype) or isinstance( + right.dtype, CategoricalDtype + ): assert_categorical_equal(left._values, right._values, obj=f"{obj} category") @@ -946,7 +949,9 @@ def assert_series_equal( f"is not equal to {right._values}." ) raise AssertionError(msg) - elif is_interval_dtype(left.dtype) and is_interval_dtype(right.dtype): + elif isinstance(left.dtype, IntervalDtype) and isinstance( + right.dtype, IntervalDtype + ): assert_interval_array_equal(left.array, right.array) elif isinstance(left.dtype, CategoricalDtype) or isinstance( right.dtype, CategoricalDtype diff --git a/pandas/core/algorithms.py b/pandas/core/algorithms.py index db45f140c268e..5cddf3c2c865b 100644 --- a/pandas/core/algorithms.py +++ b/pandas/core/algorithms.py @@ -42,7 +42,6 @@ ensure_platform_int, is_array_like, is_bool_dtype, - is_categorical_dtype, is_complex_dtype, is_dict_like, is_extension_array_dtype, @@ -59,6 +58,7 @@ from pandas.core.dtypes.concat import concat_compat from pandas.core.dtypes.dtypes import ( BaseMaskedDtype, + CategoricalDtype, ExtensionDtype, PandasDtype, ) @@ -141,7 +141,7 @@ def _ensure_data(values: ArrayLike) -> np.ndarray: return _ensure_data(values._data) return np.asarray(values) - elif is_categorical_dtype(values.dtype): + elif isinstance(values.dtype, CategoricalDtype): # NB: cases that go through here should NOT be using _reconstruct_data # on the back-end. values = cast("Categorical", values) @@ -417,7 +417,7 @@ def unique_with_mask(values, mask: npt.NDArray[np.bool_] | None = None): """See algorithms.unique for docs. Takes a mask for masked arrays.""" values = _ensure_arraylike(values) - if is_extension_array_dtype(values.dtype): + if isinstance(values.dtype, ExtensionDtype): # Dispatch to extension dtype's unique. return values.unique() @@ -1534,7 +1534,7 @@ def safe_sort( ordered: AnyArrayLike if ( - not is_extension_array_dtype(values) + not isinstance(values.dtype, ExtensionDtype) and lib.infer_dtype(values, skipna=False) == "mixed-integer" ): ordered = _sort_mixed(values) diff --git a/pandas/core/arrays/datetimelike.py b/pandas/core/arrays/datetimelike.py index 3eb7159399bb3..334400cc13201 100644 --- a/pandas/core/arrays/datetimelike.py +++ b/pandas/core/arrays/datetimelike.py @@ -83,10 +83,8 @@ from pandas.core.dtypes.common import ( is_all_strings, - is_categorical_dtype, is_datetime64_any_dtype, is_datetime64_dtype, - is_datetime64tz_dtype, is_datetime_or_timedelta_dtype, is_dtype_equal, is_float_dtype, @@ -99,8 +97,10 @@ pandas_dtype, ) from pandas.core.dtypes.dtypes import ( + CategoricalDtype, DatetimeTZDtype, ExtensionDtype, + PeriodDtype, ) from pandas.core.dtypes.generic import ( ABCCategorical, @@ -167,7 +167,7 @@ def _period_dispatch(meth: F) -> F: @wraps(meth) def new_meth(self, *args, **kwargs): - if not is_period_dtype(self.dtype): + if not isinstance(self.dtype, PeriodDtype): return meth(self, *args, **kwargs) arr = self.view("M8[ns]") @@ -377,7 +377,7 @@ def _get_getitem_freq(self, key) -> BaseOffset | None: """ Find the `freq` attribute to assign to the result of a __getitem__ lookup. """ - is_period = is_period_dtype(self.dtype) + is_period = isinstance(self.dtype, PeriodDtype) if is_period: freq = self.freq elif self.ndim != 1: @@ -437,7 +437,7 @@ def astype(self, dtype, copy: bool = True): # 3. DatetimeArray.astype handles datetime -> period dtype = pandas_dtype(dtype) - if is_object_dtype(dtype): + if dtype == object: if self.dtype.kind == "M": self = cast("DatetimeArray", self) # *much* faster than self._box_values @@ -521,7 +521,7 @@ def _concat_same_type( dtype = obj.dtype new_freq = None - if is_period_dtype(dtype): + if isinstance(dtype, PeriodDtype): new_freq = obj.freq elif axis == 0: # GH 3232: If the concat result is evenly spaced, we can retain the @@ -703,7 +703,7 @@ def _validate_listlike(self, value, allow_object: bool = False): except ValueError: pass - if is_categorical_dtype(value.dtype): + if isinstance(value.dtype, CategoricalDtype): # e.g. we have a Categorical holding self.dtype if is_dtype_equal(value.categories.dtype, self.dtype): # TODO: do we need equal dtype or just comparable? @@ -951,7 +951,7 @@ def _cmp_method(self, other, op): result = np.zeros(self.shape, dtype=bool) return result - if not is_period_dtype(self.dtype): + if not isinstance(self.dtype, PeriodDtype): self = cast(TimelikeOps, self) if self._creso != other._creso: if not isinstance(other, type(self)): @@ -1022,7 +1022,7 @@ def _get_arithmetic_result_freq(self, other) -> BaseOffset | None: """ # Adding or subtracting a Timedelta/Timestamp scalar is freq-preserving # whenever self.freq is a Tick - if is_period_dtype(self.dtype): + if isinstance(self.dtype, PeriodDtype): return self.freq elif not lib.is_scalar(other): return None @@ -1200,7 +1200,7 @@ def _add_nat(self): """ Add pd.NaT to self """ - if is_period_dtype(self.dtype): + if isinstance(self.dtype, PeriodDtype): raise TypeError( f"Cannot add {type(self).__name__} and {type(NaT).__name__}" ) @@ -1237,7 +1237,7 @@ def _sub_nat(self): def _sub_periodlike(self, other: Period | PeriodArray) -> npt.NDArray[np.object_]: # If the operation is well-defined, we return an object-dtype ndarray # of DateOffsets. Null entries are filled with pd.NaT - if not is_period_dtype(self.dtype): + if not isinstance(self.dtype, PeriodDtype): raise TypeError( f"cannot subtract {type(other).__name__} from {type(self).__name__}" ) @@ -1327,7 +1327,7 @@ def __add__(self, other): elif lib.is_integer(other): # This check must come after the check for np.timedelta64 # as is_integer returns True for these - if not is_period_dtype(self.dtype): + if not isinstance(self.dtype, PeriodDtype): raise integer_op_not_supported(self) obj = cast("PeriodArray", self) result = obj._addsub_int_array_or_scalar(other * obj.freq.n, operator.add) @@ -1339,11 +1339,13 @@ def __add__(self, other): elif is_object_dtype(other_dtype): # e.g. Array/Index of DateOffset objects result = self._addsub_object_array(other, operator.add) - elif is_datetime64_dtype(other_dtype) or is_datetime64tz_dtype(other_dtype): + elif is_datetime64_dtype(other_dtype) or isinstance( + other_dtype, DatetimeTZDtype + ): # DatetimeIndex, ndarray[datetime64] return self._add_datetime_arraylike(other) elif is_integer_dtype(other_dtype): - if not is_period_dtype(self.dtype): + if not isinstance(self.dtype, PeriodDtype): raise integer_op_not_supported(self) obj = cast("PeriodArray", self) result = obj._addsub_int_array_or_scalar(other * obj.freq.n, operator.add) @@ -1383,7 +1385,7 @@ def __sub__(self, other): elif lib.is_integer(other): # This check must come after the check for np.timedelta64 # as is_integer returns True for these - if not is_period_dtype(self.dtype): + if not isinstance(self.dtype, PeriodDtype): raise integer_op_not_supported(self) obj = cast("PeriodArray", self) result = obj._addsub_int_array_or_scalar(other * obj.freq.n, operator.sub) @@ -1398,14 +1400,16 @@ def __sub__(self, other): elif is_object_dtype(other_dtype): # e.g. Array/Index of DateOffset objects result = self._addsub_object_array(other, operator.sub) - elif is_datetime64_dtype(other_dtype) or is_datetime64tz_dtype(other_dtype): + elif is_datetime64_dtype(other_dtype) or isinstance( + other_dtype, DatetimeTZDtype + ): # DatetimeIndex, ndarray[datetime64] result = self._sub_datetime_arraylike(other) elif is_period_dtype(other_dtype): # PeriodIndex result = self._sub_periodlike(other) elif is_integer_dtype(other_dtype): - if not is_period_dtype(self.dtype): + if not isinstance(self.dtype, PeriodDtype): raise integer_op_not_supported(self) obj = cast("PeriodArray", self) result = obj._addsub_int_array_or_scalar(other * obj.freq.n, operator.sub) @@ -1444,7 +1448,7 @@ def __rsub__(self, other): raise TypeError( f"cannot subtract {type(self).__name__} from {type(other).__name__}" ) - elif is_period_dtype(self.dtype) and is_timedelta64_dtype(other_dtype): + elif isinstance(self.dtype, PeriodDtype) and is_timedelta64_dtype(other_dtype): # TODO: Can we simplify/generalize these cases at all? raise TypeError(f"cannot subtract {type(self).__name__} from {other.dtype}") elif is_timedelta64_dtype(self.dtype): @@ -1458,7 +1462,7 @@ def __iadd__(self, other) -> Self: result = self + other self[:] = result[:] - if not is_period_dtype(self.dtype): + if not isinstance(self.dtype, PeriodDtype): # restore freq, which is invalidated by setitem self._freq = result.freq return self @@ -1467,7 +1471,7 @@ def __isub__(self, other) -> Self: result = self - other self[:] = result[:] - if not is_period_dtype(self.dtype): + if not isinstance(self.dtype, PeriodDtype): # restore freq, which is invalidated by setitem self._freq = result.freq return self @@ -1543,7 +1547,7 @@ def mean(self, *, skipna: bool = True, axis: AxisInt | None = 0): ----- mean is only defined for Datetime and Timedelta dtypes, not for Period. """ - if is_period_dtype(self.dtype): + if isinstance(self.dtype, PeriodDtype): # See discussion in GH#24757 raise TypeError( f"mean is not implemented for {type(self).__name__} since the " @@ -1987,7 +1991,7 @@ def __array_ufunc__(self, ufunc: np.ufunc, method: str, *inputs, **kwargs): def _round(self, freq, mode, ambiguous, nonexistent): # round the local times - if is_datetime64tz_dtype(self.dtype): + if isinstance(self.dtype, DatetimeTZDtype): # operate on naive timestamps, then convert back to aware self = cast("DatetimeArray", self) naive = self.tz_localize(None) diff --git a/pandas/core/arrays/datetimes.py b/pandas/core/arrays/datetimes.py index 296e8e0784e38..c80ab32db1ea6 100644 --- a/pandas/core/arrays/datetimes.py +++ b/pandas/core/arrays/datetimes.py @@ -52,12 +52,9 @@ is_bool_dtype, is_datetime64_any_dtype, is_datetime64_dtype, - is_datetime64tz_dtype, is_dtype_equal, - is_extension_array_dtype, is_float_dtype, is_object_dtype, - is_period_dtype, is_sparse, is_string_dtype, is_timedelta64_dtype, @@ -66,6 +63,7 @@ from pandas.core.dtypes.dtypes import ( DatetimeTZDtype, ExtensionDtype, + PeriodDtype, ) from pandas.core.dtypes.missing import isna @@ -697,7 +695,7 @@ def astype(self, dtype, copy: bool = True): "Pass e.g. 'datetime64[ns]' instead." ) - elif is_period_dtype(dtype): + elif isinstance(dtype, PeriodDtype): return self.to_period(freq=dtype.freq) return dtl.DatetimeLikeArrayMixin.astype(self, dtype, copy) @@ -734,7 +732,7 @@ def _assert_tzawareness_compat(self, other) -> None: other_tz = getattr(other, "tzinfo", None) other_dtype = getattr(other, "dtype", None) - if is_datetime64tz_dtype(other_dtype): + if isinstance(other_dtype, DatetimeTZDtype): # Get tzinfo from Series dtype other_tz = other.dtype.tz if other is NaT: @@ -2075,7 +2073,7 @@ def _sequence_to_dt64ns( # `data` may have originally been a Categorical[datetime64[ns, tz]], # so we need to handle these types. - if is_datetime64tz_dtype(data_dtype): + if isinstance(data_dtype, DatetimeTZDtype): # DatetimeArray -> ndarray tz = _maybe_infer_tz(tz, data.tz) result = data._ndarray @@ -2242,14 +2240,16 @@ def maybe_convert_dtype(data, copy: bool, tz: tzinfo | None = None): elif is_timedelta64_dtype(data.dtype) or is_bool_dtype(data.dtype): # GH#29794 enforcing deprecation introduced in GH#23539 raise TypeError(f"dtype {data.dtype} cannot be converted to datetime64[ns]") - elif is_period_dtype(data.dtype): + elif isinstance(data.dtype, PeriodDtype): # Note: without explicitly raising here, PeriodIndex # test_setops.test_join_does_not_recur fails raise TypeError( "Passing PeriodDtype data is invalid. Use `data.to_timestamp()` instead" ) - elif is_extension_array_dtype(data.dtype) and not is_datetime64tz_dtype(data.dtype): + elif isinstance(data.dtype, ExtensionDtype) and not isinstance( + data.dtype, DatetimeTZDtype + ): # TODO: We have no tests for these data = np.array(data, dtype=np.object_) copy = False diff --git a/pandas/core/dtypes/missing.py b/pandas/core/dtypes/missing.py index 99c0553998d63..3978e5bf13fbe 100644 --- a/pandas/core/dtypes/missing.py +++ b/pandas/core/dtypes/missing.py @@ -26,7 +26,6 @@ TD64NS_DTYPE, ensure_object, is_bool_dtype, - is_categorical_dtype, is_complex_dtype, is_dtype_equal, is_extension_array_dtype, @@ -283,7 +282,7 @@ def _isna_array(values: ArrayLike, inf_as_na: bool = False): if not isinstance(values, np.ndarray): # i.e. ExtensionArray - if inf_as_na and is_categorical_dtype(dtype): + if inf_as_na and isinstance(dtype, CategoricalDtype): result = libmissing.isnaobj(values.to_numpy(), inf_as_na=inf_as_na) else: # error: Incompatible types in assignment (expression has type diff --git a/pandas/core/indexers/utils.py b/pandas/core/indexers/utils.py index d2f53af8ca1d9..ffd33a39b8d2b 100644 --- a/pandas/core/indexers/utils.py +++ b/pandas/core/indexers/utils.py @@ -13,11 +13,11 @@ from pandas.core.dtypes.common import ( is_array_like, is_bool_dtype, - is_extension_array_dtype, is_integer, is_integer_dtype, is_list_like, ) +from pandas.core.dtypes.dtypes import ExtensionDtype from pandas.core.dtypes.generic import ( ABCIndex, ABCSeries, @@ -531,7 +531,7 @@ def check_array_indexer(array: AnyArrayLike, indexer: Any) -> Any: dtype = indexer.dtype if is_bool_dtype(dtype): - if is_extension_array_dtype(dtype): + if isinstance(dtype, ExtensionDtype): indexer = indexer.to_numpy(dtype=bool, na_value=False) else: indexer = np.asarray(indexer, dtype=bool) diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index c93eb0fe3def6..934daf7eaf708 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -95,7 +95,6 @@ is_bool_dtype, is_dtype_equal, is_ea_or_datetimelike_dtype, - is_extension_array_dtype, is_float, is_float_dtype, is_hashable, @@ -1376,7 +1375,7 @@ def _format_native_types( """ from pandas.io.formats.format import FloatArrayFormatter - if is_float_dtype(self.dtype) and not is_extension_array_dtype(self.dtype): + if is_float_dtype(self.dtype) and not isinstance(self.dtype, ExtensionDtype): formatter = FloatArrayFormatter( self._values, na_rep=na_rep, @@ -1388,7 +1387,7 @@ def _format_native_types( return formatter.get_result_as_array() mask = isna(self) - if not is_object_dtype(self) and not quoting: + if self.dtype != object and not quoting: values = np.asarray(self).astype(str) else: values = np.array(self, dtype=object, copy=True) @@ -5200,7 +5199,7 @@ def __getitem__(self, key): # takes 166 µs + 2.1 ms and cuts the ndarray.__getitem__ # time below from 3.8 ms to 496 µs # if we already have ndarray[bool], the overhead is 1.4 µs or .25% - if is_extension_array_dtype(getattr(key, "dtype", None)): + if isinstance(getattr(key, "dtype", None), ExtensionDtype): key = key.to_numpy(dtype=bool, na_value=False) else: key = np.asarray(key, dtype=bool) @@ -5409,7 +5408,7 @@ def equals(self, other: Any) -> bool: earr = cast(ExtensionArray, self._data) return earr.equals(other._data) - if is_extension_array_dtype(other.dtype): + if isinstance(other.dtype, ExtensionDtype): # All EA-backed Index subclasses override equals return other.equals(self) diff --git a/pandas/core/indexes/multi.py b/pandas/core/indexes/multi.py index abe4a00e0b813..dc1c87b4787a8 100644 --- a/pandas/core/indexes/multi.py +++ b/pandas/core/indexes/multi.py @@ -57,8 +57,6 @@ from pandas.core.dtypes.common import ( ensure_int64, ensure_platform_int, - is_categorical_dtype, - is_extension_array_dtype, is_hashable, is_integer, is_iterator, @@ -67,7 +65,10 @@ is_scalar, pandas_dtype, ) -from pandas.core.dtypes.dtypes import ExtensionDtype +from pandas.core.dtypes.dtypes import ( + CategoricalDtype, + ExtensionDtype, +) from pandas.core.dtypes.generic import ( ABCDataFrame, ABCDatetimeIndex, @@ -748,7 +749,7 @@ def _values(self) -> np.ndarray: codes = self.codes[i] vals = index - if is_categorical_dtype(vals.dtype): + if isinstance(vals.dtype, CategoricalDtype): vals = cast("CategoricalIndex", vals) vals = vals._data._internal_get_values() @@ -3650,7 +3651,7 @@ def _convert_can_do_setop(self, other): @doc(Index.astype) def astype(self, dtype, copy: bool = True): dtype = pandas_dtype(dtype) - if is_categorical_dtype(dtype): + if isinstance(dtype, CategoricalDtype): msg = "> 1 ndim Categorical are not supported at this time" raise NotImplementedError(msg) if not is_object_dtype(dtype): @@ -3852,13 +3853,13 @@ def sparsify_labels(label_list, start: int = 0, sentinel: object = ""): return list(zip(*result)) -def _get_na_rep(dtype) -> str: - if is_extension_array_dtype(dtype): +def _get_na_rep(dtype: DtypeObj) -> str: + if isinstance(dtype, ExtensionDtype): return f"{dtype.na_value}" else: - dtype = dtype.type + dtype_type = dtype.type - return {np.datetime64: "NaT", np.timedelta64: "NaT"}.get(dtype, "NaN") + return {np.datetime64: "NaT", np.timedelta64: "NaT"}.get(dtype_type, "NaN") def maybe_droplevels(index: Index, key) -> Index: diff --git a/pandas/core/internals/blocks.py b/pandas/core/internals/blocks.py index 311729d3dc00a..f48b044ff0016 100644 --- a/pandas/core/internals/blocks.py +++ b/pandas/core/internals/blocks.py @@ -58,14 +58,13 @@ is_1d_only_ea_dtype, is_1d_only_ea_obj, is_dtype_equal, - is_interval_dtype, is_list_like, - is_sparse, is_string_dtype, ) from pandas.core.dtypes.dtypes import ( DatetimeTZDtype, ExtensionDtype, + IntervalDtype, PandasDtype, PeriodDtype, ) @@ -106,6 +105,7 @@ PeriodArray, TimedeltaArray, ) +from pandas.core.arrays.sparse.dtype import SparseDtype from pandas.core.base import PandasObject import pandas.core.common as com from pandas.core.computation import expressions @@ -1620,7 +1620,7 @@ def setitem(self, indexer, value, using_cow: bool = False): except (ValueError, TypeError) as err: _catch_deprecated_value_error(err) - if is_interval_dtype(self.dtype): + if isinstance(self.dtype, IntervalDtype): # see TestSetitemFloatIntervalWithIntIntervalValues nb = self.coerce_to_target_dtype(orig_value) return nb.setitem(orig_indexer, orig_value) @@ -1665,7 +1665,7 @@ def where( _catch_deprecated_value_error(err) if self.ndim == 1 or self.shape[0] == 1: - if is_interval_dtype(self.dtype): + if isinstance(self.dtype, IntervalDtype): # TestSetitemFloatIntervalWithIntIntervalValues blk = self.coerce_to_target_dtype(orig_other) nbs = blk.where(orig_other, orig_cond, using_cow=using_cow) @@ -1740,7 +1740,7 @@ def putmask(self, mask, new, using_cow: bool = False) -> list[Block]: _catch_deprecated_value_error(err) if self.ndim == 1 or self.shape[0] == 1: - if is_interval_dtype(self.dtype): + if isinstance(self.dtype, IntervalDtype): # Discussion about what we want to support in the general # case GH#39584 blk = self.coerce_to_target_dtype(orig_new) @@ -1848,7 +1848,7 @@ def fillna( downcast=None, using_cow: bool = False, ) -> list[Block]: - if is_interval_dtype(self.dtype): + if isinstance(self.dtype, IntervalDtype): # Block.fillna handles coercion (test_fillna_interval) return super().fillna( value=value, @@ -2517,7 +2517,7 @@ def to_native_types( results_converted.append(result.astype(object, copy=False)) return np.vstack(results_converted) - elif values.dtype.kind == "f" and not is_sparse(values): + elif values.dtype.kind == "f" and not isinstance(values.dtype, SparseDtype): # see GH#13418: no special formatting is desired at the # output (important for appropriate 'quoting' behaviour), # so do not pass it through the FloatArrayFormatter diff --git a/pandas/core/internals/construction.py b/pandas/core/internals/construction.py index bcf0b77dab9b2..34e3ce92698cb 100644 --- a/pandas/core/internals/construction.py +++ b/pandas/core/internals/construction.py @@ -30,7 +30,6 @@ is_bool_dtype, is_datetime_or_timedelta_dtype, is_dtype_equal, - is_extension_array_dtype, is_float_dtype, is_integer_dtype, is_list_like, @@ -283,7 +282,7 @@ def ndarray_to_mgr( return arrays_to_mgr(values, columns, index, dtype=dtype, typ=typ) - elif is_extension_array_dtype(vdtype): + elif isinstance(vdtype, ExtensionDtype): # i.e. Datetime64TZ, PeriodDtype; cases with is_1d_only_ea_dtype(vdtype) # are already caught above values = extract_array(values, extract_numpy=True) diff --git a/pandas/core/reshape/tile.py b/pandas/core/reshape/tile.py index 54ae217990d96..0ce6a86d98403 100644 --- a/pandas/core/reshape/tile.py +++ b/pandas/core/reshape/tile.py @@ -26,13 +26,13 @@ is_datetime64_dtype, is_datetime64tz_dtype, is_datetime_or_timedelta_dtype, - is_extension_array_dtype, is_integer, is_list_like, is_numeric_dtype, is_scalar, is_timedelta64_dtype, ) +from pandas.core.dtypes.dtypes import ExtensionDtype from pandas.core.dtypes.generic import ABCSeries from pandas.core.dtypes.missing import isna @@ -498,7 +498,7 @@ def _coerce_to_type(x): # Will properly support in the future. # https://github.com/pandas-dev/pandas/pull/31290 # https://github.com/pandas-dev/pandas/issues/31389 - elif is_extension_array_dtype(x.dtype) and is_numeric_dtype(x.dtype): + elif isinstance(x.dtype, ExtensionDtype) and is_numeric_dtype(x.dtype): x = x.to_numpy(dtype=np.float64, na_value=np.nan) if dtype is not None: diff --git a/pandas/io/formats/format.py b/pandas/io/formats/format.py index dc89c11bef231..fc12a8b0722e6 100644 --- a/pandas/io/formats/format.py +++ b/pandas/io/formats/format.py @@ -51,10 +51,8 @@ from pandas._libs.tslibs.nattype import NaTType from pandas.core.dtypes.common import ( - is_categorical_dtype, is_complex_dtype, is_datetime64_dtype, - is_extension_array_dtype, is_float, is_float_dtype, is_integer, @@ -64,7 +62,11 @@ is_scalar, is_timedelta64_dtype, ) -from pandas.core.dtypes.dtypes import DatetimeTZDtype +from pandas.core.dtypes.dtypes import ( + CategoricalDtype, + DatetimeTZDtype, + ExtensionDtype, +) from pandas.core.dtypes.missing import ( isna, notna, @@ -355,7 +357,7 @@ def _get_footer(self) -> str: # level infos are added to the end and in a new line, like it is done # for Categoricals - if is_categorical_dtype(self.tr_series.dtype): + if isinstance(self.tr_series.dtype, CategoricalDtype): level_info = self.tr_series._values._repr_categories_info() if footer: footer += "\n" @@ -1294,7 +1296,7 @@ def format_array( fmt_klass = Datetime64TZFormatter elif is_timedelta64_dtype(values.dtype): fmt_klass = Timedelta64Formatter - elif is_extension_array_dtype(values.dtype): + elif isinstance(values.dtype, ExtensionDtype): fmt_klass = ExtensionArrayFormatter elif is_float_dtype(values.dtype) or is_complex_dtype(values.dtype): fmt_klass = FloatArrayFormatter