8000 DEPR: deprecate SparseArray.values by jorisvandenbossche · Pull Request #26421 · pandas-dev/pandas · GitHub
[go: up one dir, main page]

Skip to content

DEPR: deprecate SparseArray.values #26421

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

Merged
Prev Previous commit
Next Next commit
fix json warning
  • Loading branch information
jorisvandenbossche committed May 17, 2019
commit 09d5122d7abfbdd702df167ad0be15c7c020ecd3
26 changes: 23 additions & 3 deletions pandas/_libs/src/ujson/python/objToJSON.c
Original file line number Diff line number Diff line change
Expand Up @@ -210,17 +210,37 @@ static TypeContext *createTypeContext(void) {
return pc;
}


static int is_sparse_array(PyObject *obj) {
// TODO can be removed again once SparseArray.values is removed (GH26421)
if (PyObject_HasAttrString(obj, "_subtyp")) {
PyObject *_subtype = PyObject_GetAttrString(obj, "_subtyp");
PyObject *sparse_array = PyUnicode_FromString("sparse_array");
int ret = PyUnicode_Compare(_subtype, sparse_array);

if (ret == 0) {
return 1;
}
}
return 0;
}


static PyObject *get_values(PyObject *obj) {
PyObject *values = PyObject_GetAttrString(obj, "values");
PRINTMARK();
PyObject *values = NULL;

if (!is_sparse_array(obj)) {
values = PyObject_GetAttrString(obj, "values");
PRINTMARK();
}

if (values && !PyArray_CheckExact(values)) {

if (PyObject_HasAttrString(values, "to_numpy")) {
values = PyObject_CallMethod(values, "to_numpy", NULL);
}

if (PyObject_HasAttrString(values, "values")) {
if (!is_sparse_array(values) && PyObject_HasAttrString(values, "values")) {
PyObject *subvals = get_values(values);
PyErr_Clear();
PRINTMARK();
Expand Down
33 changes: 3 additions & 30 deletions pandas/core/arrays/sparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -896,8 +896,8 @@ def values(self):
Use ``np.asarray(...)`` or the ``.to_dense()`` method instead.
"""
msg = (
"The 'values' attribute of a SparseArray is deprecated and will "
"be removed in a future version. You can use `np.asarray(...)` or "
"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()
Expand Down Expand Up @@ -1867,33 +1867,6 @@ def _maybe_to_sparse(array):
return array


def _sanitize_values(arr):
"""
return an ndarray for our input,
in a platform independent manner
"""

if hasattr(arr, 'values'):
arr = arr.values
else:

# scalar
if is_scalar(arr):
arr = [arr]

# ndarray
if isinstance(arr, np.ndarray):
pass

elif is_list_like(arr) and len(arr) > 0:
arr = maybe_convert_platform(arr)

else:
arr = np.asarray(arr)

return arr


def make_sparse(arr, kind='block', fill_value=None, dtype=None, copy=False):
"""
Convert ndarray to sparse format
Expand All @@ -1911,7 +1884,7 @@ def make_sparse(arr, kind='block', fill_value=None, dtype=None, copy=False):
(sparse_values, index, fill_value) : (ndarray, SparseIndex, Scalar)
"""

arr = _sanitize_values(arr)
arr = com.values_from_object(arr)

if arr.ndim > 1:
raise TypeError("expected dimension <= 1 data")
Expand Down
3 changes: 3 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ markers =
doctest_optionflags = NORMALIZE_WHITESPACE IGNORE_EXCEPTION_DETAIL
addopts = --strict-data-files
xfail_strict = True
# filterwarnings =
# error:The SparseArray:FutureWarning


[coverage:run]
branch = False
Expand Down
0