Skip to content

Commit 959a37c

Browse files
jbrockmendelproost
authored andcommitted
DEPR: CategoricalBlock.where casting to object (pandas-dev#29913)
1 parent cff32e7 commit 959a37c

File tree

3 files changed

+7
-42
lines changed

3 files changed

+7
-42
lines changed

doc/source/whatsnew/v1.0.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,7 @@ or ``matplotlib.Axes.plot``. See :ref:`plotting.formatters` for more.
418418
- Removed :meth:`DataFrame.as_blocks`, :meth:`Series.as_blocks`, `DataFrame.blocks`, :meth:`Series.blocks` (:issue:`17656`)
419419
- :meth:`pandas.Series.str.cat` now defaults to aligning ``others``, using ``join='left'`` (:issue:`27611`)
420420
- :meth:`pandas.Series.str.cat` does not accept list-likes *within* list-likes anymore (:issue:`27611`)
421+
- :meth:`Series.where` with ``Categorical`` dtype (or :meth:`DataFrame.where` with ``Categorical`` column) no longer allows setting new categories (:issue:`24114`)
421422
- :func:`core.internals.blocks.make_block` no longer accepts the "fastpath" keyword(:issue:`19265`)
422423
- :meth:`Block.make_block_same_class` no longer accepts the "dtype" keyword(:issue:`19434`)
423424
- Removed the previously deprecated :meth:`ExtensionArray._formatting_values`. Use :attr:`ExtensionArray._formatter` instead. (:issue:`23601`)

pandas/core/internals/blocks.py

-31
Original file line numberDiff line numberDiff line change
@@ -2887,37 +2887,6 @@ def concat_same_type(self, to_concat, placement=None):
28872887
values, placement=placement or slice(0, len(values), 1), ndim=self.ndim
28882888
)
28892889

2890-
def where(
2891-
self,
2892-
other,
2893-
cond,
2894-
align=True,
2895-
errors="raise",
2896-
try_cast: bool = False,
2897-
axis: int = 0,
2898-
) -> List["Block"]:
2899-
# TODO(CategoricalBlock.where):
2900-
# This can all be deleted in favor of ExtensionBlock.where once
2901-
# we enforce the deprecation.
2902-
object_msg = (
2903-
"Implicitly converting categorical to object-dtype ndarray. "
2904-
"One or more of the values in 'other' are not present in this "
2905-
"categorical's categories. A future version of pandas will raise "
2906-
"a ValueError when 'other' contains different categories.\n\n"
2907-
"To preserve the current behavior, add the new categories to "
2908-
"the categorical before calling 'where', or convert the "
2909-
"categorical to a different dtype."
2910-
)
2911-
try:
2912-
# Attempt to do preserve categorical dtype.
2913-
result = super().where(other, cond, align, errors, try_cast, axis)
2914-
except (TypeError, ValueError):
2915-
warnings.warn(object_msg, FutureWarning, stacklevel=6)
2916-
result = self.astype(object).where(
2917-
other, cond, align=align, errors=errors, try_cast=try_cast, axis=axis
2918-
)
2919-
return result
2920-
29212890
def replace(
29222891
self,
29232892
to_replace,

pandas/tests/arrays/categorical/test_indexing.py

+6-11
Original file line numberDiff line numberDiff line change
@@ -206,13 +206,11 @@ def test_where_other_categorical(self):
206206
expected = pd.Series(Categorical(["a", "c", "c"], dtype=ser.dtype))
207207
tm.assert_series_equal(result, expected)
208208

209-
def test_where_warns(self):
209+
def test_where_new_category_raises(self):
210210
ser = pd.Series(Categorical(["a", "b", "c"]))
211-
with tm.assert_produces_warning(FutureWarning):
212-
result = ser.where([True, False, True], "d")
213-
214-
expected = pd.Series(np.array(["a", "d", "c"], dtype="object"))
215-
tm.assert_series_equal(result, expected)
211+
msg = "Cannot setitem on a Categorical with a new category"
212+
with pytest.raises(ValueError, match=msg):
213+
ser.where([True, False, True], "d")
216214

217215
def test_where_ordered_differs_rasies(self):
218216
ser = pd.Series(
@@ -221,11 +219,8 @@ def test_where_ordered_differs_rasies(self):
221219
other = Categorical(
222220
["b", "c", "a"], categories=["a", "c", "b", "d"], ordered=True
223221
)
224-
with tm.assert_produces_warning(FutureWarning):
225-
result = ser.where([True, False, True], other)
226-
227-
expected = pd.Series(np.array(["a", "c", "c"], dtype=object))
228-
tm.assert_series_equal(result, expected)
222+
with pytest.raises(ValueError, match="without identical categories"):
223+
ser.where([True, False, True], other)
229224

230225

231226
@pytest.mark.parametrize("index", [True, False])

0 commit comments

Comments
 (0)