From 977eb49f0222daeff62c6f50313f28b58fc1e3f3 Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Wed, 4 Dec 2019 14:51:47 -0800 Subject: [PATCH] DEPR: remove get_values, SparseArray.values (#29989) --- doc/redirects.csv | 3 -- doc/source/reference/frame.rst | 1 - doc/source/reference/indexing.rst | 1 - doc/source/reference/series.rst | 1 - pandas/_libs/reduction.pyx | 11 +----- pandas/core/arrays/categorical.py | 19 ++-------- pandas/core/arrays/sparse/array.py | 33 ----------------- pandas/core/generic.py | 41 +-------------------- pandas/core/indexes/base.py | 26 ++++--------- pandas/core/series.py | 13 +------ pandas/tests/arrays/categorical/test_api.py | 6 --- pandas/tests/arrays/sparse/test_array.py | 12 +----- pandas/tests/frame/test_api.py | 6 --- pandas/tests/indexes/period/test_period.py | 3 +- pandas/tests/series/test_api.py | 6 --- 15 files changed, 16 insertions(+), 166 deletions(-) diff --git a/doc/redirects.csv b/doc/redirects.csv index 61902f3134a4d..33ba94e2baa01 100644 --- a/doc/redirects.csv +++ b/doc/redirects.csv @@ -360,7 +360,6 @@ generated/pandas.DataFrame.from_records,../reference/api/pandas.DataFrame.from_r generated/pandas.DataFrame.ge,../reference/api/pandas.DataFrame.ge generated/pandas.DataFrame.get,../reference/api/pandas.DataFrame.get generated/pandas.DataFrame.get_value,../reference/api/pandas.DataFrame.get_value -generated/pandas.DataFrame.get_values,../reference/api/pandas.DataFrame.get_values generated/pandas.DataFrame.groupby,../reference/api/pandas.DataFrame.groupby generated/pandas.DataFrame.gt,../reference/api/pandas.DataFrame.gt generated/pandas.DataFrame.head,../reference/api/pandas.DataFrame.head @@ -643,7 +642,6 @@ generated/pandas.Index.get_level_values,../reference/api/pandas.Index.get_level_ generated/pandas.Index.get_loc,../reference/api/pandas.Index.get_loc generated/pandas.Index.get_slice_bound,../reference/api/pandas.Index.get_slice_bound generated/pandas.Index.get_value,../reference/api/pandas.Index.get_value -generated/pandas.Index.get_values,../reference/api/pandas.Index.get_values generated/pandas.Index.groupby,../reference/api/pandas.Index.groupby generated/pandas.Index.has_duplicates,../reference/api/pandas.Index.has_duplicates generated/pandas.Index.hasnans,../reference/api/pandas.Index.hasnans @@ -1044,7 +1042,6 @@ generated/pandas.Series.from_csv,../reference/api/pandas.Series.from_csv generated/pandas.Series.ge,../reference/api/pandas.Series.ge generated/pandas.Series.get,../reference/api/pandas.Series.get generated/pandas.Series.get_value,../reference/api/pandas.Series.get_value -generated/pandas.Series.get_values,../reference/api/pandas.Series.get_values generated/pandas.Series.groupby,../reference/api/pandas.Series.groupby generated/pandas.Series.gt,../reference/api/pandas.Series.gt generated/pandas.Series.hasnans,../reference/api/pandas.Series.hasnans diff --git a/doc/source/reference/frame.rst b/doc/source/reference/frame.rst index 2604af4e33a89..4c296d74b5ef9 100644 --- a/doc/source/reference/frame.rst +++ b/doc/source/reference/frame.rst @@ -30,7 +30,6 @@ Attributes and underlying data DataFrame.dtypes DataFrame.select_dtypes DataFrame.values - DataFrame.get_values DataFrame.axes DataFrame.ndim DataFrame.size diff --git a/doc/source/reference/indexing.rst b/doc/source/reference/indexing.rst index 23f8424213574..a01f2bcd40612 100644 --- a/doc/source/reference/indexing.rst +++ b/doc/source/reference/indexing.rst @@ -159,7 +159,6 @@ Selecting Index.get_loc Index.get_slice_bound Index.get_value - Index.get_values Index.isin Index.slice_indexer Index.slice_locs diff --git a/doc/source/reference/series.rst b/doc/source/reference/series.rst index e13b4ed98a38b..1e17b72db2aaf 100644 --- a/doc/source/reference/series.rst +++ b/doc/source/reference/series.rst @@ -53,7 +53,6 @@ Conversion Series.to_period Series.to_timestamp Series.to_list - Series.get_values Series.__array__ Indexing, iteration diff --git a/pandas/_libs/reduction.pyx b/pandas/_libs/reduction.pyx index ea54b00cf5be4..0019fc4b36d20 100644 --- a/pandas/_libs/reduction.pyx +++ b/pandas/_libs/reduction.pyx @@ -26,14 +26,6 @@ cdef _check_result_array(object obj, Py_ssize_t cnt): raise ValueError('Function does not reduce') -cdef bint _is_sparse_array(object obj): - # TODO can be removed one SparseArray.values is removed (GH26421) - if hasattr(obj, '_subtyp'): - if obj._subtyp == 'sparse_array': - return True - return False - - cdef class Reducer: """ Performs generic reduction operation on a C or Fortran-contiguous ndarray @@ -404,8 +396,7 @@ cdef class SeriesGrouper(_BaseGrouper): cdef inline _extract_result(object res, bint squeeze=True): """ extract the result object, it might be a 0-dim ndarray or a len-1 0-dim, or a scalar """ - if (not _is_sparse_array(res) and hasattr(res, 'values') - and util.is_array(res.values)): + if hasattr(res, 'values') and util.is_array(res.values): res = res.values if util.is_array(res): if res.ndim == 0: diff --git a/pandas/core/arrays/categorical.py b/pandas/core/arrays/categorical.py index f4a20b808292a..a2e456581cb4f 100644 --- a/pandas/core/arrays/categorical.py +++ b/pandas/core/arrays/categorical.py @@ -302,9 +302,7 @@ class Categorical(ExtensionArray, PandasObject): __array_priority__ = 1000 _dtype = CategoricalDtype(ordered=False) # tolist is not actually deprecated, just suppressed in the __dir__ - _deprecations = PandasObject._deprecations | frozenset( - ["tolist", "itemsize", "get_values"] - ) + _deprecations = PandasObject._deprecations | frozenset(["tolist", "itemsize"]) _typ = "categorical" def __init__( @@ -1461,29 +1459,18 @@ def value_counts(self, dropna=True): return Series(count, index=CategoricalIndex(ix), dtype="int64") - def get_values(self): + def _internal_get_values(self): """ Return the values. - .. deprecated:: 0.25.0 - For internal compatibility with pandas formatting. Returns ------- - numpy.array + np.ndarray or Index A numpy array of the same dtype as categorical.categories.dtype or Index if datetime / periods. """ - warn( - "The 'get_values' method is deprecated and will be removed in a " - "future version", - FutureWarning, - stacklevel=2, - ) - return self._internal_get_values() - - def _internal_get_values(self): # if we are a datetime and period index, return Index to keep metadata if needs_i8_conversion(self.categories): return self.categories.take(self._codes, fill_value=np.nan) diff --git a/pandas/core/arrays/sparse/array.py b/pandas/core/arrays/sparse/array.py index 593ba7a643193..e909e92139c80 100644 --- a/pandas/core/arrays/sparse/array.py +++ b/pandas/core/arrays/sparse/array.py @@ -568,23 +568,6 @@ def npoints(self) -> int: """ return self.sp_index.npoints - @property - def values(self): - """ - Dense values - - .. deprecated:: 0.25.0 - - Use ``np.asarray(...)`` or the ``.to_dense()`` method instead. - """ - msg = ( - "The SparseArray.values attribute is deprecated and will be " - "removed in a future version. You can use `np.asarray(...)` or " - "the `.to_dense()` method instead." - ) - warnings.warn(msg, FutureWarning, stacklevel=2) - return self.to_dense() - def isna(self): # If null fill value, we want SparseDtype[bool, true] # to preserve the same memory usage. @@ -1137,22 +1120,6 @@ def to_dense(self): """ return np.asarray(self, dtype=self.sp_values.dtype) - def get_values(self): - """ - Convert SparseArray to a NumPy array. - - .. deprecated:: 0.25.0 - Use `to_dense` instead. - - """ - warnings.warn( - "The 'get_values' method is deprecated and will be removed in a " - "future version. Use the 'to_dense' method instead.", - FutureWarning, - stacklevel=2, - ) - return self._internal_get_values() - _internal_get_values = to_dense # ------------------------------------------------------------------------ diff --git a/pandas/core/generic.py b/pandas/core/generic.py index c52b1c65ad08d..de9f38e28c86a 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -5506,13 +5506,10 @@ def _get_values(self): # compat return self.values - def get_values(self): + def _internal_get_values(self): """ Return an ndarray after converting sparse values to dense. - .. deprecated:: 0.25.0 - Use ``np.asarray(..)`` or :meth:`DataFrame.values` instead. - This is the same as ``.values`` for non-sparse data. For sparse data contained in a `SparseArray`, the data are first converted to a dense representation. @@ -5526,41 +5523,7 @@ def get_values(self): -------- values : Numpy representation of DataFrame. SparseArray : Container for sparse data. - - Examples - -------- - >>> df = pd.DataFrame({'a': [1, 2], 'b': [True, False], - ... 'c': [1.0, 2.0]}) - >>> df - a b c - 0 1 True 1.0 - 1 2 False 2.0 - - >>> df.get_values() - array([[1, True, 1.0], [2, False, 2.0]], dtype=object) - - >>> df = pd.DataFrame({"a": pd.SparseArray([1, None, None]), - ... "c": [1.0, 2.0, 3.0]}) - >>> df - a c - 0 1.0 1.0 - 1 NaN 2.0 - 2 NaN 3.0 - - >>> df.get_values() - array([[ 1., 1.], - [nan, 2.], - [nan, 3.]]) - """ - warnings.warn( - "The 'get_values' method is deprecated and will be removed in a " - "future version. Use '.values' or 'np.asarray(..)' instead.", - FutureWarning, - stacklevel=2, - ) - return self._internal_get_values() - - def _internal_get_values(self): + """ return self.values @property diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index 87c110f95c13a..cb6e3b172c5c9 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -215,7 +215,7 @@ class Index(IndexOpsMixin, PandasObject): _deprecations: FrozenSet[str] = ( PandasObject._deprecations | IndexOpsMixin._deprecations - | frozenset(["contains", "get_values", "set_value"]) + | frozenset(["contains", "set_value"]) ) # To hand over control to subclasses @@ -3753,13 +3753,10 @@ def _values(self) -> Union[ExtensionArray, ABCIndexClass, np.ndarray]: """ return self._data - def get_values(self): + def _internal_get_values(self): """ Return `Index` data as an `numpy.ndarray`. - .. deprecated:: 0.25.0 - Use :meth:`Index.to_numpy` or :attr:`Index.array` instead. - Returns ------- numpy.ndarray @@ -3767,7 +3764,7 @@ def get_values(self): See Also -------- - Index.values : The attribute that get_values wraps. + Index.values : The attribute that _internal_get_values wraps. Examples -------- @@ -3780,33 +3777,24 @@ def get_values(self): a 1 2 3 b 4 5 6 c 7 8 9 - >>> df.index.get_values() + >>> df.index._internal_get_values() array(['a', 'b', 'c'], dtype=object) Standalone `Index` values: >>> idx = pd.Index(['1', '2', '3']) - >>> idx.get_values() + >>> idx._internal_get_values() array(['1', '2', '3'], dtype=object) `MultiIndex` arrays also have only one dimension: >>> midx = pd.MultiIndex.from_arrays([[1, 2, 3], ['a', 'b', 'c']], ... names=('number', 'letter')) - >>> midx.get_values() + >>> midx._internal_get_values() array([(1, 'a'), (2, 'b'), (3, 'c')], dtype=object) - >>> midx.get_values().ndim + >>> midx._internal_get_values().ndim 1 """ - warnings.warn( - "The 'get_values' method is deprecated and will be removed in a " - "future version. Use '.to_numpy()' or '.array' instead.", - FutureWarning, - stacklevel=2, - ) - return self._internal_get_values() - - def _internal_get_values(self): return self.values @Appender(IndexOpsMixin.memory_usage.__doc__) diff --git a/pandas/core/series.py b/pandas/core/series.py index 410b10a69ecd5..1bfd5c047ed76 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -465,27 +465,16 @@ def _values(self): """ return self._data.internal_values() - def get_values(self): + def _internal_get_values(self): """ Same as values (but handles sparseness conversions); is a view. - .. deprecated:: 0.25.0 - Use :meth:`Series.to_numpy` or :attr:`Series.array` instead. - Returns ------- numpy.ndarray Data of the Series. """ - warnings.warn( - "The 'get_values' method is deprecated and will be removed in a " - "future version. Use '.to_numpy()' or '.array' instead.", - FutureWarning, - stacklevel=2, - ) - return self._internal_get_values() - def _internal_get_values(self): return self._data.get_values() # ops diff --git a/pandas/tests/arrays/categorical/test_api.py b/pandas/tests/arrays/categorical/test_api.py index 42087b89a19b5..c80e963d5c409 100644 --- a/pandas/tests/arrays/categorical/test_api.py +++ b/pandas/tests/arrays/categorical/test_api.py @@ -504,9 +504,3 @@ def test_recode_to_categories_large(self): new = Index(expected) result = _recode_for_categories(codes, old, new) tm.assert_numpy_array_equal(result, expected) - - def test_deprecated_get_values(self): - cat = Categorical(["a", "b", "c", "a"]) - with tm.assert_produces_warning(FutureWarning): - res = cat.get_values() - tm.assert_numpy_array_equal(res, np.array(cat)) diff --git a/pandas/tests/arrays/sparse/test_array.py b/pandas/tests/arrays/sparse/test_array.py index c9f96ed516dc5..75a4f683e92d2 100644 --- a/pandas/tests/arrays/sparse/test_array.py +++ b/pandas/tests/arrays/sparse/test_array.py @@ -617,8 +617,7 @@ def test_dense_repr(self, vals, fill_value): res = arr.to_dense() tm.assert_numpy_array_equal(res, vals) - with tm.assert_produces_warning(FutureWarning): - res2 = arr.get_values() + res2 = arr._internal_get_values() tm.assert_numpy_array_equal(res2, vals) @@ -1244,12 +1243,3 @@ def test_map_missing(): result = arr.map({0: 10, 1: 11}) tm.assert_sp_array_equal(result, expected) - - -def test_deprecated_values(): - arr = SparseArray([0, 1, 2]) - - with tm.assert_produces_warning(FutureWarning): - result = arr.values - - tm.assert_numpy_array_equal(result, arr.to_dense()) diff --git a/pandas/tests/frame/test_api.py b/pandas/tests/frame/test_api.py index 60befe5e73d37..91fb71c9de7a4 100644 --- a/pandas/tests/frame/test_api.py +++ b/pandas/tests/frame/test_api.py @@ -528,9 +528,3 @@ def test_tab_complete_warning(self, ip): with tm.assert_produces_warning(None): with provisionalcompleter("ignore"): list(ip.Completer.completions("df.", 1)) - - def test_get_values_deprecated(self): - df = DataFrame({"a": [1, 2], "b": [0.1, 0.2]}) - with tm.assert_produces_warning(FutureWarning): - res = df.get_values() - tm.assert_numpy_array_equal(res, df.values) diff --git a/pandas/tests/indexes/period/test_period.py b/pandas/tests/indexes/period/test_period.py index 14bf6490a706b..6a3137785e6f3 100644 --- a/pandas/tests/indexes/period/test_period.py +++ b/pandas/tests/indexes/period/test_period.py @@ -180,8 +180,7 @@ def test_values(self): exp = np.array([], dtype=np.object) tm.assert_numpy_array_equal(idx.values, exp) tm.assert_numpy_array_equal(idx.to_numpy(), exp) - with tm.assert_produces_warning(FutureWarning): - tm.assert_numpy_array_equal(idx.get_values(), exp) + exp = np.array([], dtype=np.int64) tm.assert_numpy_array_equal(idx._ndarray_values, exp) diff --git a/pandas/tests/series/test_api.py b/pandas/tests/series/test_api.py index 42b2c37638c76..8acab3fa2541d 100644 --- a/pandas/tests/series/test_api.py +++ b/pandas/tests/series/test_api.py @@ -498,12 +498,6 @@ def test_integer_series_size(self): s = Series(range(9), dtype="Int64") assert s.size == 9 - def test_get_values_deprecation(self): - s = Series(range(9)) - with tm.assert_produces_warning(FutureWarning): - res = s.get_values() - tm.assert_numpy_array_equal(res, s.values) - class TestCategoricalSeries: @pytest.mark.parametrize(