Skip to content

Commit 739833a

Browse files
committed
Will raise in the future
1 parent 57d99a7 commit 739833a

File tree

10 files changed

+68
-75
lines changed

10 files changed

+68
-75
lines changed

doc/source/user_guide/categorical.rst

+2-3
Original file line numberDiff line numberDiff line change
@@ -635,9 +635,8 @@ even if some categories are not present in the data:
635635
)
636636
df.sum(axis=1, level=1)
637637
638-
Groupby will also show "unused" categories, though this default is deprecated
639-
and will be changed in a future release. It is recommended to use the
640-
``observed`` keyword explicitly as below:
638+
Groupby will also show "unused" categories by default, though this behavior
639+
is deprecated. In a future release, users must specify a value for ``observed``:
641640

642641
.. ipython:: python
643642

pandas/core/groupby/grouper.py

+6-5
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,13 @@
3434

3535
_observed_msg = textwrap.dedent(
3636
"""\
37-
Using 'observed=False', because grouping on a categorical. A future version
38-
of pandas will change to 'observed=True'.
37+
Grouping by a categorical but 'observed' was not specified.
38+
Using 'observed=False', but in a future version of pandas
39+
not specifying 'observed' will raise an error. Pass
40+
'observed=True' or 'observed=False' to silence this warning.
3941
40-
To silence the warning and switch to the future behavior, pass 'observed=True'.
41-
42-
To keep the current behavior and silence the warning, pass 'observed=False'.
42+
See the `groupby` documentation for more information on the
43+
observed keyword.
4344
"""
4445
)
4546

pandas/core/shared_docs.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,10 @@
120120
If True: only show observed values for categorical groupers.
121121
If False: show all values for categorical groupers.
122122
123-
The current default of ``observed=False`` is deprecated and will
124-
change to ``observed=True`` in a future version of pandas.
123+
The current default of ``observed=False`` is deprecated. In
124+
the future this will be a required keyword in the presence
125+
of a categorical grouper and a failure to specify a value will
126+
result in an error.
125127
126128
Explicitly pass ``observed=True`` to silence the warning and not
127129
show all observed values.

pandas/tests/groupby/aggregate/test_cython.py

+13-17
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
11
"""
22
test cython .agg behavior
33
"""
4-
54
import numpy as np
65
import pytest
76

87
import pandas as pd
9-
from pandas import DataFrame, Index, NaT, Series, Timedelta, Timestamp, bdate_range
10-
import pandas._testing as tm
8+
from pandas import (
9+
DataFrame,
10+
Index,
11+
NaT,
12+
Series,
13+
Timedelta,
14+
Timestamp,
15+
_testing as tm,
16+
bdate_range,
17+
)
1118
from pandas.core.groupby.groupby import DataError
1219

1320

@@ -175,7 +182,7 @@ def test__cython_agg_general(op, targop):
175182
("max", np.max),
176183
],
177184
)
178-
@pytest.mark.filterwarnings("ignore:Using 'observed:FutureWarning")
185+
@pytest.mark.filterwarnings("ignore:Grouping by a categorical:FutureWarning")
179186
def test_cython_agg_empty_buckets(op, targop, observed):
180187
df = DataFrame([11, 12, 13])
181188
grps = range(0, 55, 5)
@@ -190,7 +197,7 @@ def test_cython_agg_empty_buckets(op, targop, observed):
190197
tm.assert_frame_equal(result, expected)
191198

192199

193-
@pytest.mark.filterwarnings("ignore:Using 'observed:FutureWarning")
200+
@pytest.mark.filterwarnings("ignore:Grouping by a categorical:FutureWarning")
194201
def test_cython_agg_empty_buckets_nanops(observed):
195202
# GH-18869 can't call nanops on empty groups, so hardcode expected
196203
# for these
@@ -283,18 +290,7 @@ def test_read_only_buffer_source_agg(agg):
283290

