13
13
14
14
import numpy as np
15
15
16
+ from pandas ._config .config import get_option
17
+
16
18
from pandas ._libs import lib
17
19
from pandas ._libs .tslibs import OutOfBoundsDatetime
18
20
from pandas .errors import (
@@ -621,6 +623,7 @@ def dropna(self) -> bool:
621
623
@cache_readonly
622
624
def _codes_and_uniques (self ) -> tuple [npt .NDArray [np .signedinteger ], ArrayLike ]:
623
625
uniques : ArrayLike
626
+ unspecified_dropna = self ._dropna is lib .no_default
624
627
if self ._passed_categorical :
625
628
# we make a CategoricalIndex out of the cat grouper
626
629
# preserving the categories / ordered attributes;
@@ -662,7 +665,7 @@ def _codes_and_uniques(self) -> tuple[npt.NDArray[np.signedinteger], ArrayLike]:
662
665
# NA code is based on first appearance, increment higher codes
663
666
codes = np .where (codes >= na_code , codes + 1 , codes )
664
667
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 :
666
669
warnings .warn (
667
670
_NULL_KEY_MESSAGE ,
668
671
NullKeyWarning ,
@@ -688,8 +691,11 @@ def _codes_and_uniques(self) -> tuple[npt.NDArray[np.signedinteger], ArrayLike]:
688
691
codes , uniques = algorithms .factorize ( # type: ignore[assignment]
689
692
self .grouping_vector , sort = self ._sort , use_na_sentinel = self .dropna
690
693
)
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
+ ):
693
699
warnings .warn (
694
700
_NULL_KEY_MESSAGE ,
695
701
NullKeyWarning ,
0 commit comments