Skip to content

Fix indexing, reindex on all-sparse SparseArray. #35287

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 5 commits into from
Jul 16, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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: 0 additions & 1 deletion doc/source/whatsnew/v1.1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1130,7 +1130,6 @@ 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`)
- Bug where empty :class:`DataFrame` could not be cast to :class:`SparseDtype` (:issue:`33113`)
- Bug in :meth:`arrays.SparseArray` was returning the incorrect type when indexing a sparse dataframe with an iterable (:issue:`34526`, :issue:`34540`)
Expand Down
5 changes: 1 addition & 4 deletions pandas/core/internals/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -1636,10 +1636,7 @@ def _holder(self):
@property
def fill_value(self):
# Used in reindex_indexer
if is_sparse(self.values):
return self.values.dtype.fill_value
else:
return self.values.dtype.na_value
return self.values.dtype.na_value

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

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

def test_boolean_mask_frame_fill_value(self, data):
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I noticed that these tests were passing regardless of the change, so I'm not sure they were testing what we expected. I've left them removed.

# 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: 0 additions & 5 deletions pandas/tests/extension/test_sparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,6 @@ 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)

Comment on lines -44 to -47
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this was doing anything on master.


@pytest.fixture(params=[0, np.nan])
def data_missing(request):
"""Length 2 array with [NA, Valid]"""
Expand Down
15 changes: 15 additions & 0 deletions pandas/tests/frame/indexing/test_sparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,18 @@ def test_locindexer_from_spmatrix(self, spmatrix_t, dtype):
result = df.loc[itr_idx].dtypes.values
expected = np.full(cols, SparseDtype(dtype, fill_value=0))
tm.assert_numpy_array_equal(result, expected)

def test_reindex(self):
# https://github.com/pandas-dev/pandas/issues/35286
df = pd.DataFrame(
{"A": [0, 1], "B": pd.array([0, 1], dtype=pd.SparseDtype("int64", 0))}
)
result = df.reindex([0, 2])
expected = pd.DataFrame(
{
"A": [0.0, np.nan],
"B": pd.array([0.0, np.nan], dtype=pd.SparseDtype("float64", 0.0)),
},
index=[0, 2],
)
tm.assert_frame_equal(result, expected)