Skip to content

Commit

Permalink
DEPR: remove inplace arg in Categorical methods (#49321)
Browse files Browse the repository at this point in the history
* deprecate inplace arg in categorical methods

* fix tests

* add back test

* doc fix

* doc fixes

* avoid constructing new objects on every iteration

* cleanup
  • Loading branch information
lukemanley committed Oct 29, 2022
1 parent 8ea52bb commit ab6562a
Show file tree
Hide file tree
Showing 10 changed files with 87 additions and 507 deletions.
14 changes: 2 additions & 12 deletions doc/source/user_guide/categorical.rst
Original file line number Diff line number Diff line change
Expand Up @@ -353,11 +353,6 @@ Renaming categories is done by using the

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

.. note::

Be aware that assigning new categories is an inplace operation, while most other operations
under ``Series.cat`` per default return a new ``Series`` of dtype ``category``.

Categories must be unique or a ``ValueError`` is raised:

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

.. ipython:: python
:okwarning:
import io
Expand All @@ -969,8 +963,8 @@ relevant columns back to ``category`` and assign the right categories and catego
df2["cats"]
# Redo the category
df2["cats"] = df2["cats"].astype("category")
df2["cats"].cat.set_categories(
["very bad", "bad", "medium", "good", "very good"], inplace=True
df2["cats"] = df2["cats"].cat.set_categories(
["very bad", "bad", "medium", "good", "very good"]
)
df2.dtypes
df2["cats"]
Expand Down Expand Up @@ -1162,16 +1156,12 @@ Constructing a ``Series`` from a ``Categorical`` will not copy the input
change the original ``Categorical``:

.. ipython:: python
:okwarning:
cat = pd.Categorical([1, 2, 3, 10], categories=[1, 2, 3, 4, 10])
s = pd.Series(cat, name="cat")
cat
s.iloc[0:2] = 10
cat
df = pd.DataFrame(s)
df["cat"].cat.categories = [1, 2, 3, 4, 5]
cat
Use ``copy=True`` to prevent such a behaviour or simply don't reuse ``Categoricals``:

Expand Down
3 changes: 1 addition & 2 deletions doc/source/whatsnew/v0.15.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ For full docs, see the :ref:`categorical introduction <categorical>` and the
:ref:`API documentation <api.arrays.categorical>`.

.. ipython:: python
:okwarning:
df = pd.DataFrame({"id": [1, 2, 3, 4, 5, 6],
"raw_grade": ['a', 'b', 'b', 'a', 'a', 'e']})
Expand All @@ -79,7 +78,7 @@ For full docs, see the :ref:`categorical introduction <categorical>` and the
df["grade"]
# Rename the categories
df["grade"].cat.categories = ["very good", "good", "very bad"]
df["grade"] = df["grade"].cat.rename_categories(["very good", "good", "very bad"])
# Reorder the categories and simultaneously add the missing categories
df["grade"] = df["grade"].cat.set_categories(["very bad", "bad",
Expand Down
4 changes: 2 additions & 2 deletions doc/source/whatsnew/v0.19.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -271,12 +271,12 @@ Individual columns can be parsed as a ``Categorical`` using a dict specification
such as :func:`to_datetime`.

.. ipython:: python
:okwarning:
df = pd.read_csv(StringIO(data), dtype="category")
df.dtypes
df["col3"]
df["col3"].cat.categories = pd.to_numeric(df["col3"].cat.categories)
new_categories = pd.to_numeric(df["col3"].cat.categories)
df["col3"] = df["col3"].cat.rename_categories(new_categories)
df["col3"]
.. _whatsnew_0190.enhancements.union_categoricals:
Expand Down

0 comments on commit ab6562a

Please sign in to comment.