Skip to content

Commit

Permalink
DEPR: CategoricalBlock.where casting to object (pandas-dev#29913)
Browse files Browse the repository at this point in the history
  • Loading branch information
jbrockmendel authored and proost committed Dec 19, 2019
1 parent 90c26fd commit 38eade3
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 42 deletions.
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,7 @@ or ``matplotlib.Axes.plot``. See :ref:`plotting.formatters` for more.
- Removed :meth:`DataFrame.as_blocks`, :meth:`Series.as_blocks`, `DataFrame.blocks`, :meth:`Series.blocks` (:issue:`17656`)
- :meth:`pandas.Series.str.cat` now defaults to aligning ``others``, using ``join='left'`` (:issue:`27611`)
- :meth:`pandas.Series.str.cat` does not accept list-likes *within* list-likes anymore (:issue:`27611`)
- :meth:`Series.where` with ``Categorical`` dtype (or :meth:`DataFrame.where` with ``Categorical`` column) no longer allows setting new categories (:issue:`24114`)
- :func:`core.internals.blocks.make_block` no longer accepts the "fastpath" keyword(:issue:`19265`)
- :meth:`Block.make_block_same_class` no longer accepts the "dtype" keyword(:issue:`19434`)
- Removed the previously deprecated :meth:`ExtensionArray._formatting_values`. Use :attr:`ExtensionArray._formatter` instead. (:issue:`23601`)
Expand Down
31 changes: 0 additions & 31 deletions pandas/core/internals/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -2887,37 +2887,6 @@ def concat_same_type(self, to_concat, placement=None):
values, placement=placement or slice(0, len(values), 1), ndim=self.ndim
)

def where(
self,
other,
cond,
align=True,
errors="raise",
try_cast: bool = False,
axis: int = 0,
) -> List["Block"]:
# TODO(CategoricalBlock.where):
# This can all be deleted in favor of ExtensionBlock.where once
# we enforce the deprecation.
object_msg = (
"Implicitly converting categorical to object-dtype ndarray. "
"One or more of the values in 'other' are not present in this "
"categorical's categories. A future version of pandas will raise "
"a ValueError when 'other' contains different categories.\n\n"
"To preserve the current behavior, add the new categories to "
"the categorical before calling 'where', or convert the "
"categorical to a different dtype."
)
try:
# Attempt to do preserve categorical dtype.
result = super().where(other, cond, align, errors, try_cast, axis)
except (TypeError, ValueError):
warnings.warn(object_msg, FutureWarning, stacklevel=6)
result = self.astype(object).where(
other, cond, align=align, errors=errors, try_cast=try_cast, axis=axis
)
return result

def replace(
self,
to_replace,
Expand Down
17 changes: 6 additions & 11 deletions pandas/tests/arrays/categorical/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,13 +206,11 @@ def test_where_other_categorical(self):
expected = pd.Series(Categorical(["a", "c", "c"], dtype=ser.dtype))
tm.assert_series_equal(result, expected)

def test_where_warns(self):
def test_where_new_category_raises(self):
ser = pd.Series(Categorical(["a", "b", "c"]))
with tm.assert_produces_warning(FutureWarning):
result = ser.where([True, False, True], "d")

expected = pd.Series(np.array(["a", "d", "c"], dtype="object"))
tm.assert_series_equal(result, expected)
msg = "Cannot setitem on a Categorical with a new category"
with pytest.raises(ValueError, match=msg):
ser.where([True, False, True], "d")

def test_where_ordered_differs_rasies(self):
ser = pd.Series(
Expand All @@ -221,11 +219,8 @@ def test_where_ordered_differs_rasies(self):
other = Categorical(
["b", "c", "a"], categories=["a", "c", "b", "d"], ordered=True
)
with tm.assert_produces_warning(FutureWarning):
result = ser.where([True, False, True], other)

expected = pd.Series(np.array(["a", "c", "c"], dtype=object))
tm.assert_series_equal(result, expected)
with pytest.raises(ValueError, match="without identical categories"):
ser.where([True, False, True], other)


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

0 comments on commit 38eade3

Please sign in to comment.