@@ -575,7 +575,7 @@ def test_grouper_column_and_index_sugar(self):
575
575
expected = df_single .groupby (pd .Grouper (level = 'inner' )).mean ()
576
576
assert_frame_equal (result , expected )
577
577
578
- def test_grouper_column_takes_precedence_over_level (self ):
578
+ def test_grouper_column_takes_precedence_over_level_with_warning (self ):
579
579
# GH 5677, when a string passed as the `by` parameter
580
580
# matches a column and an index level the column takes
581
581
# precedence
@@ -592,74 +592,94 @@ def test_grouper_column_takes_precedence_over_level(self):
592
592
df_single_both = df_multi_both .reset_index ('outer' )
593
593
594
594
# Group MultiIndex by single key
595
- result = df_multi_both .groupby ('inner' ).mean ()
596
- expected = df_multi_both .groupby (pd .Grouper (key = 'inner' )).mean ()
595
+ with tm .assert_produces_warning (FutureWarning , check_stacklevel = False ):
596
+ result = df_multi_both .groupby ('inner' ).mean ()
597
+
598
+ expected = df_multi_both .groupby ([pd .Grouper (key = 'inner' )]).mean ()
599
+
597
600
assert_frame_equal (result , expected )
598
601
not_expected = df_multi_both .groupby (pd .Grouper (level = 'inner' )).mean ()
599
- assert not result .index .equals (not_expected .index )
602
+ self . assertFalse ( result .index .equals (not_expected .index ) )
600
603
601
604
# Group single Index by single key
602
- result = df_single_both .groupby ('inner' ).mean ()
603
- expected = df_single_both .groupby (pd .Grouper (key = 'inner' )).mean ()
605
+ with tm .assert_produces_warning (FutureWarning , check_stacklevel = False ):
606
+ result = df_single_both .groupby ('inner' ).mean ()
607
+
608
+ expected = df_single_both .groupby ([pd .Grouper (key = 'inner' )]).mean ()
609
+
604
610
assert_frame_equal (result , expected )
605
611
not_expected = df_single_both .groupby (pd .Grouper (level = 'inner' )).mean ()
606
- assert not result .index .equals (not_expected .index )
612
+ self . assertFalse ( result .index .equals (not_expected .index ) )
607
613
608
614
# Group MultiIndex by single key list
609
- result = df_multi_both .groupby (['inner' ]).mean ()
610
- expected = df_multi_both .groupby (pd .Grouper (key = 'inner' )).mean ()
615
+ with tm .assert_produces_warning (FutureWarning , check_stacklevel = False ):
616
+ result = df_multi_both .groupby (['inner' ]).mean ()
617
+
618
+ expected = df_multi_both .groupby ([pd .Grouper (key = 'inner' )]).mean ()
619
+
611
620
assert_frame_equal (result , expected )
612
621
not_expected = df_multi_both .groupby (pd .Grouper (level = 'inner' )).mean ()
613
- assert not result .index .equals (not_expected .index )
622
+ self . assertFalse ( result .index .equals (not_expected .index ) )
614
623
615
624
# Group single Index by single key list
616
- result = df_single_both .groupby (['inner' ]).mean ()
617
- expected = df_single_both .groupby (pd .Grouper (key = 'inner' )).mean ()
625
+ with tm .assert_produces_warning (FutureWarning , check_stacklevel = False ):
626
+ result = df_single_both .groupby (['inner' ]).mean ()
627
+
628
+ expected = df_single_both .groupby ([pd .Grouper (key = 'inner' )]).mean ()
629
+
618
630
assert_frame_equal (result , expected )
619
631
not_expected = df_single_both .groupby (pd .Grouper (level = 'inner' )).mean ()
620
- assert not result .index .equals (not_expected .index )
632
+ self . assertFalse ( result .index .equals (not_expected .index ) )
621
633
622
634
# Group MultiIndex by two keys (1)
623
- result = df_multi_both .groupby (['B' , 'inner' ]).mean ()
635
+ with tm .assert_produces_warning (FutureWarning , check_stacklevel = False ):
636
+ result = df_multi_both .groupby (['B' , 'inner' ]).mean ()
637
+
624
638
expected = df_multi_both .groupby (['B' ,
625
- pd .Grouper (key = 'inner' )]).mean ()
639
+ pd .Grouper (key = 'inner' )]).mean ()
626
640
assert_frame_equal (result , expected )
627
641
628
642
not_expected = df_multi_both .groupby (['B' ,
629
643
pd .Grouper (level = 'inner' )
630
644
]).mean ()
631
- assert not result .index .equals (not_expected .index )
645
+ self . assertFalse ( result .index .equals (not_expected .index ) )
632
646
633
647
# Group MultiIndex by two keys (2)
634
- result = df_multi_both .groupby (['inner' , 'B' ]).mean ()
648
+ with tm .assert_produces_warning (FutureWarning , check_stacklevel = False ):
649
+ result = df_multi_both .groupby (['inner' , 'B' ]).mean ()
650
+
635
651
expected = df_multi_both .groupby ([pd .Grouper (key = 'inner' ),
636
652
'B' ]).mean ()
637
653
assert_frame_equal (result , expected )
638
654
639
655
not_expected = df_multi_both .groupby ([pd .Grouper (level = 'inner' ),
640
656
'B' ]).mean ()
641
- assert not result .index .equals (not_expected .index )
657
+ self . assertFalse ( result .index .equals (not_expected .index ) )
642
658
643
659
# Group single Index by two keys (1)
644
- result = df_single_both .groupby (['B' , 'inner' ]).mean ()
660
+ with tm .assert_produces_warning (FutureWarning , check_stacklevel = False ):
661
+ result = df_single_both .groupby (['B' , 'inner' ]).mean ()
662
+
645
663
expected = df_single_both .groupby (['B' ,
646
- pd .Grouper (key = 'inner' )]).mean ()
664
+ pd .Grouper (key = 'inner' )]).mean ()
647
665
assert_frame_equal (result , expected )
648
666
649
667
not_expected = df_single_both .groupby (['B' ,
650
668
pd .Grouper (level = 'inner' )
651
669
]).mean ()
652
- assert not result .index .equals (not_expected .index )
670
+ self . assertFalse ( result .index .equals (not_expected .index ) )
653
671
654
672
# Group single Index by two keys (2)
655
- result = df_single_both .groupby (['inner' , 'B' ]).mean ()
673
+ with tm .assert_produces_warning (FutureWarning , check_stacklevel = False ):
674
+ result = df_single_both .groupby (['inner' , 'B' ]).mean ()
675
+
656
676
expected = df_single_both .groupby ([pd .Grouper (key = 'inner' ),
657
677
'B' ]).mean ()
658
678
assert_frame_equal (result , expected )
659
679
660
680
not_expected = df_single_both .groupby ([pd .Grouper (level = 'inner' ),
661
681
'B' ]).mean ()
662
- assert not result .index .equals (not_expected .index )
682
+ self . assertFalse ( result .index .equals (not_expected .index ) )
663
683
664
684
def test_grouper_getting_correct_binner (self ):
665
685
0 commit comments