@@ -140,7 +140,7 @@ def __new__(cls, data, sparse_index=None, index=None, kind='integer',
140
140
values , sparse_index = make_sparse (data , kind = kind ,
141
141
fill_value = fill_value )
142
142
else :
143
- values = data
143
+ values = _sanitize_values ( data )
144
144
if len (values ) != sparse_index .npoints :
145
145
raise AssertionError ("Non array-like type {0} must have"
146
146
" the same length as the"
@@ -515,6 +515,33 @@ def _maybe_to_sparse(array):
515
515
return array
516
516
517
517
518
+ def _sanitize_values (arr ):
519
+ """
520
+ return an ndarray for our input,
521
+ in a platform independent manner
522
+ """
523
+
524
+ if hasattr (arr , 'values' ):
525
+ arr = arr .values
526
+ else :
527
+
528
+ # scalar
529
+ if lib .isscalar (arr ):
530
+ arr = [arr ]
531
+
532
+ # ndarray
533
+ if isinstance (arr , np .ndarray ):
534
+ pass
535
+
536
+ elif com .is_list_like (arr ) and len (arr ) > 0 :
537
+ arr = com ._possibly_convert_platform (arr )
538
+
539
+ else :
540
+ arr = np .asarray (arr )
541
+
542
+ return arr
543
+
544
+
518
545
def make_sparse (arr , kind = 'block' , fill_value = nan ):
519
546
"""
520
547
Convert ndarray to sparse format
@@ -529,13 +556,8 @@ def make_sparse(arr, kind='block', fill_value=nan):
529
556
-------
530
557
(sparse_values, index) : (ndarray, SparseIndex)
531
558
"""
532
- if hasattr (arr , 'values' ):
533
- arr = arr .values
534
- else :
535
- if lib .isscalar (arr ):
536
- arr = [arr ]
537
- arr = np .asarray (arr )
538
559
560
+ arr = _sanitize_values (arr )
539
561
length = len (arr )
540
562
541
563
if np .isnan (fill_value ):
0 commit comments