Skip to content

Commit 484ec01

Browse files
authored
BUG: Keep original values when taking with a new fill value (#55929)
* Keep original values when taking with a new fill value * Switch one-letter name * Fix whatsnew
1 parent 02a2720 commit 484ec01

File tree

3 files changed

+13
-5
lines changed

3 files changed

+13
-5
lines changed

doc/source/whatsnew/v2.2.0.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -467,7 +467,7 @@ Reshaping
467467

468468
Sparse
469469
^^^^^^
470-
-
470+
- Bug in :meth:`SparseArray.take` when using a different fill value than the array's fill value (:issue:`55181`)
471471
-
472472

473473
ExtensionArray

pandas/core/arrays/sparse/array.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1086,9 +1086,10 @@ def _take_with_fill(self, indices, fill_value=None) -> np.ndarray:
10861086
)
10871087

10881088
elif self.sp_index.npoints == 0:
1089-
# Avoid taking from the empty self.sp_values
1089+
# Use the old fill_value unless we took for an index of -1
10901090
_dtype = np.result_type(self.dtype.subtype, type(fill_value))
10911091
taken = np.full(sp_indexer.shape, fill_value=fill_value, dtype=_dtype)
1092+
taken[old_fill_indices] = self.fill_value
10921093
else:
10931094
taken = self.sp_values.take(sp_indexer)
10941095

pandas/tests/arrays/sparse/test_indexing.py

+10-3
Original file line numberDiff line numberDiff line change
@@ -166,9 +166,16 @@ def test_take(self, arr_data, arr):
166166
tm.assert_sp_array_equal(arr.take([0, 1, 2]), exp)
167167

168168
def test_take_all_empty(self):
169-
a = pd.array([0, 0], dtype=SparseDtype("int64"))
170-
result = a.take([0, 1], allow_fill=True, fill_value=np.nan)
171-
tm.assert_sp_array_equal(a, result)
169+
sparse = pd.array([0, 0], dtype=SparseDtype("int64"))
170+
result = sparse.take([0, 1], allow_fill=True, fill_value=np.nan)
171+
tm.assert_sp_array_equal(sparse, result)
172+
173+
def test_take_different_fill_value(self):
174+
# Take with a different fill value shouldn't overwrite the original
175+
sparse = pd.array([0.0], dtype=SparseDtype("float64", fill_value=0.0))
176+
result = sparse.take([0, -1], allow_fill=True, fill_value=np.nan)
177+
expected = pd.array([0, np.nan], dtype=sparse.dtype)
178+
tm.assert_sp_array_equal(expected, result)
172179

173180
def test_take_fill_value(self):
174181
data = np.array([1, np.nan, 0, 3, 0])

0 commit comments

Comments
 (0)