@@ -1182,17 +1182,26 @@ def test_grouping_by_key_is_in_axis():
1182
1182
tm .assert_frame_equal (result , expected )
1183
1183
1184
1184
1185
- def test_groupby_any_with_timedelta (self ):
1186
- # Create a DataFrame with Timedelta and NaT values
1187
- df = DataFrame ({
1185
+ def test_groupby_any_with_timedelta ():
1186
+ # Create a DataFrame with Timedelta and NaT values
1187
+ df = DataFrame (
1188
+ {
1188
1189
"A" : ["foo" , "foo" , "bar" , "bar" ],
1189
- "B" : [pd .Timedelta (1 , unit = 'D' ), pd .NaT , pd .Timedelta (2 , unit = 'D' ), pd .NaT ]
1190
- })
1190
+ "B" : [pd .Timedelta (1 , unit = "D" ), pd .NaT , pd .Timedelta (2 , unit = "D" ), pd .NaT ],
1191
+ }
1192
+ )
1191
1193
1192
- # Group by column A and check if any Timedelta exists (i.e., non-NaT)
1193
- result = df .groupby ("A" )["B" ].any ()
1194
+ # Group by column A with sorting enabled and check if any Timedelta exists
1195
+ result = df .groupby ("A" , sort = True )["B" ].any ()
1194
1196
1195
- # Expected result: groups with only NaT should return False, others should return True
1196
- expected = Series ([True , False ], index = ["foo" , "bar" ], name = "B" )
1197
+ # Corrected expected result: groups with only NaT should return False, else True
1198
+ expected = Series ([True , True ], index = ["foo" , "bar" ], name = "B" )
1197
1199
1198
- tm .assert_series_equal (result , expected )
1200
+ # Set the expected index name to match the result
1201
+ expected .index .name = "A"
1202
+
1203
+ # Sort the expected result to match the order of result
1204
+ expected = expected .sort_index ()
1205
+
1206
+ # Assert that the result matches the expected output
1207
+ tm .assert_series_equal (result , expected )
0 commit comments