diff --git a/pandas/sparse/array.py b/pandas/sparse/array.py index a96663d757e74..486dbaaa624d9 100644 --- a/pandas/sparse/array.py +++ b/pandas/sparse/array.py @@ -140,7 +140,7 @@ def __new__(cls, data, sparse_index=None, index=None, kind='integer', values, sparse_index = make_sparse(data, kind=kind, fill_value=fill_value) else: - values = data + values = _sanitize_values(data) if len(values) != sparse_index.npoints: raise AssertionError("Non array-like type {0} must have" " the same length as the" @@ -515,6 +515,33 @@ 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 lib.isscalar(arr): + arr = [arr] + + # ndarray + if isinstance(arr, np.ndarray): + pass + + elif com.is_list_like(arr) and len(arr) > 0: + arr = com._possibly_convert_platform(arr) + + else: + arr = np.asarray(arr) + + return arr + + def make_sparse(arr, kind='block', fill_value=nan): """ Convert ndarray to sparse format @@ -529,13 +556,8 @@ def make_sparse(arr, kind='block', fill_value=nan): ------- (sparse_values, index) : (ndarray, SparseIndex) """ - if hasattr(arr, 'values'): - arr = arr.values - else: - if lib.isscalar(arr): - arr = [arr] - arr = np.asarray(arr) + arr = _sanitize_values(arr) length = len(arr) if np.isnan(fill_value): diff --git a/pandas/sparse/tests/test_array.py b/pandas/sparse/tests/test_array.py index 7f76c079e17b3..1786123191866 100644 --- a/pandas/sparse/tests/test_array.py +++ b/pandas/sparse/tests/test_array.py @@ -76,6 +76,15 @@ def test_constructor_spindex_dtype(self): self.assertEqual(arr.dtype, np.int64) self.assertTrue(np.isnan(arr.fill_value)) + # scalar input + arr = SparseArray(data=1, + sparse_index=IntIndex(1, [0]), + dtype=None) + exp = SparseArray([1], dtype=None) + tm.assert_sp_array_equal(arr, exp) + self.assertEqual(arr.dtype, np.int64) + self.assertTrue(np.isnan(arr.fill_value)) + arr = SparseArray(data=[1, 2], sparse_index=IntIndex(4, [1, 2]), fill_value=0, dtype=None) exp = SparseArray([0, 1, 2, 0], fill_value=0, dtype=None)