From b23ffc9e80e573aac960aca28c19a6e473e7db1c Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Thu, 28 Nov 2019 09:21:17 -0800 Subject: [PATCH 1/3] DEPR: remove itemsize, data, base, flags, strides --- doc/source/whatsnew/v1.0.0.rst | 2 +- pandas/core/arrays/categorical.py | 7 -- pandas/core/arrays/period.py | 8 -- pandas/core/base.py | 97 +------------------ pandas/core/indexes/interval.py | 14 --- pandas/core/indexes/period.py | 47 +-------- .../tests/indexes/interval/test_interval.py | 11 --- pandas/tests/indexes/test_category.py | 6 +- pandas/tests/series/test_api.py | 9 +- pandas/tests/test_base.py | 24 +---- 10 files changed, 19 insertions(+), 206 deletions(-) diff --git a/doc/source/whatsnew/v1.0.0.rst b/doc/source/whatsnew/v1.0.0.rst index e54397e635c77..4b6d691525072 100644 --- a/doc/source/whatsnew/v1.0.0.rst +++ b/doc/source/whatsnew/v1.0.0.rst @@ -458,7 +458,7 @@ or ``matplotlib.Axes.plot``. See :ref:`plotting.formatters` for more. - Changed the default value for the `raw` argument in :func:`Series.rolling().apply() `, :func:`DataFrame.rolling().apply() `, - :func:`Series.expanding().apply() `, and :func:`DataFrame.expanding().apply() ` to ``False`` (:issue:`20584`) - Removed previously deprecated :attr:`Timestamp.weekday_name`, :attr:`DatetimeIndex.weekday_name`, and :attr:`Series.dt.weekday_name` (:issue:`18164`) -- +- Removed the previously deprecated :attr:`Series.base`, :attr:`Index.base`, :attr:`Categorical.base`, :attr:`Series.flags`, :attr:`Index.flags`, :attr:`PeriodArray.flags`, :attr:`Series.strides`, :attr:`Index.strides`, :attr:`Series.itemsize`, :attr:`Index.itemsize`, :attr:`Series.data`, :attr:`Index.data` (:issue:`20721`) .. _whatsnew_1000.performance: diff --git a/pandas/core/arrays/categorical.py b/pandas/core/arrays/categorical.py index f20308be1ee09..f67d12f40ad2e 100644 --- a/pandas/core/arrays/categorical.py +++ b/pandas/core/arrays/categorical.py @@ -540,13 +540,6 @@ def tolist(self) -> list: to_list = tolist - @property - def base(self) -> None: - """ - compat, we are always our own object - """ - return None - @classmethod def _from_inferred_categories( cls, inferred_categories, inferred_codes, dtype, true_values=None diff --git a/pandas/core/arrays/period.py b/pandas/core/arrays/period.py index 41a8c48452647..1012abd0b5d13 100644 --- a/pandas/core/arrays/period.py +++ b/pandas/core/arrays/period.py @@ -618,14 +618,6 @@ def astype(self, dtype, copy=True): return self.asfreq(dtype.freq) return super().astype(dtype, copy=copy) - @property - def flags(self): - # TODO: remove - # We need this since reduction.SeriesBinGrouper uses values.flags - # Ideally, we wouldn't be passing objects down there in the first - # place. - return self._data.flags - # ------------------------------------------------------------------ # Arithmetic Methods _create_comparison_method = classmethod(_period_array_cmp) diff --git a/pandas/core/base.py b/pandas/core/base.py index 83d6ac76cdd98..394b2f8532a1a 100644 --- a/pandas/core/base.py +++ b/pandas/core/base.py @@ -5,7 +5,6 @@ from collections import OrderedDict import textwrap from typing import Dict, FrozenSet, List, Optional -import warnings import numpy as np @@ -628,15 +627,7 @@ class IndexOpsMixin: # ndarray compatibility __array_priority__ = 1000 _deprecations: FrozenSet[str] = frozenset( - [ - "tolist", # tolist is not deprecated, just suppressed in the __dir__ - "base", - "data", - "item", - "itemsize", - "flags", - "strides", - ] + ["tolist"] # tolist is not deprecated, just suppressed in the __dir__ ) def transpose(self, *args, **kwargs): @@ -693,49 +684,14 @@ def item(self): """ Return the first element of the underlying data as a python scalar. - .. deprecated:: 0.25.0 - Returns ------- scalar The first element of %(klass)s. """ - warnings.warn( - "`item` has been deprecated and will be removed in a future version", - FutureWarning, - stacklevel=2, - ) - return self.values.item() - - @property - def data(self): - """ - Return the data pointer of the underlying data. - - .. deprecated:: 0.23.0 - """ - warnings.warn( - "{obj}.data is deprecated and will be removed " - "in a future version".format(obj=type(self).__name__), - FutureWarning, - stacklevel=2, - ) - return self.values.data - - @property - def itemsize(self): - """ - Return the size of the dtype of the item of the underlying data. - - .. deprecated:: 0.23.0 - """ - warnings.warn( - "{obj}.itemsize is deprecated and will be removed " - "in a future version".format(obj=type(self).__name__), - FutureWarning, - stacklevel=2, - ) - return self._ndarray_values.itemsize + if len(self) == 1: + return next(iter(self)) + raise ValueError("can only convert an array of size 1 to a Python scalar") @property def nbytes(self): @@ -744,21 +700,6 @@ def nbytes(self): """ return self._values.nbytes - @property - def strides(self): - """ - Return the strides of the underlying data. - - .. deprecated:: 0.23.0 - """ - warnings.warn( - "{obj}.strides is deprecated and will be removed " - "in a future version".format(obj=type(self).__name__), - FutureWarning, - stacklevel=2, - ) - return self._ndarray_values.strides - @property def size(self): """ @@ -766,36 +707,6 @@ def size(self): """ return len(self._values) - @property - def flags(self): - """ - Return the ndarray.flags for the underlying data. - - .. deprecated:: 0.23.0 - """ - warnings.warn( - "{obj}.flags is deprecated and will be removed " - "in a future version".format(obj=type(self).__name__), - FutureWarning, - stacklevel=2, - ) - return self.values.flags - - @property - def base(self): - """ - Return the base object if the memory of the underlying data is shared. - - .. deprecated:: 0.23.0 - """ - warnings.warn( - "{obj}.base is deprecated and will be removed " - "in a future version".format(obj=type(self).__name__), - FutureWarning, - stacklevel=2, - ) - return self.values.base - @property def array(self) -> ExtensionArray: """ diff --git a/pandas/core/indexes/interval.py b/pandas/core/indexes/interval.py index 35e8405e0f1aa..f885c9a99685d 100644 --- a/pandas/core/indexes/interval.py +++ b/pandas/core/indexes/interval.py @@ -2,7 +2,6 @@ from operator import le, lt import textwrap from typing import Any, Optional, Tuple, Union -import warnings import numpy as np @@ -455,19 +454,6 @@ def size(self): # Avoid materializing ndarray[Interval] return self._data.size - @property - def itemsize(self): - msg = ( - "IntervalIndex.itemsize is deprecated and will be removed in " - "a future version" - ) - warnings.warn(msg, FutureWarning, stacklevel=2) - - # suppress the warning from the underlying left/right itemsize - with warnings.catch_warnings(): - warnings.simplefilter("ignore") - return self.left.itemsize + self.right.itemsize - def __len__(self) -> int: return len(self.left) diff --git a/pandas/core/indexes/period.py b/pandas/core/indexes/period.py index cdd0e600c888d..25bf4710ea0e4 100644 --- a/pandas/core/indexes/period.py +++ b/pandas/core/indexes/period.py @@ -911,31 +911,10 @@ def __setstate__(self, state): _unpickle_compat = __setstate__ - @property - def flags(self): - """ return the ndarray.flags for the underlying data """ - warnings.warn( - "{obj}.flags is deprecated and will be removed " - "in a future version".format(obj=type(self).__name__), - FutureWarning, - stacklevel=2, - ) - return self._ndarray_values.flags - def item(self): """ - return the first element of the underlying data as a python - scalar - - .. deprecated:: 0.25.0 - + Return the first element of the underlying data as a python scalar. """ - warnings.warn( - "`item` has been deprecated and will be removed in a future version", - FutureWarning, - stacklevel=2, - ) - # TODO(DatetimeArray): remove if len(self) == 1: return self[0] else: @@ -943,30 +922,6 @@ def item(self): # copy numpy's message here because Py26 raises an IndexError raise ValueError("can only convert an array of size 1 to a Python scalar") - @property - def data(self): - """ return the data pointer of the underlying data """ - warnings.warn( - "{obj}.data is deprecated and will be removed " - "in a future version".format(obj=type(self).__name__), - FutureWarning, - stacklevel=2, - ) - return np.asarray(self._data).data - - @property - def base(self): - """ return the base object if the memory of the underlying data is - shared - """ - warnings.warn( - "{obj}.base is deprecated and will be removed " - "in a future version".format(obj=type(self).__name__), - FutureWarning, - stacklevel=2, - ) - return np.asarray(self._data) - def memory_usage(self, deep=False): result = super().memory_usage(deep=deep) if hasattr(self, "_cache") and "_int64index" in self._cache: diff --git a/pandas/tests/indexes/interval/test_interval.py b/pandas/tests/indexes/interval/test_interval.py index f3c8c5cb6efa1..6ad7dfb22f2b3 100644 --- a/pandas/tests/indexes/interval/test_interval.py +++ b/pandas/tests/indexes/interval/test_interval.py @@ -836,17 +836,6 @@ def test_nbytes(self): expected = 64 # 4 * 8 * 2 assert result == expected - def test_itemsize(self): - # GH 19209 - left = np.arange(0, 4, dtype="i8") - right = np.arange(1, 5, dtype="i8") - expected = 16 # 8 * 2 - - with tm.assert_produces_warning(FutureWarning, check_stacklevel=False): - result = IntervalIndex.from_arrays(left, right).itemsize - - assert result == expected - @pytest.mark.parametrize("new_closed", ["left", "right", "both", "neither"]) def test_set_closed(self, name, closed, new_closed): # GH 21670 diff --git a/pandas/tests/indexes/test_category.py b/pandas/tests/indexes/test_category.py index 84f98a55376f7..e786d20f2e642 100644 --- a/pandas/tests/indexes/test_category.py +++ b/pandas/tests/indexes/test_category.py @@ -788,8 +788,10 @@ def test_ensure_copied_data(self, indices): # Index.__new__ is honored. # # Must be tested separately from other indexes because - # self.value is not an ndarray. - _base = lambda ar: ar if ar.base is None else ar.base + # self.values is not an ndarray. + # GH#????? Index.base has been removed + # FIXME: is this test still meaningful? + _base = lambda ar: ar if getattr(ar, "base", None) is None else ar.base result = CategoricalIndex(indices.values, copy=True) tm.assert_index_equal(indices, result) diff --git a/pandas/tests/series/test_api.py b/pandas/tests/series/test_api.py index 1e4757ffecb5d..7dc57e6d29835 100644 --- a/pandas/tests/series/test_api.py +++ b/pandas/tests/series/test_api.py @@ -433,11 +433,10 @@ def f(x): tm.assert_series_equal(result, expected) # .item() - with tm.assert_produces_warning(FutureWarning): - s = Series([1]) - result = s.item() - assert result == 1 - assert s.item() == s.iloc[0] + s = Series([1]) + result = s.item() + assert result == 1 + assert s.item() == s.iloc[0] # using an ndarray like function s = Series(np.random.randn(10)) diff --git a/pandas/tests/test_base.py b/pandas/tests/test_base.py index f24bb9e72aef5..c6553bbe9f7bb 100644 --- a/pandas/tests/test_base.py +++ b/pandas/tests/test_base.py @@ -333,31 +333,17 @@ def test_ndarray_compat_properties(self): assert getattr(o, p, None) is not None # deprecated properties - for p in ["flags", "strides", "itemsize"]: - with tm.assert_produces_warning(FutureWarning): - assert getattr(o, p, None) is not None - - with tm.assert_produces_warning(FutureWarning): - assert hasattr(o, "base") - - # If we have a datetime-like dtype then needs a view to work - # but the user is responsible for that - try: - with tm.assert_produces_warning(FutureWarning): - assert o.data is not None - except ValueError: - pass + for p in ["flags", "strides", "itemsize", "base", "data"]: + assert not hasattr(o, p) with pytest.raises(ValueError): - with tm.assert_produces_warning(FutureWarning): - o.item() # len > 1 + o.item() # len > 1 assert o.ndim == 1 assert o.size == len(o) - with tm.assert_produces_warning(FutureWarning): - assert Index([1]).item() == 1 - assert Series([1]).item() == 1 + assert Index([1]).item() == 1 + assert Series([1]).item() == 1 def test_value_counts_unique_nunique(self): for orig in self.objs: From afc28ff84cdc044a48eb22d9f1ea8e64f836b41c Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Fri, 29 Nov 2019 08:25:58 -0800 Subject: [PATCH 2/3] update docs, revert item change --- doc/redirects.csv | 8 -------- doc/source/reference/indexing.rst | 3 --- doc/source/reference/series.rst | 4 ---- pandas/core/base.py | 14 ++++++++++---- pandas/core/indexes/period.py | 12 +++++++++++- pandas/tests/indexes/test_category.py | 2 +- pandas/tests/series/test_api.py | 9 +++++---- pandas/tests/test_base.py | 8 +++++--- 8 files changed, 32 insertions(+), 28 deletions(-) diff --git a/doc/redirects.csv b/doc/redirects.csv index f124fdb840ce0..a305a1a50a31a 100644 --- a/doc/redirects.csv +++ b/doc/redirects.csv @@ -622,7 +622,6 @@ generated/pandas.Index.asi8,../reference/api/pandas.Index.asi8 generated/pandas.Index.asof,../reference/api/pandas.Index.asof generated/pandas.Index.asof_locs,../reference/api/pandas.Index.asof_locs generated/pandas.Index.astype,../reference/api/pandas.Index.astype -generated/pandas.Index.base,../reference/api/pandas.Index.base generated/pandas.Index.contains,../reference/api/pandas.Index.contains generated/pandas.Index.copy,../reference/api/pandas.Index.copy generated/pandas.Index.data,../reference/api/pandas.Index.data @@ -639,7 +638,6 @@ generated/pandas.Index.empty,../reference/api/pandas.Index.empty generated/pandas.Index.equals,../reference/api/pandas.Index.equals generated/pandas.Index.factorize,../reference/api/pandas.Index.factorize generated/pandas.Index.fillna,../reference/api/pandas.Index.fillna -generated/pandas.Index.flags,../reference/api/pandas.Index.flags generated/pandas.Index.format,../reference/api/pandas.Index.format generated/pandas.Index.get_duplicates,../reference/api/pandas.Index.get_duplicates generated/pandas.Index.get_indexer_for,../reference/api/pandas.Index.get_indexer_for @@ -679,7 +677,6 @@ generated/pandas.Index.is_object,../reference/api/pandas.Index.is_object generated/pandas.Index.is_type_compatible,../reference/api/pandas.Index.is_type_compatible generated/pandas.Index.is_unique,../reference/api/pandas.Index.is_unique generated/pandas.Index.item,../reference/api/pandas.Index.item -generated/pandas.Index.itemsize,../reference/api/pandas.Index.itemsize generated/pandas.Index.join,../reference/api/pandas.Index.join generated/pandas.Index.map,../reference/api/pandas.Index.map generated/pandas.Index.max,../reference/api/pandas.Index.max @@ -711,7 +708,6 @@ generated/pandas.Index.sort,../reference/api/pandas.Index.sort generated/pandas.Index.sortlevel,../reference/api/pandas.Index.sortlevel generated/pandas.Index.sort_values,../reference/api/pandas.Index.sort_values generated/pandas.Index.str,../reference/api/pandas.Index.str -generated/pandas.Index.strides,../reference/api/pandas.Index.strides generated/pandas.Index.summary,../reference/api/pandas.Index.summary generated/pandas.Index.symmetric_difference,../reference/api/pandas.Index.symmetric_difference generated/pandas.Index.take,../reference/api/pandas.Index.take @@ -1106,7 +1102,6 @@ generated/pandas.Series.at,../reference/api/pandas.Series.at generated/pandas.Series.at_time,../reference/api/pandas.Series.at_time generated/pandas.Series.autocorr,../reference/api/pandas.Series.autocorr generated/pandas.Series.axes,../reference/api/pandas.Series.axes -generated/pandas.Series.base,../reference/api/pandas.Series.base generated/pandas.Series.between,../reference/api/pandas.Series.between generated/pandas.Series.between_time,../reference/api/pandas.Series.between_time generated/pandas.Series.bfill,../reference/api/pandas.Series.bfill @@ -1215,7 +1210,6 @@ generated/pandas.Series.fillna,../reference/api/pandas.Series.fillna generated/pandas.Series.filter,../reference/api/pandas.Series.filter generated/pandas.Series.first,../reference/api/pandas.Series.first generated/pandas.Series.first_valid_index,../reference/api/pandas.Series.first_valid_index -generated/pandas.Series.flags,../reference/api/pandas.Series.flags generated/pandas.Series.floordiv,../reference/api/pandas.Series.floordiv generated/pandas.Series.from_array,../reference/api/pandas.Series.from_array generated/pandas.Series.from_csv,../reference/api/pandas.Series.from_csv @@ -1248,7 +1242,6 @@ generated/pandas.Series.isnull,../reference/api/pandas.Series.isnull generated/pandas.Series.is_unique,../reference/api/pandas.Series.is_unique generated/pandas.Series.item,../reference/api/pandas.Series.item generated/pandas.Series.items,../reference/api/pandas.Series.items -generated/pandas.Series.itemsize,../reference/api/pandas.Series.itemsize generated/pandas.Series.__iter__,../reference/api/pandas.Series.__iter__ generated/pandas.Series.iteritems,../reference/api/pandas.Series.iteritems generated/pandas.Series.ix,../reference/api/pandas.Series.ix @@ -1361,7 +1354,6 @@ generated/pandas.Series.str.find,../reference/api/pandas.Series.str.find generated/pandas.Series.str.get_dummies,../reference/api/pandas.Series.str.get_dummies generated/pandas.Series.str.get,../reference/api/pandas.Series.str.get generated/pandas.Series.str,../reference/api/pandas.Series.str -generated/pandas.Series.strides,../reference/api/pandas.Series.strides generated/pandas.Series.str.index,../reference/api/pandas.Series.str.index generated/pandas.Series.str.isalnum,../reference/api/pandas.Series.str.isalnum generated/pandas.Series.str.isalpha,../reference/api/pandas.Series.str.isalpha diff --git a/doc/source/reference/indexing.rst b/doc/source/reference/indexing.rst index 448f020cfa56f..8edea28c17318 100644 --- a/doc/source/reference/indexing.rst +++ b/doc/source/reference/indexing.rst @@ -42,9 +42,6 @@ Properties Index.ndim Index.size Index.empty - Index.strides - Index.itemsize - Index.base Index.T Index.memory_usage diff --git a/doc/source/reference/series.rst b/doc/source/reference/series.rst index 528cc8a0c3920..eda6d05e0cf42 100644 --- a/doc/source/reference/series.rst +++ b/doc/source/reference/series.rst @@ -33,13 +33,9 @@ Attributes Series.nbytes Series.ndim Series.size - Series.strides - Series.itemsize - Series.base Series.T Series.memory_usage Series.hasnans - Series.flags Series.empty Series.dtypes Series.data diff --git a/pandas/core/base.py b/pandas/core/base.py index 394b2f8532a1a..066a7628be364 100644 --- a/pandas/core/base.py +++ b/pandas/core/base.py @@ -5,6 +5,7 @@ from collections import OrderedDict import textwrap from typing import Dict, FrozenSet, List, Optional +import warnings import numpy as np @@ -627,7 +628,7 @@ class IndexOpsMixin: # ndarray compatibility __array_priority__ = 1000 _deprecations: FrozenSet[str] = frozenset( - ["tolist"] # tolist is not deprecated, just suppressed in the __dir__ + ["tolist", "item"] # tolist is not deprecated, just suppressed in the __dir__ ) def transpose(self, *args, **kwargs): @@ -684,14 +685,19 @@ def item(self): """ Return the first element of the underlying data as a python scalar. + .. deprecated:: 0.25.0 + Returns ------- scalar The first element of %(klass)s. """ - if len(self) == 1: - return next(iter(self)) - raise ValueError("can only convert an array of size 1 to a Python scalar") + warnings.warn( + "`item` has been deprecated and will be removed in a future version", + FutureWarning, + stacklevel=2, + ) + return self.values.item() @property def nbytes(self): diff --git a/pandas/core/indexes/period.py b/pandas/core/indexes/period.py index 25bf4710ea0e4..b3476dcb12abd 100644 --- a/pandas/core/indexes/period.py +++ b/pandas/core/indexes/period.py @@ -913,8 +913,18 @@ def __setstate__(self, state): def item(self): """ - Return the first element of the underlying data as a python scalar. + Return the first element of the underlying data as a python + scalar + + .. deprecated:: 0.25.0 + """ + warnings.warn( + "`item` has been deprecated and will be removed in a future version", + FutureWarning, + stacklevel=2, + ) + # TODO(DatetimeArray): remove if len(self) == 1: return self[0] else: diff --git a/pandas/tests/indexes/test_category.py b/pandas/tests/indexes/test_category.py index e786d20f2e642..86219d77542af 100644 --- a/pandas/tests/indexes/test_category.py +++ b/pandas/tests/indexes/test_category.py @@ -789,7 +789,7 @@ def test_ensure_copied_data(self, indices): # # Must be tested separately from other indexes because # self.values is not an ndarray. - # GH#????? Index.base has been removed + # GH#29918 Index.base has been removed # FIXME: is this test still meaningful? _base = lambda ar: ar if getattr(ar, "base", None) is None else ar.base diff --git a/pandas/tests/series/test_api.py b/pandas/tests/series/test_api.py index 7dc57e6d29835..1e4757ffecb5d 100644 --- a/pandas/tests/series/test_api.py +++ b/pandas/tests/series/test_api.py @@ -433,10 +433,11 @@ def f(x): tm.assert_series_equal(result, expected) # .item() - s = Series([1]) - result = s.item() - assert result == 1 - assert s.item() == s.iloc[0] + with tm.assert_produces_warning(FutureWarning): + s = Series([1]) + result = s.item() + assert result == 1 + assert s.item() == s.iloc[0] # using an ndarray like function s = Series(np.random.randn(10)) diff --git a/pandas/tests/test_base.py b/pandas/tests/test_base.py index c6553bbe9f7bb..4d3eecf89c732 100644 --- a/pandas/tests/test_base.py +++ b/pandas/tests/test_base.py @@ -337,13 +337,15 @@ def test_ndarray_compat_properties(self): assert not hasattr(o, p) with pytest.raises(ValueError): - o.item() # len > 1 + with tm.assert_produces_warning(FutureWarning): + o.item() # len > 1 assert o.ndim == 1 assert o.size == len(o) - assert Index([1]).item() == 1 - assert Series([1]).item() == 1 + with tm.assert_produces_warning(FutureWarning): + assert Index([1]).item() == 1 + assert Series([1]).item() == 1 def test_value_counts_unique_nunique(self): for orig in self.objs: From 07d2776c0599d3fd3106de239f8de31f1f4c02fe Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Fri, 29 Nov 2019 11:49:29 -0800 Subject: [PATCH 3/3] update docs --- doc/source/reference/series.rst | 1 - 1 file changed, 1 deletion(-) diff --git a/doc/source/reference/series.rst b/doc/source/reference/series.rst index eda6d05e0cf42..91843c7975a2c 100644 --- a/doc/source/reference/series.rst +++ b/doc/source/reference/series.rst @@ -38,7 +38,6 @@ Attributes Series.hasnans Series.empty Series.dtypes - Series.data Series.name Series.put