Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DEPR: remove get_values, SparseArray.values #29989

Merged
merged 10 commits into from
Dec 4, 2019
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
22 changes: 0 additions & 22 deletions pandas/core/arrays/categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -1461,28 +1461,6 @@ def value_counts(self, dropna=True):

return Series(count, index=CategoricalIndex(ix), dtype="int64")

def get_values(self):
"""
Return the values.

.. deprecated:: 0.25.0

For internal compatibility with pandas formatting.

Returns
-------
numpy.array
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):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add part of the above docstring to this method? (the interesting parts for internal use, eg what it returns)

(and potentially the same comment for some of the other cases below)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good idea

# if we are a datetime and period index, return Index to keep metadata
if needs_i8_conversion(self.categories):
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
54 changes: 0 additions & 54 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -5470,60 +5470,6 @@ def _get_values(self):
# compat
return self.values

def 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.

Returns
-------
numpy.ndarray
Numpy representation of DataFrame.

See Also
--------
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

Expand Down
55 changes: 1 addition & 54 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,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 @@ -3814,59 +3814,6 @@ def _values(self) -> Union[ExtensionArray, ABCIndexClass, np.ndarray]:
"""
return self._data

def 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.

Examples
--------
Getting the `Index` values of a `DataFrame`:

>>> df = pd.DataFrame([[1, 2, 3], [4, 5, 6], [7, 8, 9]],
... index=['a', 'b', 'c'], columns=['A', 'B', 'C'])
>>> df
A B C
a 1 2 3
b 4 5 6
c 7 8 9
>>> df.index.get_values()
array(['a', 'b', 'c'], dtype=object)

Standalone `Index` values:

>>> idx = pd.Index(['1', '2', '3'])
>>> idx.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()
array([(1, 'a'), (2, 'b'), (3, 'c')], dtype=object)
>>> midx.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

Expand Down
20 changes: 0 additions & 20 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -465,26 +465,6 @@ def _values(self):
"""
return self._data.internal_values()

def 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()

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 @@ -563,9 +563,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 @@ -516,12 +516,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