From b13ddd57f6f88f75d987dd84d208a2329f1d94bb Mon Sep 17 00:00:00 2001 From: gfyoung Date: Thu, 28 Apr 2016 10:02:35 -0400 Subject: [PATCH] API, DOC: Clarify and Enforce Array to be 1-D during SparseArray Init Construction closes #12794 Author: gfyoung Closes #13011 from gfyoung/sparse-array-clarify and squashes the following commits: 0746e1d [gfyoung] API, DOC: Clarify and enforce array to be 1-D during SparseArray construction --- pandas/sparse/array.py | 8 +++++--- pandas/sparse/tests/test_array.py | 4 ++++ 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/pandas/sparse/array.py b/pandas/sparse/array.py index b080f2eb45a90..ff7bd81953292 100644 --- a/pandas/sparse/array.py +++ b/pandas/sparse/array.py @@ -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) @@ -563,7 +563,9 @@ def make_sparse(arr, kind='block', fill_value=nan): """ arr = _sanitize_values(arr) - length = len(arr) + + if arr.ndim > 1: + raise TypeError("expected dimension <= 1 data") if np.isnan(fill_value): mask = ~np.isnan(arr) diff --git a/pandas/sparse/tests/test_array.py b/pandas/sparse/tests/test_array.py index 2a905597c7fa0..b45cdc038a70d 100644 --- a/pandas/sparse/tests/test_array.py +++ b/pandas/sparse/tests/test_array.py @@ -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)