Skip to content
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

DEPR: CategoricalBlock.where casting to object #29913

Merged
merged 2 commits into from
Nov 29, 2019
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.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