Skip to content

Commit 41131a1

Browse files
committed
add mode.null_grouper_warning option
1 parent cee2378 commit 41131a1

File tree

3 files changed

+31
-3
lines changed

3 files changed

+31
-3
lines changed

pandas/core/config_init.py

+16
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,22 @@ def is_terminal() -> bool:
457457
)
458458

459459

460+
null_grouper_warning = """
461+
: string
462+
Whether to show or hide NullKeyWarning if default grouping would result in a
463+
null group key being dropped,
464+
The default is False
465+
"""
466+
467+
with cf.config_prefix("mode"):
468+
cf.register_option(
469+
"null_grouper_warning",
470+
False,
471+
null_grouper_warning,
472+
validator=is_bool,
473+
)
474+
475+
460476
string_storage_doc = """
461477
: string
462478
The default storage for StringDtype.

pandas/core/groupby/grouper.py

+9-3
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
import numpy as np
1515

16+
from pandas._config.config import get_option
17+
1618
from pandas._libs import lib
1719
from pandas._libs.tslibs import OutOfBoundsDatetime
1820
from pandas.errors import (
@@ -621,6 +623,7 @@ def dropna(self) -> bool:
621623
@cache_readonly
622624
def _codes_and_uniques(self) -> tuple[npt.NDArray[np.signedinteger], ArrayLike]:
623625
uniques: ArrayLike
626+
unspecified_dropna = self._dropna is lib.no_default
624627
if self._passed_categorical:
625628
# we make a CategoricalIndex out of the cat grouper
626629
# preserving the categories / ordered attributes;
@@ -662,7 +665,7 @@ def _codes_and_uniques(self) -> tuple[npt.NDArray[np.signedinteger], ArrayLike]:
662665
# NA code is based on first appearance, increment higher codes
663666
codes = np.where(codes >= na_code, codes + 1, codes)
664667
codes = np.where(na_mask, na_code, codes)
665-
elif self._dropna is lib.no_default:
668+
elif get_option("null_grouper_warning") and unspecified_dropna:
666669
warnings.warn(
667670
_NULL_KEY_MESSAGE,
668671
NullKeyWarning,
@@ -688,8 +691,11 @@ def _codes_and_uniques(self) -> tuple[npt.NDArray[np.signedinteger], ArrayLike]:
688691
codes, uniques = algorithms.factorize( # type: ignore[assignment]
689692
self.grouping_vector, sort=self._sort, use_na_sentinel=self.dropna
690693
)
691-
# TODO: Is `min(codes)` or `-1 in codes` faster?
692-
if self._dropna is lib.no_default and (codes == -1).any():
694+
if (
695+
get_option("null_grouper_warning")
696+
and unspecified_dropna
697+
and codes.min() == -1
698+
):
693699
warnings.warn(
694700
_NULL_KEY_MESSAGE,
695701
NullKeyWarning,

pandas/tests/groupby/test_groupby_dropna.py

+6
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@
1010
from pandas.tests.groupby import get_groupby_method_args
1111

1212

13+
@pytest.fixture(scope="module", autouse=True)
14+
def setup_warnings():
15+
with pd.option_context("mode.null_grouper_warning", True):
16+
yield
17+
18+
1319
@pytest.mark.parametrize(
1420
"dropna, tuples, outputs",
1521
[

0 commit comments

Comments
 (0)