284291
@pytest.mark.parametrize(
285292
"op_name",
286-
[
287-
"count",
288-
"sum",
289-
"std",
290-
"var",
291-
"sem",
292-
"mean",
293-
"median",
294-
"prod",
295-
"min",
296-
"max",
297-
],
293+
["count", "sum", "std", "var", "sem", "mean", "median", "prod", "min", "max",],
298294
)
299295
def test_cython_agg_nullable_int(op_name):
300296
# ensure that the cython-based aggregations don't fail for nullable dtype

pandas/tests/groupby/aggregate/test_other.py

+4-11
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
"""
22
test all other .agg behavior
33
"""
4-
54
import datetime as dt
65
from functools import partial
76

@@ -15,10 +14,10 @@
1514
MultiIndex,
1615
PeriodIndex,
1716
Series,
17+
_testing as tm,
1818
date_range,
1919
period_range,
2020
)
21-
import pandas._testing as tm
2221
from pandas.core.base import SpecificationError
2322

2423
from pandas.io.formats.printing import pprint_thing
@@ -515,14 +514,8 @@ def test_sum_uint64_overflow():
515514
[
516515
(tuple, DataFrame({"C": {(1, 1): (1, 1, 1), (3, 4): (3, 4, 4)}})),
517516
(list, DataFrame({"C": {(1, 1): [1, 1, 1], (3, 4): [3, 4, 4]}})),
518-
(
519-
lambda x: tuple(x),
520-
DataFrame({"C": {(1, 1): (1, 1, 1), (3, 4): (3, 4, 4)}}),
521-
),
522-
(
523-
lambda x: list(x),
524-
DataFrame({"C": {(1, 1): [1, 1, 1], (3, 4): [3, 4, 4]}}),
525-
),
517+
(lambda x: tuple(x), DataFrame({"C": {(1, 1): (1, 1, 1), (3, 4): (3, 4, 4)}}),),
518+
(lambda x: list(x), DataFrame({"C": {(1, 1): [1, 1, 1], (3, 4): [3, 4, 4]}}),),
526519
],
527520
)
528521
def test_agg_structs_dataframe(structure, expected):
@@ -555,7 +548,7 @@ def test_agg_structs_series(structure, expected):
555548
tm.assert_series_equal(result, expected)
556549

557550

