Skip to content

Commit ad6ae60

Browse files
lukemanleynoatamir
authored andcommitted
DEPR: remove inplace arg in Categorical methods (pandas-dev#49321)
* deprecate inplace arg in categorical methods * fix tests * add back test * doc fix * doc fixes * avoid constructing new objects on every iteration * cleanup
1 parent 4d1b32c commit ad6ae60

File tree

10 files changed

+87
-507
lines changed

10 files changed

+87
-507
lines changed

doc/source/user_guide/categorical.rst

+2-12
Original file line numberDiff line numberDiff line change
@@ -353,11 +353,6 @@ Renaming categories is done by using the
353353

354354
In contrast to R's ``factor``, categorical data can have categories of other types than string.
355355

356-
.. note::
357-
358-
Be aware that assigning new categories is an inplace operation, while most other operations
359-
under ``Series.cat`` per default return a new ``Series`` of dtype ``category``.
360-
361356
Categories must be unique or a ``ValueError`` is raised:
362357

363358
.. ipython:: python
@@ -952,7 +947,6 @@ categorical (categories and ordering). So if you read back the CSV file you have
952947
relevant columns back to ``category`` and assign the right categories and categories ordering.
953948

954949
.. ipython:: python
955-
:okwarning:
956950
957951
import io
958952
@@ -969,8 +963,8 @@ relevant columns back to ``category`` and assign the right categories and catego
969963
df2["cats"]
970964
# Redo the category
971965
df2["cats"] = df2["cats"].astype("category")
972-
df2["cats"].cat.set_categories(
973-
["very bad", "bad", "medium", "good", "very good"], inplace=True
966+
df2["cats"] = df2["cats"].cat.set_categories(
967+
["very bad", "bad", "medium", "good", "very good"]
974968
)
975969
df2.dtypes
976970
df2["cats"]
@@ -1162,16 +1156,12 @@ Constructing a ``Series`` from a ``Categorical`` will not copy the input
11621156
change the original ``Categorical``:
11631157

11641158
.. ipython:: python
1165-
:okwarning:
11661159
11671160
cat = pd.Categorical([1, 2, 3, 10], categories=[1, 2, 3, 4, 10])
11681161
s = pd.Series(cat, name="cat")
11691162
cat
11701163
s.iloc[0:2] = 10
11711164
cat
1172-
df = pd.DataFrame(s)
1173-
df["cat"].cat.categories = [1, 2, 3, 4, 5]
1174-
cat
11751165
11761166
Use ``copy=True`` to prevent such a behaviour or simply don't reuse ``Categoricals``:
11771167

doc/source/whatsnew/v0.15.0.rst

+1-2
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@ For full docs, see the :ref:`categorical introduction <categorical>` and the
7070
:ref:`API documentation <api.arrays.categorical>`.
7171

7272
.. ipython:: python
73-
:okwarning:
7473
7574
df = pd.DataFrame({"id": [1, 2, 3, 4, 5, 6],
7675
"raw_grade": ['a', 'b', 'b', 'a', 'a', 'e']})
@@ -79,7 +78,7 @@ For full docs, see the :ref:`categorical introduction <categorical>` and the
7978
df["grade"]
8079
8180
# Rename the categories
82-
df["grade"].cat.categories = ["very good", "good", "very bad"]
81+
df["grade"] = df["grade"].cat.rename_categories(["very good", "good", "very bad"])
8382
8483
# Reorder the categories and simultaneously add the missing categories
8584
df["grade"] = df["grade"].cat.set_categories(["very bad", "bad",

doc/source/whatsnew/v0.19.0.rst

+2-2
Original file line numberDiff line numberDiff line change
@@ -271,12 +271,12 @@ Individual columns can be parsed as a ``Categorical`` using a dict specification
271271
such as :func:`to_datetime`.
272272

273273
.. ipython:: python
274-
:okwarning:
275274
276275
df = pd.read_csv(StringIO(data), dtype="category")
277276
df.dtypes
278277
df["col3"]
279-
df["col3"].cat.categories = pd.to_numeric(df["col3"].cat.categories)
278+
new_categories = pd.to_numeric(df["col3"].cat.categories)
279+
df["col3"] = df["col3"].cat.rename_categories(new_categories)
280280
df["col3"]
281281
282282
.. _whatsnew_0190.enhancements.union_categoricals:

0 commit comments

Comments
 (0)