Skip to content

Commit e8d7648

Browse files
committed
patch ExtensionBlock fill_value, closes #27781, closes #29563
1 parent 23d141e commit e8d7648

File tree

4 files changed

+38
-2
lines changed

4 files changed

+38
-2
lines changed

doc/source/whatsnew/v1.1.0.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -843,7 +843,7 @@ Sparse
843843
^^^^^^
844844
- Creating a :class:`SparseArray` from timezone-aware dtype will issue a warning before dropping timezone information, instead of doing so silently (:issue:`32501`)
845845
- Bug in :meth:`arrays.SparseArray.from_spmatrix` wrongly read scipy sparse matrix (:issue:`31991`)
846-
-
846+
- Bug where :class:`DataFrame` containing :class:`SparseArray` filled with ``NaN`` when indexed by a list-like (:issue:`27781`, :issue:`29563`)
847847

848848
ExtensionArray
849849
^^^^^^^^^^^^^^

pandas/core/internals/blocks.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -1627,7 +1627,10 @@ def _holder(self):
16271627
@property
16281628
def fill_value(self):
16291629
# Used in reindex_indexer
1630-
return self.values.dtype.na_value
1630+
if is_sparse(self.values):
1631+
return self.values.dtype.fill_value
1632+
else:
1633+
return self.values.dtype.na_value
16311634

16321635
@property
16331636
def _can_hold_na(self):

pandas/tests/extension/base/getitem.py

+28
Original file line numberDiff line numberDiff line change
@@ -399,3 +399,31 @@ def test_item(self, data):
399399

400400
with pytest.raises(ValueError, match=msg):
401401
s.item()
402+
403+
def test_boolean_mask_frame_fill_value(self, data):
404+
# https://github.com/pandas-dev/pandas/issues/27781
405+
df = pd.DataFrame({"A": data})
406+
407+
mask = np.random.choice([True, False], df.shape[0])
408+
result = pd.isna(df.iloc[mask]["A"])
409+
expected = pd.isna(df["A"].iloc[mask])
410+
self.assert_series_equal(result, expected)
411+
412+
mask = pd.Series(mask, index=df.index)
413+
result = pd.isna(df.loc[mask]["A"])
414+
expected = pd.isna(df["A"].loc[mask])
415+
self.assert_series_equal(result, expected)
416+
417+
def test_fancy_index_frame_fill_value(self, data):
418+
# https://github.com/pandas-dev/pandas/issues/29563
419+
df = pd.DataFrame({"A": data})
420+
421+
mask = np.random.choice(df.shape[0], df.shape[0])
422+
result = pd.isna(df.iloc[mask]["A"])
423+
expected = pd.isna(df["A"].iloc[mask])
424+
self.assert_series_equal(result, expected)
425+
426+
mask = pd.Series(mask, index=df.index)
427+
result = pd.isna(df.loc[mask]["A"])
428+
expected = pd.isna(df["A"].loc[mask])
429+
self.assert_series_equal(result, expected)

pandas/tests/extension/test_sparse.py

+5
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ def data_for_twos(request):
4141
return SparseArray(np.ones(100) * 2)
4242

4343

44+
@pytest.fixture(params=[0, np.nan])
45+
def data_zeros(request):
46+
return SparseArray(np.zeros(100, dtype=int), fill_value=request.param)
47+
48+
4449
@pytest.fixture(params=[0, np.nan])
4550
def data_missing(request):
4651
"""Length 2 array with [NA, Valid]"""

0 commit comments

Comments
 (0)