Skip to content

Commit a4744ea

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

File tree

3 files changed

+39
-2
lines changed

3 files changed

+39
-2
lines changed

doc/source/whatsnew/v1.1.0.rst

+2-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+
- Fixed bug where :class:`DataFrame` containing :class:`SparseArray` filled with ``NaN`` when indexed by a list-like (:issue:`27781`, :issue:`29563`)
847847

848848
ExtensionArray
849849
^^^^^^^^^^^^^^
@@ -856,6 +856,7 @@ ExtensionArray
856856
- Fixed bug where :meth:`StringArray.memory_usage` was not implemented (:issue:`33963`)
857857
- Fixed bug where :meth:`DataFrameGroupBy` would ignore the ``min_count`` argument for aggregations on nullable boolean dtypes (:issue:`34051`)
858858
- Fixed bug that `DataFrame(columns=.., dtype='string')` would fail (:issue:`27953`, :issue:`33623`)
859+
- Fixed bug in :meth:`ExtensionBlock.fill_value` which did not use ``dtype.fill_value`` where available (:issue:`27781`, :issue:`29563`)
859860

860861
Other
861862
^^^^^

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+
try:
1631+
return self.values.dtype.fill_value
1632+
except AttributeError:
1633+
return self.values.dtype.na_value
16311634

16321635
@property
16331636
def _can_hold_na(self):

pandas/tests/extension/test_sparse.py

+33
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]"""
@@ -169,6 +174,34 @@ def test_reindex(self, data, na_value):
169174
self._check_unsupported(data)
170175
super().test_reindex(data, na_value)
171176

177+
def test_boolean_mask_frame_fill_value(self, data):
178+
# https://github.com/pandas-dev/pandas/issues/27781
179+
df = pd.DataFrame({"A": data})
180+
181+
mask = np.random.choice([True, False], df.shape[0])
182+
result = pd.isna(df.iloc[mask]["A"])
183+
expected = pd.isna(df["A"].iloc[mask])
184+
self.assert_series_equal(result, expected)
185+
186+
mask = pd.Series(mask, index=df.index)
187+
result = pd.isna(df.loc[mask]["A"])
188+
expected = pd.isna(df["A"].loc[mask])
189+
self.assert_series_equal(result, expected)
190+
191+
def test_fancy_index_frame_fill_value(self, data):
192+
# https://github.com/pandas-dev/pandas/issues/27781
193+
df = pd.DataFrame({"A": data})
194+
195+
mask = np.random.choice(df.shape[0], df.shape[0])
196+
result = pd.isna(df.iloc[mask]["A"])
197+
expected = pd.isna(df["A"].iloc[mask])
198+
self.assert_series_equal(result, expected)
199+
200+
mask = pd.Series(mask, index=df.index)
201+
result = pd.isna(df.loc[mask]["A"])
202+
expected = pd.isna(df["A"].loc[mask])
203+
self.assert_series_equal(result, expected)
204+
172205

173206
# Skipping TestSetitem, since we don't implement it.
174207

0 commit comments

Comments
 (0)