Skip to content

Commit 0746e1d

Browse files
committed
API, DOC: Clarify and enforce array to be 1-D during SparseArray construction
Closes gh-12794.
1 parent 8001e15 commit 0746e1d

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

pandas/sparse/array.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -87,11 +87,11 @@ def _wrap_result(name, data, sparse_index, fill_value):
8787

8888

8989
class SparseArray(PandasObject, np.ndarray):
90-
"""Data structure for labeled, sparse floating point data
90+
"""Data structure for labeled, sparse floating point 1-D data
9191
9292
Parameters
9393
----------
94-
data : {array-like, Series, SparseSeries, dict}
94+
data : {array-like (1-D), Series, SparseSeries, dict}
9595
kind : {'block', 'integer'}
9696
fill_value : float
9797
Defaults to NaN (code for missing)
@@ -563,7 +563,9 @@ def make_sparse(arr, kind='block', fill_value=nan):
563563
"""
564564

565565
arr = _sanitize_values(arr)
566-
length = len(arr)
566+
567+
if np.ndim(arr) > 1:
568+
raise TypeError("expected dimension <= 1 data")
567569

568570
if np.isnan(fill_value):
569571
mask = ~np.isnan(arr)

pandas/sparse/tests/test_array.py

+4
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,10 @@ def setslice():
234234
assertRaisesRegexp(TypeError, "item assignment", setitem)
235235
assertRaisesRegexp(TypeError, "item assignment", setslice)
236236

237+
def test_constructor_from_too_large_array(self):
238+
assertRaisesRegexp(TypeError, "expected dimension <= 1 data",
239+
SparseArray, np.arange(10).reshape((2, 5)))
240+
237241
def test_constructor_from_sparse(self):
238242
res = SparseArray(self.zarr)
239243
self.assertEqual(res.fill_value, 0)

0 commit comments

Comments
 (0)