1313
1414import numpy as np
1515
16+ from pandas ._config .config import get_option
17+
1618from pandas ._libs import lib
1719from pandas ._libs .tslibs import OutOfBoundsDatetime
1820from 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 ,
0 commit comments