Skip to content

Commit

Permalink
DEPR: remove get_values, SparseArray.values (pandas-dev#29989)
Browse files Browse the repository at this point in the history
  • Loading branch information
jbrockmendel authored and proost committed Dec 19, 2019
1 parent eaa5943 commit 84b2e35
Show file tree
Hide file tree
Showing 15 changed files with 16 additions and 166 deletions.
3 changes: 0 additions & 3 deletions doc/redirects.csv
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion doc/source/reference/frame.rst
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ Attributes and underlying data
DataFrame.dtypes
DataFrame.select_dtypes
DataFrame.values
DataFrame.get_values
DataFrame.axes
DataFrame.ndim
DataFrame.size
Expand Down
1 change: 0 additions & 1 deletion doc/source/reference/indexing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion doc/source/reference/series.rst
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ Conversion
Series.to_period
Series.to_timestamp
Series.to_list
Series.get_values
Series.__array__

Indexing, iteration
Expand Down
11 changes: 1 addition & 10 deletions pandas/_libs/reduction.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down
19 changes: 3 additions & 16 deletions pandas/core/arrays/categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__(
Expand Down Expand Up @@ -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)
Expand Down
33 changes: 0 additions & 33 deletions pandas/core/arrays/sparse/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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

# ------------------------------------------------------------------------
Expand Down
41 changes: 2 additions & 39 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand Down
26 changes: 7 additions & 19 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -3753,21 +3753,18 @@ 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
A one-dimensional numpy array of the `Index` values.
See Also
--------
Index.values : The attribute that get_values wraps.
Index.values : The attribute that _internal_get_values wraps.
Examples
--------
Expand All @@ -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__)
Expand Down
13 changes: 1 addition & 12 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 0 additions & 6 deletions pandas/tests/arrays/categorical/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
12 changes: 1 addition & 11 deletions pandas/tests/arrays/sparse/test_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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())
6 changes: 0 additions & 6 deletions pandas/tests/frame/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
3 changes: 1 addition & 2 deletions pandas/tests/indexes/period/test_period.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
6 changes: 0 additions & 6 deletions pandas/tests/series/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit 84b2e35

Please sign in to comment.