@@ -384,6 +384,33 @@ def test_groupby_nan_included():
384
384
assert list (result .keys ())[0 :2 ] == ["g1" , "g2" ]
385
385
386
386
387
+ @pytest .mark .parametrize (
388
+ "by" ,
389
+ [
390
+ pytest .param ("group" , id = "column" ),
391
+ pytest .param (pd .Series (["g1" , np .nan , "g1" , "g2" , np .nan ]), id = "Series" ),
392
+ pytest .param ("_index" , id = "index" ),
393
+ ],
394
+ )
395
+ @pytest .mark .parametrize ("dropna" , [True , False , None ])
396
+ def test_groupby_nan_included_warns (by , dropna ):
397
+ # GH 61339
398
+ data = {"group" : ["g1" , np .nan , "g1" , "g2" , np .nan ], "B" : [0 , 1 , 2 , 3 , 4 ]}
399
+ df = pd .DataFrame (data )
400
+ if by == "_index" :
401
+ df = df .set_index ("group" )
402
+
403
+ kwargs = {}
404
+ warning_type = pd .errors .NullKeyWarning
405
+ if dropna is not None :
406
+ kwargs = {"dropna" : dropna }
407
+ warning_type = None
408
+
409
+ with tm .assert_produces_warning (warning_type ):
410
+ grouped = df .groupby (by , ** kwargs )
411
+ result = grouped .indices # noqa:F841
412
+
413
+
387
414
def test_groupby_drop_nan_with_multi_index ():
388
415
# GH 39895
389
416
df = pd .DataFrame ([[np .nan , 0 , 1 ]], columns = ["a" , "b" , "c" ])
0 commit comments