diff --git a/doc/source/whatsnew/v1.0.0.rst b/doc/source/whatsnew/v1.0.0.rst index 370e1c09d33aa..8c2b140cc2311 100644 --- a/doc/source/whatsnew/v1.0.0.rst +++ b/doc/source/whatsnew/v1.0.0.rst @@ -213,7 +213,8 @@ Deprecations - ``Index.set_value`` has been deprecated. For a given index ``idx``, array ``arr``, value in ``idx`` of ``idx_val`` and a new value of ``val``, ``idx.set_value(arr, idx_val, val)`` is equivalent to ``arr[idx.get_loc(idx_val)] = val``, which should be used instead (:issue:`28621`). -- +- :func:`is_extension_type` is deprecated, :func:`is_extension_array_dtype` should be used instead (:issue:`29457`) + .. _whatsnew_1000.prior_deprecations: diff --git a/pandas/core/apply.py b/pandas/core/apply.py index f402154dc91ca..e7b088658ac5d 100644 --- a/pandas/core/apply.py +++ b/pandas/core/apply.py @@ -7,7 +7,7 @@ from pandas.core.dtypes.common import ( is_dict_like, - is_extension_type, + is_extension_array_dtype, is_list_like, is_sequence, ) @@ -230,7 +230,7 @@ def apply_standard(self): # as demonstrated in gh-12244 if ( self.result_type in ["reduce", None] - and not self.dtypes.apply(is_extension_type).any() + and not self.dtypes.apply(is_extension_array_dtype).any() # Disallow complex_internals since libreduction shortcut # cannot handle MultiIndex and not self.agg_axis._has_complex_internals diff --git a/pandas/core/arrays/datetimes.py b/pandas/core/arrays/datetimes.py index 788cd2a3ce5b7..7cd103d12fa8a 100644 --- a/pandas/core/arrays/datetimes.py +++ b/pandas/core/arrays/datetimes.py @@ -31,7 +31,7 @@ is_datetime64_ns_dtype, is_datetime64tz_dtype, is_dtype_equal, - is_extension_type, + is_extension_array_dtype, is_float_dtype, is_object_dtype, is_period_dtype, @@ -2131,7 +2131,7 @@ def maybe_convert_dtype(data, copy): data = data.categories.take(data.codes, fill_value=NaT)._values copy = False - elif is_extension_type(data) and not is_datetime64tz_dtype(data): + elif is_extension_array_dtype(data) and not is_datetime64tz_dtype(data): # Includes categorical # TODO: We have no tests for these data = np.array(data, dtype=np.object_) diff --git a/pandas/core/base.py b/pandas/core/base.py index 61dc5f35cadf7..a1985f4afc754 100644 --- a/pandas/core/base.py +++ b/pandas/core/base.py @@ -23,7 +23,6 @@ is_datetime64tz_dtype, is_datetimelike, is_extension_array_dtype, - is_extension_type, is_list_like, is_object_dtype, is_scalar, @@ -1268,7 +1267,7 @@ def _map_values(self, mapper, na_action=None): # use the built in categorical series mapper which saves # time by mapping the categories instead of all values return self._values.map(mapper) - if is_extension_type(self.dtype): + if is_extension_array_dtype(self.dtype): values = self._values else: values = self.values @@ -1279,7 +1278,8 @@ def _map_values(self, mapper, na_action=None): return new_values # we must convert to python types - if is_extension_type(self.dtype): + if is_extension_array_dtype(self.dtype) and hasattr(self._values, "map"): + # GH#23179 some EAs do not have `map` values = self._values if na_action is not None: raise NotImplementedError diff --git a/pandas/core/construction.py b/pandas/core/construction.py index 5e8b28267f24f..c0b08beead0ca 100644 --- a/pandas/core/construction.py +++ b/pandas/core/construction.py @@ -27,7 +27,6 @@ is_categorical_dtype, is_datetime64_ns_dtype, is_extension_array_dtype, - is_extension_type, is_float_dtype, is_integer_dtype, is_iterator, @@ -527,7 +526,7 @@ def _try_cast( and not (is_iterator(subarr) or isinstance(subarr, np.ndarray)) ): subarr = construct_1d_object_array_from_listlike(subarr) - elif not is_extension_type(subarr): + elif not is_extension_array_dtype(subarr): subarr = construct_1d_ndarray_preserving_na(subarr, dtype, copy=copy) except OutOfBoundsDatetime: # in case of out of bound datetime64 -> always raise diff --git a/pandas/core/dtypes/cast.py b/pandas/core/dtypes/cast.py index fad80d6bf5745..98874fce288bc 100644 --- a/pandas/core/dtypes/cast.py +++ b/pandas/core/dtypes/cast.py @@ -30,7 +30,6 @@ is_datetimelike, is_dtype_equal, is_extension_array_dtype, - is_extension_type, is_float, is_float_dtype, is_integer, @@ -633,7 +632,7 @@ def infer_dtype_from_array(arr, pandas_dtype: bool = False): if not is_list_like(arr): arr = [arr] - if pandas_dtype and is_extension_type(arr): + if pandas_dtype and is_extension_array_dtype(arr): return arr.dtype, arr elif isinstance(arr, ABCSeries): @@ -695,7 +694,7 @@ def maybe_upcast(values, fill_value=np.nan, dtype=None, copy=False): # We allow arbitrary fill values for object dtype raise ValueError("fill_value must be a scalar") - if is_extension_type(values): + if is_extension_array_dtype(values): if copy: values = values.copy() else: diff --git a/pandas/core/dtypes/common.py b/pandas/core/dtypes/common.py index 2a46d335ff512..41cbc731e18c4 100644 --- a/pandas/core/dtypes/common.py +++ b/pandas/core/dtypes/common.py @@ -1674,6 +1674,8 @@ def is_extension_type(arr): """ Check whether an array-like is of a pandas extension class instance. + .. deprecated:: 1.0.0 + Extension classes include categoricals, pandas sparse objects (i.e. classes represented within the pandas library and not ones external to it like scipy sparse matrices), and datetime-like arrays. @@ -1716,6 +1718,12 @@ def is_extension_type(arr): >>> is_extension_type(s) True """ + warnings.warn( + "'is_extension_type' is deprecated and will be removed in a future " + "version. Use 'is_extension_array_dtype' instead.", + FutureWarning, + stacklevel=2, + ) if is_categorical(arr): return True diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 40efc4c65476a..b005b70eedc7e 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -71,7 +71,6 @@ is_dict_like, is_dtype_equal, is_extension_array_dtype, - is_extension_type, is_float_dtype, is_hashable, is_integer, @@ -3690,7 +3689,7 @@ def reindexer(value): value = maybe_cast_to_datetime(value, infer_dtype) # return internal types directly - if is_extension_type(value) or is_extension_array_dtype(value): + if is_extension_array_dtype(value): return value # broadcast across multiple columns if necessary diff --git a/pandas/core/internals/blocks.py b/pandas/core/internals/blocks.py index 448d2faf8b85f..ce889ea95f782 100644 --- a/pandas/core/internals/blocks.py +++ b/pandas/core/internals/blocks.py @@ -37,7 +37,6 @@ is_datetime64tz_dtype, is_dtype_equal, is_extension_array_dtype, - is_extension_type, is_float_dtype, is_integer, is_integer_dtype, @@ -2605,10 +2604,6 @@ def should_store(self, value): value.dtype.type, (np.integer, np.floating, np.complexfloating, np.datetime64, np.bool_), ) - or - # TODO(ExtensionArray): remove is_extension_type - # when all extension arrays have been ported. - is_extension_type(value) or is_extension_array_dtype(value) ) @@ -3168,7 +3163,7 @@ def _putmask_preserve(nv, n): # change the dtype if needed dtype, _ = maybe_promote(n.dtype) - if is_extension_type(v.dtype) and is_object_dtype(dtype): + if is_extension_array_dtype(v.dtype) and is_object_dtype(dtype): v = v._internal_get_values(dtype) else: v = v.astype(dtype) diff --git a/pandas/core/internals/managers.py b/pandas/core/internals/managers.py index 21ae820cfcee6..d32e026351e22 100644 --- a/pandas/core/internals/managers.py +++ b/pandas/core/internals/managers.py @@ -20,7 +20,6 @@ _NS_DTYPE, is_datetimelike_v_numeric, is_extension_array_dtype, - is_extension_type, is_list_like, is_numeric_v_string_like, is_scalar, @@ -1034,11 +1033,7 @@ def set(self, item, value): # FIXME: refactor, clearly separate broadcasting & zip-like assignment # can prob also fix the various if tests for sparse/categorical - # TODO(EA): Remove an is_extension_ when all extension types satisfy - # the interface - value_is_extension_type = is_extension_type(value) or is_extension_array_dtype( - value - ) + value_is_extension_type = is_extension_array_dtype(value) # categorical/sparse/datetimetz if value_is_extension_type: diff --git a/pandas/core/reshape/melt.py b/pandas/core/reshape/melt.py index 98fee491e0a73..9ccd36871050f 100644 --- a/pandas/core/reshape/melt.py +++ b/pandas/core/reshape/melt.py @@ -4,7 +4,7 @@ from pandas.util._decorators import Appender -from pandas.core.dtypes.common import is_extension_type, is_list_like +from pandas.core.dtypes.common import is_extension_array_dtype, is_list_like from pandas.core.dtypes.concat import concat_compat from pandas.core.dtypes.generic import ABCMultiIndex from pandas.core.dtypes.missing import notna @@ -103,7 +103,7 @@ def melt( mdata = {} for col in id_vars: id_data = frame.pop(col) - if is_extension_type(id_data): + if is_extension_array_dtype(id_data): id_data = concat([id_data] * K, ignore_index=True) else: id_data = np.tile(id_data.values, K) diff --git a/pandas/core/series.py b/pandas/core/series.py index 73a05b4cdfa66..ffaecfde6e10f 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -28,7 +28,6 @@ is_datetimelike, is_dict_like, is_extension_array_dtype, - is_extension_type, is_integer, is_iterator, is_list_like, @@ -3958,7 +3957,8 @@ def f(x): return f(self) # row-wise access - if is_extension_type(self.dtype): + if is_extension_array_dtype(self.dtype) and hasattr(self._values, "map"): + # GH#23179 some EAs do not have `map` mapped = self._values.map(f) else: values = self.astype(object).values diff --git a/pandas/io/pytables.py b/pandas/io/pytables.py index 35e6d53127e59..77b2db20ac2a9 100644 --- a/pandas/io/pytables.py +++ b/pandas/io/pytables.py @@ -26,7 +26,7 @@ is_categorical_dtype, is_datetime64_dtype, is_datetime64tz_dtype, - is_extension_type, + is_extension_array_dtype, is_list_like, is_timedelta64_dtype, ) @@ -2827,7 +2827,7 @@ def write_multi_index(self, key, index): zip(index.levels, index.codes, index.names) ): # write the level - if is_extension_type(lev): + if is_extension_array_dtype(lev): raise NotImplementedError( "Saving a MultiIndex with an extension dtype is not supported." ) diff --git a/pandas/tests/api/test_types.py b/pandas/tests/api/test_types.py index 24f325643479c..e9f68692a9863 100644 --- a/pandas/tests/api/test_types.py +++ b/pandas/tests/api/test_types.py @@ -18,7 +18,6 @@ class TestTypes(Base): "is_datetime64_ns_dtype", "is_datetime64tz_dtype", "is_dtype_equal", - "is_extension_type", "is_float", "is_float_dtype", "is_int64_dtype", @@ -51,7 +50,7 @@ class TestTypes(Base): "infer_dtype", "is_extension_array_dtype", ] - deprecated = ["is_period", "is_datetimetz"] + deprecated = ["is_period", "is_datetimetz", "is_extension_type"] dtypes = ["CategoricalDtype", "DatetimeTZDtype", "PeriodDtype", "IntervalDtype"] def test_types(self):