558-
@pytest.mark.filterwarnings("ignore:Using 'observed:FutureWarning")
551+
@pytest.mark.filterwarnings("ignore:Grouping by a categorical:FutureWarning")
559552
def test_agg_category_nansum(observed):
560553
categories = ["a", "b", "c"]
561554
df = DataFrame(

pandas/tests/groupby/test_categorical.py

+19-20
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ def f(x):
212212
tm.assert_index_equal((desc_result.stack().index.get_level_values(1)), exp)
213213

214214

215-
@pytest.mark.filterwarnings("ignore:Using 'observed:FutureWarning")
215+
@pytest.mark.filterwarnings("ignore:Grouping by a categorical:FutureWarning")
216216
def test_level_get_group(observed):
217217
# GH15155
218218
df = DataFrame(
@@ -277,7 +277,7 @@ def test_apply(ordered):
277277
tm.assert_series_equal(result, expected)
278278

279279

280-
@pytest.mark.filterwarnings("ignore:Using 'observed:FutureWarning")
280+
@pytest.mark.filterwarnings("ignore:Grouping by a categorical:FutureWarning")
281281
def test_observed(observed):
282282
# multiple groupers, don't re-expand the output space
283283
# of the grouper
@@ -386,7 +386,7 @@ def test_observed(observed):
386386
tm.assert_frame_equal(result, expected)
387387

388388

389-
@pytest.mark.filterwarnings("ignore:Using 'observed:FutureWarning")
389+
@pytest.mark.filterwarnings("ignore:Grouping by a categorical:FutureWarning")
390390
def test_observed_codes_remap(observed):
391391
d = {"C1": [3, 3, 4, 5], "C2": [1, 2, 3, 4], "C3": [10, 100, 200, 34]}
392392
df = DataFrame(d)
@@ -427,7 +427,7 @@ def test_observed_perf():
427427
assert result.index.levels[2].nunique() == df.other_id.nunique()
428428

429429

430-
@pytest.mark.filterwarnings("ignore:Using 'observed:FutureWarning")
430+
@pytest.mark.filterwarnings("ignore:Grouping by a categorical:FutureWarning")
431431
def test_observed_groups(observed):
432432
# gh-20583
433433
# test that we have the appropriate groups
@@ -450,7 +450,7 @@ def test_observed_groups(observed):
450450
tm.assert_dict_equal(result, expected)
451451

452452

453-
@pytest.mark.filterwarnings("ignore:Using 'observed:FutureWarning")
453+
@pytest.mark.filterwarnings("ignore:Grouping by a categorical:FutureWarning")
454454
def test_observed_groups_with_nan(observed):
455455
# GH 24740
456456
df = DataFrame(
@@ -487,7 +487,7 @@ def test_observed_nth():
487487
tm.assert_series_equal(result, expected)
488488

489489

490-
@pytest.mark.filterwarnings("ignore:Using 'observed:FutureWarning")
490+
@pytest.mark.filterwarnings("ignore:Grouping by a categorical:FutureWarning")
491491
def test_dataframe_categorical_with_nan(observed):
492492
# GH 21151
493493
s1 = Categorical([np.nan, "a", np.nan, "a"], categories=["a", "b", "c"])
@@ -511,7 +511,7 @@ def test_dataframe_categorical_with_nan(observed):
511511
@pytest.mark.parametrize("ordered", [True, False])
512512
@pytest.mark.parametrize("observed", [True, False])
513513
@pytest.mark.parametrize("sort", [True, False])
514-
@pytest.mark.filterwarnings("ignore:Using 'observed:FutureWarning")
514+
@pytest.mark.filterwarnings("ignore:Grouping by a categorical:FutureWarning")
515515
def test_dataframe_categorical_ordered_observed_sort(ordered, observed, sort):
516516
# GH 25871: Fix groupby sorting on ordered Categoricals
517517
# GH 25167: Groupby with observed=True doesn't sort
@@ -1176,7 +1176,7 @@ def test_seriesgroupby_observed_true(df_cat, operation, kwargs):
11761176

11771177
@pytest.mark.parametrize("operation", ["agg", "apply"])
11781178
@pytest.mark.parametrize("observed", [False, None])
1179-
@pytest.mark.filterwarnings("ignore:Using 'observed:FutureWarning")
1179+
@pytest.mark.filterwarnings("ignore:Grouping by a categorical:FutureWarning")
11801180
def test_seriesgroupby_observed_false_or_none(df_cat, observed, operation):
11811181
# GH 24880
11821182
index, _ = MultiIndex.from_product(
@@ -1241,7 +1241,7 @@ def test_seriesgroupby_observed_false_or_none(df_cat, observed, operation):
12411241
),
12421242
],
12431243
)
1244-
@pytest.mark.filterwarnings("ignore:Using 'observed:FutureWarning")
1244+
@pytest.mark.filterwarnings("ignore:Grouping by a categorical:FutureWarning")
12451245
def test_seriesgroupby_observed_apply_dict(df_cat, observed, index, data):
12461246
# GH 24880
12471247
expected = Series(data=data, index=index, name="C")
@@ -1259,7 +1259,7 @@ def test_groupby_categorical_series_dataframe_consistent(df_cat):
12591259

12601260

12611261
@pytest.mark.parametrize("code", [([1, 0, 0]), ([0, 0, 0])])
1262-
@pytest.mark.filterwarnings("ignore:Using 'observed:FutureWarning")
1262+
@pytest.mark.filterwarnings("ignore:Grouping by a categorical:FutureWarning")
12631263
def test_groupby_categorical_axis_1(code):
12641264
# GH 13420
12651265
df = DataFrame({"a": [1, 2, 3, 4], "b": [-1, -2, -3, -4], "c": [5, 6, 7, 8]})
@@ -1269,7 +1269,7 @@ def test_groupby_categorical_axis_1(code):
12691269
tm.assert_frame_equal(result, expected)
12701270

12711271

1272-
@pytest.mark.filterwarnings("ignore:Using 'observed:FutureWarning")
1272+
@pytest.mark.filterwarnings("ignore:Grouping by a categorical:FutureWarning")
12731273
def test_groupby_cat_preserves_structure(observed, ordered):
12741274
# GH 28787
12751275
df = DataFrame(
@@ -1298,7 +1298,7 @@ def test_get_nonexistent_category():
12981298
)
12991299

13001300

1301-
@pytest.mark.filterwarnings("ignore:Using 'observed:FutureWarning")
1301+
@pytest.mark.filterwarnings("ignore:Grouping by a categorical:FutureWarning")
13021302
def test_series_groupby_on_2_categoricals_unobserved(reduction_func, observed, request):
13031303
# GH 17605
13041304
if reduction_func == "ngroup":
@@ -1398,7 +1398,7 @@ def test_dataframe_groupby_on_2_categoricals_when_observed_is_true(reduction_fun
13981398

13991399

14001400
@pytest.mark.parametrize("observed", [False, None])
1401-
@pytest.mark.filterwarnings("ignore:Using 'observed:FutureWarning")
1401+
@pytest.mark.filterwarnings("ignore:Grouping by a categorical:FutureWarning")
14021402
def test_dataframe_groupby_on_2_categoricals_when_observed_is_false(
14031403
reduction_func, observed, request
14041404
):
@@ -1432,7 +1432,7 @@ def test_dataframe_groupby_on_2_categoricals_when_observed_is_false(
14321432
assert (res.loc[unobserved_cats] == expected).all().all()
14331433

14341434

1435-
@pytest.mark.filterwarnings("ignore:Using 'observed:FutureWarning")
1435+
@pytest.mark.filterwarnings("ignore:Grouping by a categorical:FutureWarning")
14361436
def test_series_groupby_categorical_aggregation_getitem():
14371437
# GH 8870
14381438
d = {"foo": [10, 8, 4, 1], "bar": [10, 20, 30, 40], "baz": ["d", "c", "d", "c"]}
@@ -1446,8 +1446,7 @@ def test_series_groupby_categorical_aggregation_getitem():
14461446

14471447

14481448
@pytest.mark.parametrize(
1449-
"func, expected_values",
1450-
[(Series.nunique, [1, 1, 2]), (Series.count, [1, 2, 2])],
1449+
"func, expected_values", [(Series.nunique, [1, 1, 2]), (Series.count, [1, 2, 2])],
14511450
)
14521451
def test_groupby_agg_categorical_columns(func, expected_values):
14531452
# 31256
@@ -1488,7 +1487,7 @@ def test_groupy_first_returned_categorical_instead_of_dataframe(func):
14881487
tm.assert_series_equal(result, expected)
14891488

14901489

1491-
@pytest.mark.filterwarnings("ignore:Using 'observed:FutureWarning")
1490+
@pytest.mark.filterwarnings("ignore:Grouping by a categorical:FutureWarning")
14921491
def test_read_only_category_no_sort():
14931492
# GH33410
14941493
cats = np.array([1, 2])
@@ -1502,7 +1501,7 @@ def test_read_only_category_no_sort():
15021501
tm.assert_frame_equal(result, expected)
15031502

15041503

1505-
@pytest.mark.filterwarnings("ignore:Using 'observed:FutureWarning")
1504+
@pytest.mark.filterwarnings("ignore:Grouping by a categorical:FutureWarning")
15061505
def test_sorted_missing_category_values():
15071506
# GH 28597
15081507
df = DataFrame(
@@ -1650,7 +1649,7 @@ def test_categorical_transform():
16501649

16511650

16521651
@pytest.mark.parametrize("func", ["first", "last"])
1653-
@pytest.mark.filterwarnings("ignore:Using 'observed:FutureWarning")
1652+
@pytest.mark.filterwarnings("ignore:Grouping by a categorical:FutureWarning")
16541653
def test_series_groupby_first_on_categorical_col_grouped_on_2_categoricals(
16551654
func: str, observed: bool
16561655
):
@@ -1676,7 +1675,7 @@ def test_series_groupby_first_on_categorical_col_grouped_on_2_categoricals(
16761675

16771676

16781677
@pytest.mark.parametrize("func", ["first", "last"])
1679-
@pytest.mark.filterwarnings("ignore:Using 'observed:FutureWarning")
1678+
@pytest.mark.filterwarnings("ignore:Grouping by a categorical:FutureWarning")
16801679
def test_df_groupby_first_on_categorical_col_grouped_on_2_categoricals(
16811680
func: str, observed: bool
16821681
):

pandas/tests/groupby/test_function.py

+12-4
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,17 @@
77
from pandas.errors import UnsupportedFunctionCall
88

99
import pandas as pd
10-
from pandas import DataFrame, Index, MultiIndex, Series, Timestamp, date_range, isna
11-
import pandas._testing as tm
12-
import pandas.core.nanops as nanops
10+
from pandas import (
11+
DataFrame,
12+
Index,
13+
MultiIndex,
14+
Series,
15+
Timestamp,
16+
_testing as tm,
17+
date_range,
18+
isna,
19+
)
20+
from pandas.core import nanops as nanops
1321
from pandas.util import _test_decorators as td
1422

1523

@@ -410,7 +418,7 @@ def test_cython_median():
410418
tm.assert_frame_equal(rs, xp)
411419

412420

413-
@pytest.mark.filterwarnings("ignore:Using 'observed:FutureWarning")
421+
@pytest.mark.filterwarnings("ignore:Grouping by a categorical:FutureWarning")
414422
def test_median_empty_bins(observed):
415423
df = DataFrame(np.random.randint(0, 44, 500))
416424

pandas/tests/groupby/test_grouping.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ def test_groupby_levels_and_columns(self):
310310
by_columns.columns = by_columns.columns.astype(np.int64)
311311
tm.assert_frame_equal(by_levels, by_columns)
312312

313-
@pytest.mark.filterwarnings("ignore:Using 'observed:FutureWarning")
313+
@pytest.mark.filterwarnings("ignore:Grouping by a categorical:FutureWarning")
314314
def test_groupby_categorical_index_and_columns(self, observed):
315315
# GH18432, adapted for GH25871
316316
columns = ["A", "B", "A", "B"]
@@ -778,7 +778,7 @@ def test_get_group(self):
778778
with pytest.raises(ValueError, match=msg):
779779
g.get_group(("foo", "bar", "baz"))
780780

781-
@pytest.mark.filterwarnings("ignore:Using 'observed:FutureWarning")
781+
@pytest.mark.filterwarnings("ignore:Grouping by a categorical:FutureWarning")
782782
def test_get_group_empty_bins(self, observed):
783783

784784
d = DataFrame([3, 1, 7, 6])

pandas/tests/groupby/transform/test_transform.py

+2-7
Original file line numberDiff line numberDiff line change
@@ -472,12 +472,7 @@ def test_groupby_transform_with_int():
472472

473473
# int case
474474
df = DataFrame(
475-
{
476-
"A": [1, 1, 1, 2, 2, 2],
477-
"B": 1,
478-
"C": [1, 2, 3, 1, 2, 3],
479-
"D": "foo",
480-
}
475+
{"A": [1, 1, 1, 2, 2, 2], "B": 1, "C": [1, 2, 3, 1, 2, 3], "D": "foo",}
481476
)
482477
with np.errstate(all="ignore"):
483478
result = df.groupby("A").transform(lambda x: (x - x.mean()) / x.std())
@@ -1153,7 +1148,7 @@ def test_transform_lambda_indexing():
11531148
tm.assert_frame_equal(result, expected)
11541149

11551150

1156-
@pytest.mark.filterwarnings("ignore:Using 'observed:FutureWarning")
1151+
@pytest.mark.filterwarnings("ignore:Grouping by a categorical:FutureWarning")
11571152
def test_categorical_and_not_categorical_key(observed):
11581153
# Checks that groupby-transform, when grouping by both a categorical
11591154
# and a non-categorical key, doesn't try to expand the output to include

0 commit comments

Comments
 (0)