@@ -545,19 +545,62 @@ def test_multiindex_columns_empty_level(self):
545
545
546
546
df = DataFrame ([[1 , "A" ]], columns = midx )
547
547
548
+ msg = "`groups` by one element list returns scalar is deprecated"
548
549
grouped = df .groupby ("to filter" ).groups
549
550
assert grouped ["A" ] == [0 ]
550
551
551
- msg = "`groups` by one element list returns scalar is deprecated"
552
-
553
552
with tm .assert_produces_warning (FutureWarning , match = msg ):
554
553
grouped = df .groupby ([("to filter" , "" )]).groups
555
- assert grouped [( "A" ,) ] == [0 ]
554
+ assert grouped ["A" ] == [0 ]
556
555
557
556
df = DataFrame ([[1 , "A" ], [2 , "B" ]], columns = midx )
558
557
558
+ expected = df .groupby ("to filter" ).groups
559
+ with tm .assert_produces_warning (FutureWarning , match = msg ):
560
+ result = df .groupby ([("to filter" , "" )]).groups
561
+ assert result == expected
562
+
563
+ df = DataFrame ([[1 , "A" ], [2 , "A" ]], columns = midx )
564
+
565
+ expected = df .groupby ("to filter" ).groups
566
+ with tm .assert_produces_warning (FutureWarning , match = msg ):
567
+ result = df .groupby ([("to filter" , "" )]).groups
568
+ tm .assert_dict_equal (result , expected )
569
+
570
+ def test_groupby_multiindex_tuple (self ):
571
+ # GH 17979, GH#59179
572
+ df = DataFrame (
573
+ [[1 , 2 , 3 , 4 ], [3 , 4 , 5 , 6 ], [1 , 4 , 2 , 3 ]],
574
+ columns = MultiIndex .from_arrays ([["a" , "b" , "b" , "c" ], [1 , 1 , 2 , 2 ]]),
575
+ )
576
+
577
+ msg = "`groups` by one element list returns scalar is deprecated"
578
+ with tm .assert_produces_warning (FutureWarning , match = msg ):
579
+ expected = df .groupby ([("b" , 1 )]).groups
580
+ result = df .groupby (("b" , 1 )).groups
581
+ tm .assert_dict_equal (expected , result )
582
+
583
+ df2 = DataFrame (
584
+ df .values ,
585
+ columns = MultiIndex .from_arrays (
586
+ [["a" , "b" , "b" , "c" ], ["d" , "d" , "e" , "e" ]]
587
+ ),
588
+ )
589
+
590
+ with tm .assert_produces_warning (FutureWarning , match = msg ):
591
+ expected = df2 .groupby ([("b" , "d" )]).groups
592
+ result = df .groupby (("b" , 1 )).groups
593
+ tm .assert_dict_equal (expected , result )
594
+
595
+ df3 = DataFrame (df .values , columns = [("a" , "d" ), ("b" , "d" ), ("b" , "e" ), "c" ])
596
+
597
+ with tm .assert_produces_warning (FutureWarning , match = msg ):
598
+ expected = df3 .groupby ([("b" , "d" )]).groups
599
+ result = df .groupby (("b" , 1 )).groups
600
+ tm .assert_dict_equal (expected , result )
601
+
559
602
def test_groupby_multiindex_partial_indexing_equivalence (self ):
560
- # GH 17977
603
+ # GH 17977, GH#59179
561
604
df = DataFrame (
562
605
[[1 , 2 , 3 , 4 ], [3 , 4 , 5 , 6 ], [1 , 4 , 2 , 3 ]],
563
606
columns = MultiIndex .from_arrays ([["a" , "b" , "b" , "c" ], [1 , 1 , 2 , 2 ]]),
@@ -584,7 +627,6 @@ def test_groupby_multiindex_partial_indexing_equivalence(self):
584
627
tm .assert_frame_equal (expected_max , result_max )
585
628
586
629
msg = "`groups` by one element list returns scalar is deprecated"
587
-
588
630
with tm .assert_produces_warning (FutureWarning , match = msg ):
589
631
expected_groups = df .groupby ([("a" , 1 )])[[("b" , 1 ), ("b" , 2 )]].groups
590
632
result_groups = df .groupby ([("a" , 1 )])["b" ].groups
@@ -690,16 +732,15 @@ def test_grouping_labels(self, multiindex_dataframe_random_data):
690
732
tm .assert_almost_equal (grouped ._grouper .codes [0 ], exp_labels )
691
733
692
734
def test_list_grouper_with_nat (self ):
693
- # GH 14715
735
+ # GH 14715, GH#59179
694
736
df = DataFrame ({"date" : date_range ("1/1/2011" , periods = 365 , freq = "D" )})
695
737
df .iloc [- 1 ] = pd .NaT
696
738
grouper = Grouper (key = "date" , freq = "YS" )
697
739
msg = "`groups` by one element list returns scalar is deprecated"
698
740
699
741
# Grouper in a list grouping
700
742
result = df .groupby ([grouper ])
701
- expected = {(Timestamp ("2011-01-01" ),): list (range (364 )), (pd .NaT ,): [364 ]}
702
-
743
+ expected = {Timestamp ("2011-01-01" ): Index (list (range (364 )))}
703
744
with tm .assert_produces_warning (FutureWarning , match = msg ):
704
745
result = result .groups
705
746
tm .assert_dict_equal (result , expected )
@@ -976,7 +1017,7 @@ def test_groups(self, df):
976
1017
assert groups is grouped .groups # caching works
977
1018
978
1019
for k , v in grouped .groups .items ():
979
- assert (df .loc [v ]["A" ] == k [ 0 ] ).all ()
1020
+ assert (df .loc [v ]["A" ] == k ).all ()
980
1021
981
1022
grouped = df .groupby (["A" , "B" ])
982
1023
groups = grouped .groups
0 commit comments