@@ -140,9 +140,9 @@ def test_len():
140
140
def test_len_nan_group ():
141
141
# issue 11016
142
142
df = DataFrame ({"a" : [np .nan ] * 3 , "b" : [1 , 2 , 3 ]})
143
- assert len (df .groupby ("a" )) == 0
143
+ assert len (df .groupby ("a" , dropna = True )) == 0
144
144
assert len (df .groupby ("b" )) == 3
145
- assert len (df .groupby (["a" , "b" ])) == 0
145
+ assert len (df .groupby (["a" , "b" ], dropna = True )) == 0
146
146
147
147
148
148
def test_groupby_timedelta_median ():
@@ -922,6 +922,7 @@ def test_groupby_complex_numbers():
922
922
tm .assert_frame_equal (result , expected )
923
923
924
924
925
+ @pytest .mark .filterwarnings ("ignore::pandas.errors.NullKeyWarning" )
925
926
def test_groupby_series_indexed_differently ():
926
927
s1 = Series (
927
928
[5.0 , - 9.0 , 4.0 , 100.0 , - 5.0 , 55.0 , 6.7 ],
@@ -1215,7 +1216,7 @@ def test_groupby_nat_exclude():
1215
1216
"str" : [np .nan , "a" , np .nan , "a" , np .nan , "a" , np .nan , "b" ],
1216
1217
}
1217
1218
)
1218
- grouped = df .groupby ("dt" )
1219
+ grouped = df .groupby ("dt" , dropna = True )
1219
1220
1220
1221
expected = [
1221
1222
RangeIndex (start = 1 , stop = 13 , step = 6 ),
@@ -1253,7 +1254,7 @@ def test_groupby_nat_exclude():
1253
1254
assert nan_df ["nat" ].dtype == "datetime64[s]"
1254
1255
1255
1256
for key in ["nan" , "nat" ]:
1256
- grouped = nan_df .groupby (key )
1257
+ grouped = nan_df .groupby (key , dropna = True )
1257
1258
assert grouped .groups == {}
1258
1259
assert grouped .ngroups == 0
1259
1260
assert grouped .indices == {}
@@ -1266,7 +1267,7 @@ def test_groupby_nat_exclude():
1266
1267
def test_groupby_two_group_keys_all_nan ():
1267
1268
# GH #36842: Grouping over two group keys shouldn't raise an error
1268
1269
df = DataFrame ({"a" : [np .nan , np .nan ], "b" : [np .nan , np .nan ], "c" : [1 , 2 ]})
1269
- result = df .groupby (["a" , "b" ]).indices
1270
+ result = df .groupby (["a" , "b" ], dropna = True ).indices
1270
1271
assert result == {}
1271
1272
1272
1273
@@ -2050,7 +2051,7 @@ def test_groupby_only_none_group():
2050
2051
# see GH21624
2051
2052
# this was crashing with "ValueError: Length of passed values is 1, index implies 0"
2052
2053
df = DataFrame ({"g" : [None ], "x" : 1 })
2053
- actual = df .groupby ("g" )["x" ].transform ("sum" )
2054
+ actual = df .groupby ("g" , dropna = True )["x" ].transform ("sum" )
2054
2055
expected = Series ([np .nan ], name = "x" )
2055
2056
2056
2057
tm .assert_series_equal (actual , expected )
@@ -2295,7 +2296,7 @@ def test_groupby_mean_duplicate_index(rand_series_with_duplicate_datetimeindex):
2295
2296
def test_groupby_all_nan_groups_drop ():
2296
2297
# GH 15036
2297
2298
s = Series ([1 , 2 , 3 ], [np .nan , np .nan , np .nan ])
2298
- result = s .groupby (s .index ).sum ()
2299
+ result = s .groupby (s .index , dropna = True ).sum ()
2299
2300
expected = Series ([], index = Index ([], dtype = np .float64 ), dtype = np .int64 )
2300
2301
tm .assert_series_equal (result , expected )
2301
2302
@@ -2459,7 +2460,7 @@ def test_groupby_none_in_first_mi_level():
2459
2460
# GH#47348
2460
2461
arr = [[None , 1 , 0 , 1 ], [2 , 3 , 2 , 3 ]]
2461
2462
ser = Series (1 , index = MultiIndex .from_arrays (arr , names = ["a" , "b" ]))
2462
- result = ser .groupby (level = [0 , 1 ]).sum ()
2463
+ result = ser .groupby (level = [0 , 1 ], dropna = True ).sum ()
2463
2464
expected = Series (
2464
2465
[1 , 2 ], MultiIndex .from_tuples ([(0.0 , 2 ), (1.0 , 3 )], names = ["a" , "b" ])
2465
2466
)
@@ -2632,9 +2633,9 @@ def test_groupby_method_drop_na(method):
2632
2633
df = DataFrame ({"A" : ["a" , np .nan , "b" , np .nan , "c" ], "B" : range (5 )})
2633
2634
2634
2635
if method == "nth" :
2635
- result = getattr (df .groupby ("A" ), method )(n = 0 )
2636
+ result = getattr (df .groupby ("A" , dropna = True ), method )(n = 0 )
2636
2637
else :
2637
- result = getattr (df .groupby ("A" ), method )()
2638
+ result = getattr (df .groupby ("A" , dropna = True ), method )()
2638
2639
2639
2640
if method in ["first" , "last" ]:
2640
2641
expected = DataFrame ({"B" : [0 , 2 , 4 ]}).set_index (
0 commit comments