diff --git a/doc/source/whatsnew/v2.1.2.rst b/doc/source/whatsnew/v2.1.2.rst index 2c9b10160d144..bbf7368513fd4 100644 --- a/doc/source/whatsnew/v2.1.2.rst +++ b/doc/source/whatsnew/v2.1.2.rst @@ -29,7 +29,7 @@ Bug fixes Other ~~~~~ -- +- Changed the ``FutureWarning`` raised when ``observed=False`` in :meth:`DataFrame.groupby` and :meth:`Series.groupby` to a ``DeprecationWarning`` (:issue:`54970`) - .. --------------------------------------------------------------------------- diff --git a/pandas/core/groupby/groupby.py b/pandas/core/groupby/groupby.py index d607baf18d6cb..942358b6db3de 100644 --- a/pandas/core/groupby/groupby.py +++ b/pandas/core/groupby/groupby.py @@ -1322,7 +1322,7 @@ def __init__( "to True in a future version of pandas. Pass observed=False to " "retain current behavior or observed=True to adopt the future " "default and silence this warning.", - FutureWarning, + DeprecationWarning, stacklevel=find_stack_level(), ) observed = False diff --git a/pandas/tests/groupby/test_categorical.py b/pandas/tests/groupby/test_categorical.py index b11240c841420..cb083eed4c2fc 100644 --- a/pandas/tests/groupby/test_categorical.py +++ b/pandas/tests/groupby/test_categorical.py @@ -2080,7 +2080,7 @@ def test_groupby_default_depr(cat_columns, keys): df = DataFrame({"a": [1, 1, 2, 3], "b": [4, 5, 6, 7]}) df[cat_columns] = df[cat_columns].astype("category") msg = "The default of observed=False is deprecated" - klass = FutureWarning if set(cat_columns) & set(keys) else None + klass = DeprecationWarning if set(cat_columns) & set(keys) else None with tm.assert_produces_warning(klass, match=msg): df.groupby(keys)