Skip to content

Commit

Permalink
Remove deprecation of fill_value for SparseDType[bool](pandas-dev#44955)
Browse files Browse the repository at this point in the history
  • Loading branch information
bdrum committed Dec 21, 2021
1 parent 2669c97 commit 4f58c8a
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 13 deletions.
2 changes: 1 addition & 1 deletion asv_bench/benchmarks/sparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ def setup(self, fill_value):
fv_inds = np.unique(
np.random.randint(low=0, high=N - 1, size=int(N * d), dtype=np.int32)
)
b_arr[fv_inds] = not fill_value
b_arr[fv_inds] = True if pd.isna(fill_value) else not fill_value
self.sp_b_arr = SparseArray(b_arr, dtype=np.bool8, fill_value=fill_value)

def time_mask(self, fill_value):
Expand Down
1 change: 0 additions & 1 deletion doc/source/whatsnew/v1.4.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -545,7 +545,6 @@ Other Deprecations
- A deprecation warning is now shown for :meth:`DataFrame.to_latex` indicating the arguments signature may change and emulate more the arguments to :meth:`.Styler.to_latex` in future versions (:issue:`44411`)
- Deprecated :meth:`Categorical.replace`, use :meth:`Series.replace` instead (:issue:`44929`)
- Deprecated :meth:`Index.__getitem__` with a bool key; use ``index.values[key]`` to get the old behavior (:issue:`44051`)
- Deprecated direct passing non boolean or non nan value to ``fill_value`` for :class:`SparseDType` when dtype is bool type (:pull:`44955`)
-

.. ---------------------------------------------------------------------------
Expand Down
20 changes: 12 additions & 8 deletions pandas/core/arrays/sparse/dtype.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
)
from pandas.core.dtypes.cast import astype_nansafe
from pandas.core.dtypes.common import (
is_bool,
is_bool_dtype,
is_object_dtype,
is_scalar,
Expand Down Expand Up @@ -154,13 +153,18 @@ def _check_fill_value(self):
raise ValueError(
f"fill_value must be a scalar. Got {self._fill_value} instead"
)
if self._is_boolean and not (
is_bool(self._fill_value) or isna(self._fill_value)
):
raise ValueError(
"fill_value must be True, False or nan "
f"for boolean type. Got {self._fill_value} instead"
)
# TODO: Right now we can use Sparse boolean array
# with any fill_value. Here was an attempt
# to allow only 3 value: True, False or nan
# but plenty test has failed.
# see pull 44955
# if self._is_boolean and not (
# is_bool(self._fill_value) or isna(self._fill_value)
# ):
# raise ValueError(
# "fill_value must be True, False or nan "
# f"for boolean type. Got {self._fill_value} instead"
# )

@property
def _is_na_fill_value(self) -> bool:
Expand Down
8 changes: 5 additions & 3 deletions pandas/tests/arrays/sparse/test_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -625,9 +625,11 @@ def test_set_fill_value(self):
assert arr.fill_value

# coerces to bool
msg = "fill_value must be True, False or nan"
with pytest.raises(ValueError, match=msg):
arr.fill_value = 0
# XXX: we can construct an sparse array of bool
# type and use as fill_value any value
# msg = "fill_value must be True, False or nan"
# with pytest.raises(ValueError, match=msg):
# arr.fill_value = 0

# msg = "unable to set fill_value nan to bool dtype"
# with pytest.raises(ValueError, match=msg):
Expand Down

0 comments on commit 4f58c8a

Please sign in to comment.