Skip to content

REGR: use dtype.fill_value in ExtensionBlock.fill_value where available #34158

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -867,6 +867,7 @@ Sparse
- Creating a :class:`SparseArray` from timezone-aware dtype will issue a warning before dropping timezone information, instead of doing so silently (:issue:`32501`)
- Bug in :meth:`arrays.SparseArray.from_spmatrix` wrongly read scipy sparse matrix (:issue:`31991`)
- Bug in :meth:`Series.sum` with ``SparseArray`` raises ``TypeError`` (:issue:`25777`)
- Bug where :class:`DataFrame` containing :class:`SparseArray` filled with ``NaN`` when indexed by a list-like (:issue:`27781`, :issue:`29563`)
- The repr of :class:`SparseDtype` now includes the repr of its ``fill_value`` attribute. Previously it used ``fill_value``'s string representation (:issue:`34352`)

ExtensionArray
Expand Down
5 changes: 4 additions & 1 deletion pandas/core/internals/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -1634,7 +1634,10 @@ def _holder(self):
@property
def fill_value(self):
# Used in reindex_indexer
return self.values.dtype.na_value
if is_sparse(self.values):
return self.values.dtype.fill_value
else:
return self.values.dtype.na_value

@property
def _can_hold_na(self):
Expand Down
28 changes: 28 additions & 0 deletions pandas/tests/extension/base/getitem.py
Original file line number Diff line number Diff line change
Expand Up @@ -399,3 +399,31 @@ def test_item(self, data):

with pytest.raises(ValueError, match=msg):
s.item()

def test_boolean_mask_frame_fill_value(self, data):
# https://github.com/pandas-dev/pandas/issues/27781
df = pd.DataFrame({"A": data})

mask = np.random.choice([True, False], df.shape[0])
result = pd.isna(df.iloc[mask]["A"])
expected = pd.isna(df["A"].iloc[mask])
self.assert_series_equal(result, expected)

mask = pd.Series(mask, index=df.index)
result = pd.isna(df.loc[mask]["A"])
expected = pd.isna(df["A"].loc[mask])
self.assert_series_equal(result, expected)

def test_fancy_index_frame_fill_value(self, data):
# https://github.com/pandas-dev/pandas/issues/29563
df = pd.DataFrame({"A": data})

mask = np.random.choice(df.shape[0], df.shape[0])
result = pd.isna(df.iloc[mask]["A"])
expected = pd.isna(df["A"].iloc[mask])
self.assert_series_equal(result, expected)

mask = pd.Series(mask, index=df.index)
result = pd.isna(df.loc[mask]["A"])
expected = pd.isna(df["A"].loc[mask])
self.assert_series_equal(result, expected)
5 changes: 5 additions & 0 deletions pandas/tests/extension/test_sparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ def data_for_twos(request):
return SparseArray(np.ones(100) * 2)


@pytest.fixture(params=[0, np.nan])
def data_zeros(request):
return SparseArray(np.zeros(100, dtype=int), fill_value=request.param)


@pytest.fixture(params=[0, np.nan])
def data_missing(request):
"""Length 2 array with [NA, Valid]"""
Expand Down