Skip to content

Commit d635fab

Browse files
committed
DEPR: fix VisibleDeprecationWarnings in sparse
1 parent 06cfdca commit d635fab

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

pandas/sparse/array.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,15 @@ def __getitem__(self, key):
283283
if com.is_integer(key):
284284
return self._get_val_at(key)
285285
else:
286-
data_slice = self.values[key]
286+
if isinstance(key, SparseArray):
287+
key = np.asarray(key)
288+
if hasattr(key,'__len__') and len(self) != len(key):
289+
indices = self.sp_index
290+
if hasattr(indices,'to_int_index'):
291+
indices = indices.to_int_index()
292+
data_slice = self.values.take(indices.indices)[key]
293+
else:
294+
data_slice = self.values[key]
287295
return self._constructor(data_slice)
288296

289297
def __getslice__(self, i, j):
@@ -513,7 +521,12 @@ def make_sparse(arr, kind='block', fill_value=nan):
513521
else:
514522
mask = arr != fill_value
515523

516-
indices = np.arange(length, dtype=np.int32)[mask]
524+
length = len(arr)
525+
if length != mask.size:
526+
# the arr is a SparseArray
527+
indices = mask.sp_index.indices
528+
else:
529+
indices = np.arange(length, dtype=np.int32)[mask]
517530

518531
if kind == 'block':
519532
locs, lens = splib.get_blocks(indices)

0 commit comments

Comments
 (0)