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

API, DOC: Clarify and Enforce Array to be 1-D during SparseArray Init Construction #13011

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions pandas/sparse/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,11 @@ def _wrap_result(name, data, sparse_index, fill_value):


class SparseArray(PandasObject, np.ndarray):
"""Data structure for labeled, sparse floating point data
"""Data structure for labeled, sparse floating point 1-D data

Parameters
----------
data : {array-like, Series, SparseSeries, dict}
data : {array-like (1-D), Series, SparseSeries, dict}
kind : {'block', 'integer'}
fill_value : float
Defaults to NaN (code for missing)
Expand Down Expand Up @@ -563,7 +563,9 @@ def make_sparse(arr, kind='block', fill_value=nan):
"""

arr = _sanitize_values(arr)
length = len(arr)

if np.ndim(arr) > 1:
raise TypeError("expected dimension <= 1 data")

if np.isnan(fill_value):
mask = ~np.isnan(arr)
Expand Down
4 changes: 4 additions & 0 deletions pandas/sparse/tests/test_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,10 @@ def setslice():
assertRaisesRegexp(TypeError, "item assignment", setitem)
assertRaisesRegexp(TypeError, "item assignment", setslice)

def test_constructor_from_too_large_array(self):
assertRaisesRegexp(TypeError, "expected dimension <= 1 data",
SparseArray, np.arange(10).reshape((2, 5)))

def test_constructor_from_sparse(self):
res = SparseArray(self.zarr)
self.assertEqual(res.fill_value, 0)
Expand Down