Skip to content

Commit fe2117c

Browse files
jbrockmendeljreback
authored andcommitted
DEPR: categorical.take allow_fill default (#29912)
1 parent 7cf189d commit fe2117c

File tree

3 files changed

+24
-27
lines changed

3 files changed

+24
-27
lines changed

doc/source/whatsnew/v1.0.0.rst

+2
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,7 @@ Deprecations
365365
is equivalent to ``arr[idx.get_loc(idx_val)] = val``, which should be used instead (:issue:`28621`).
366366
- :func:`is_extension_type` is deprecated, :func:`is_extension_array_dtype` should be used instead (:issue:`29457`)
367367
- :func:`eval` keyword argument "truediv" is deprecated and will be removed in a future version (:issue:`29812`)
368+
- :meth:`Categorical.take_nd` is deprecated, use :meth:`Categorical.take` instead (:issue:`27745`)
368369

369370
.. _whatsnew_1000.prior_deprecations:
370371

@@ -457,6 +458,7 @@ or ``matplotlib.Axes.plot``. See :ref:`plotting.formatters` for more.
457458
- In :func:`concat` the default value for ``sort`` has been changed from ``None`` to ``False`` (:issue:`20613`)
458459
- Removed previously deprecated "raise_conflict" argument from :meth:`DataFrame.update`, use "errors" instead (:issue:`23585`)
459460
- Removed previously deprecated keyword "n" from :meth:`DatetimeIndex.shift`, :meth:`TimedeltaIndex.shift`, :meth:`PeriodIndex.shift`, use "periods" instead (:issue:`22458`)
461+
- Changed the default ``fill_value`` in :meth:`Categorical.take` from ``True`` to ``False`` (:issue:`20841`)
460462
- Changed the default value for the `raw` argument in :func:`Series.rolling().apply() <pandas.core.window.Rolling.apply>`, :func:`DataFrame.rolling().apply() <pandas.core.window.Rolling.apply>`,
461463
- :func:`Series.expanding().apply() <pandas.core.window.Expanding.apply>`, and :func:`DataFrame.expanding().apply() <pandas.core.window.Expanding.apply>` to ``False`` (:issue:`20584`)
462464
- Changed :meth:`Timedelta.resolution` to match the behavior of the standard library ``datetime.timedelta.resolution``, for the old behavior, use :meth:`Timedelta.resolution_string` (:issue:`26839`)

pandas/core/arrays/categorical.py

+12-24
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import operator
22
from shutil import get_terminal_size
3-
import textwrap
43
from typing import Type, Union, cast
54
from warnings import warn
65

@@ -59,18 +58,6 @@
5958

6059
from .base import ExtensionArray, _extension_array_shared_docs, try_cast_to_ea
6160

62-
_take_msg = textwrap.dedent(
63-
"""\
64-
Interpreting negative values in 'indexer' as missing values.
65-
In the future, this will change to meaning positional indices
66-
from the right.
67-
68-
Use 'allow_fill=True' to retain the previous behavior and silence this
69-
warning.
70-
71-
Use 'allow_fill=False' to accept the new behavior."""
72-
)
73-
7461

7562
def _cat_compare_op(op):
7663
opname = f"__{op.__name__}__"
@@ -1829,7 +1816,7 @@ def fillna(self, value=None, method=None, limit=None):
18291816

18301817
return self._constructor(codes, dtype=self.dtype, fastpath=True)
18311818

1832-
def take_nd(self, indexer, allow_fill=None, fill_value=None):
1819+
def take(self, indexer, allow_fill: bool = False, fill_value=None):
18331820
"""
18341821
Take elements from the Categorical.
18351822
@@ -1838,7 +1825,7 @@ def take_nd(self, indexer, allow_fill=None, fill_value=None):
18381825
indexer : sequence of int
18391826
The indices in `self` to take. The meaning of negative values in
18401827
`indexer` depends on the value of `allow_fill`.
1841-
allow_fill : bool, default None
1828+
allow_fill : bool, default False
18421829
How to handle negative values in `indexer`.
18431830
18441831
* False: negative values in `indices` indicate positional indices
@@ -1849,11 +1836,9 @@ def take_nd(self, indexer, allow_fill=None, fill_value=None):
18491836
(the default). These values are set to `fill_value`. Any other
18501837
other negative values raise a ``ValueError``.
18511838
1852-
.. versionchanged:: 0.23.0
1839+
.. versionchanged:: 1.0.0
18531840
1854-
Deprecated the default value of `allow_fill`. The deprecated
1855-
default is ``True``. In the future, this will change to
1856-
``False``.
1841+
Default value changed from ``True`` to ``False``.
18571842
18581843
fill_value : object
18591844
The value to use for `indices` that are missing (-1), when
@@ -1903,10 +1888,6 @@ def take_nd(self, indexer, allow_fill=None, fill_value=None):
19031888
will raise a ``TypeError``.
19041889
"""
19051890
indexer = np.asarray(indexer, dtype=np.intp)
1906-
if allow_fill is None:
1907-
if (indexer < 0).any():
1908-
warn(_take_msg, FutureWarning, stacklevel=2)
1909-
allow_fill = True
19101891

19111892
dtype = self.dtype
19121893

@@ -1927,7 +1908,14 @@ def take_nd(self, indexer, allow_fill=None, fill_value=None):
19271908
result = type(self).from_codes(codes, dtype=dtype)
19281909
return result
19291910

1930-
take = take_nd
1911+
def take_nd(self, indexer, allow_fill: bool = False, fill_value=None):
1912+
# GH#27745 deprecate alias that other EAs dont have
1913+
warn(
1914+
"Categorical.take_nd is deprecated, use Categorical.take instead",
1915+
FutureWarning,
1916+
stacklevel=2,
1917+
)
1918+
return self.take(indexer, allow_fill=allow_fill, fill_value=fill_value)
19311919

19321920
def __len__(self) -> int:
19331921
"""

pandas/tests/arrays/categorical/test_algos.py

+10-3
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,12 @@ def test_isin_empty(empty):
8989
class TestTake:
9090
# https://github.com/pandas-dev/pandas/issues/20664
9191

92-
def test_take_warns(self):
92+
def test_take_default_allow_fill(self):
9393
cat = pd.Categorical(["a", "b"])
94-
with tm.assert_produces_warning(FutureWarning):
95-
cat.take([0, -1])
94+
with tm.assert_produces_warning(None):
95+
result = cat.take([0, -1])
96+
97+
assert result.equals(cat)
9698

9799
def test_take_positive_no_warning(self):
98100
cat = pd.Categorical(["a", "b"])
@@ -158,3 +160,8 @@ def test_take_fill_value_new_raises(self):
158160
xpr = r"'fill_value' \('d'\) is not in this Categorical's categories."
159161
with pytest.raises(TypeError, match=xpr):
160162
cat.take([0, 1, -1], fill_value="d", allow_fill=True)
163+
164+
def test_take_nd_deprecated(self):
165+
cat = pd.Categorical(["a", "b", "c"])
166+
with tm.assert_produces_warning(FutureWarning):
167+
cat.take_nd([0, 1])

0 commit comments

Comments
 (0)