@@ -545,31 +545,38 @@ 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
- grouped = df .groupby ([("to filter" , "" )]).groups
552
+ with tm .assert_produces_warning (FutureWarning , match = msg ):
553
+ grouped = df .groupby ([("to filter" , "" )]).groups
552
554
assert grouped ["A" ] == [0 ]
553
555
554
556
df = DataFrame ([[1 , "A" ], [2 , "B" ]], columns = midx )
555
557
556
558
expected = df .groupby ("to filter" ).groups
557
- result = df .groupby ([("to filter" , "" )]).groups
559
+ with tm .assert_produces_warning (FutureWarning , match = msg ):
560
+ result = df .groupby ([("to filter" , "" )]).groups
558
561
assert result == expected
559
562
560
563
df = DataFrame ([[1 , "A" ], [2 , "A" ]], columns = midx )
561
564
562
565
expected = df .groupby ("to filter" ).groups
563
- result = df .groupby ([("to filter" , "" )]).groups
566
+ with tm .assert_produces_warning (FutureWarning , match = msg ):
567
+ result = df .groupby ([("to filter" , "" )]).groups
564
568
tm .assert_dict_equal (result , expected )
565
569
566
570
def test_groupby_multiindex_tuple (self ):
567
- # GH 17979
571
+ # GH 17979, GH#59179
568
572
df = DataFrame (
569
573
[[1 , 2 , 3 , 4 ], [3 , 4 , 5 , 6 ], [1 , 4 , 2 , 3 ]],
570
574
columns = MultiIndex .from_arrays ([["a" , "b" , "b" , "c" ], [1 , 1 , 2 , 2 ]]),
571
575
)
572
- expected = df .groupby ([("b" , 1 )]).groups
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
573
580
result = df .groupby (("b" , 1 )).groups
574
581
tm .assert_dict_equal (expected , result )
575
582
@@ -579,17 +586,21 @@ def test_groupby_multiindex_tuple(self):
579
586
[["a" , "b" , "b" , "c" ], ["d" , "d" , "e" , "e" ]]
580
587
),
581
588
)
582
- expected = df2 .groupby ([("b" , "d" )]).groups
589
+
590
+ with tm .assert_produces_warning (FutureWarning , match = msg ):
591
+ expected = df2 .groupby ([("b" , "d" )]).groups
583
592
result = df .groupby (("b" , 1 )).groups
584
593
tm .assert_dict_equal (expected , result )
585
594
586
595
df3 = DataFrame (df .values , columns = [("a" , "d" ), ("b" , "d" ), ("b" , "e" ), "c" ])
587
- expected = df3 .groupby ([("b" , "d" )]).groups
596
+
597
+ with tm .assert_produces_warning (FutureWarning , match = msg ):
598
+ expected = df3 .groupby ([("b" , "d" )]).groups
588
599
result = df .groupby (("b" , 1 )).groups
589
600
tm .assert_dict_equal (expected , result )
590
601
591
602
def test_groupby_multiindex_partial_indexing_equivalence (self ):
592
- # GH 17977
603
+ # GH 17977, GH#59179
593
604
df = DataFrame (
594
605
[[1 , 2 , 3 , 4 ], [3 , 4 , 5 , 6 ], [1 , 4 , 2 , 3 ]],
595
606
columns = MultiIndex .from_arrays ([["a" , "b" , "b" , "c" ], [1 , 1 , 2 , 2 ]]),
@@ -615,8 +626,10 @@ def test_groupby_multiindex_partial_indexing_equivalence(self):
615
626
result_max = df .groupby ([("a" , 1 )])["b" ].max ()
616
627
tm .assert_frame_equal (expected_max , result_max )
617
628
618
- expected_groups = df .groupby ([("a" , 1 )])[[("b" , 1 ), ("b" , 2 )]].groups
619
- result_groups = df .groupby ([("a" , 1 )])["b" ].groups
629
+ msg = "`groups` by one element list returns scalar is deprecated"
630
+ with tm .assert_produces_warning (FutureWarning , match = msg ):
631
+ expected_groups = df .groupby ([("a" , 1 )])[[("b" , 1 ), ("b" , 2 )]].groups
632
+ result_groups = df .groupby ([("a" , 1 )])["b" ].groups
620
633
tm .assert_dict_equal (expected_groups , result_groups )
621
634
622
635
def test_groupby_level (self , sort , multiindex_dataframe_random_data , df ):
@@ -719,15 +732,18 @@ def test_grouping_labels(self, multiindex_dataframe_random_data):
719
732
tm .assert_almost_equal (grouped ._grouper .codes [0 ], exp_labels )
720
733
721
734
def test_list_grouper_with_nat (self ):
722
- # GH 14715
735
+ # GH 14715, GH#59179
723
736
df = DataFrame ({"date" : date_range ("1/1/2011" , periods = 365 , freq = "D" )})
724
737
df .iloc [- 1 ] = pd .NaT
725
738
grouper = Grouper (key = "date" , freq = "YS" )
739
+ msg = "`groups` by one element list returns scalar is deprecated"
726
740
727
741
# Grouper in a list grouping
728
- result = df .groupby ([grouper ])
742
+ gb = df .groupby ([grouper ])
729
743
expected = {Timestamp ("2011-01-01" ): Index (list (range (364 )))}
730
- tm .assert_dict_equal (result .groups , expected )
744
+ with tm .assert_produces_warning (FutureWarning , match = msg ):
745
+ result = gb .groups
746
+ tm .assert_dict_equal (result , expected )
731
747
732
748
# Test case without a list
733
749
result = df .groupby (grouper )
@@ -994,17 +1010,20 @@ def test_gb_key_len_equal_axis_len(self):
994
1010
class TestIteration :
995
1011
def test_groups (self , df ):
996
1012
grouped = df .groupby (["A" ])
997
- groups = grouped .groups
998
- assert groups is grouped .groups # caching works
1013
+ msg = "`groups` by one element list returns scalar is deprecated"
1014
+
1015
+ with tm .assert_produces_warning (FutureWarning , match = msg ):
1016
+ groups = grouped .groups
1017
+ assert groups is grouped .groups # caching works
999
1018
1000
- for k , v in grouped . groups .items ():
1019
+ for k , v in groups .items ():
1001
1020
assert (df .loc [v ]["A" ] == k ).all ()
1002
1021
1003
1022
grouped = df .groupby (["A" , "B" ])
1004
1023
groups = grouped .groups
1005
1024
assert groups is grouped .groups # caching works
1006
1025
1007
- for k , v in grouped . groups .items ():
1026
+ for k , v in groups .items ():
1008
1027
assert (df .loc [v ]["A" ] == k [0 ]).all ()
1009
1028
assert (df .loc [v ]["B" ] == k [1 ]).all ()
1010
1029
0 commit comments