@@ -695,17 +695,60 @@ def test_groupby_level_with_nas(self, sort):
695695 expected = Series ([6.0 , 18.0 ], index = [0.0 , 1.0 ])
696696 tm .assert_series_equal (result , expected )
697697
698+ def test_groupby_without_by (self ):
699+ # Test DataFrame.groupby() without any fields (global aggregation)
700+ df = DataFrame ({"A" : [1 , 2 , 3 , 4 ], "B" : [10 , 20 , 30 , 40 ]})
701+
702+ # Test basic aggregation with no fields
703+ result = df .groupby ().sum ()
704+ expected = df .sum ().to_frame ().T
705+ tm .assert_frame_equal (result , expected )
706+
707+ # Test with multiple aggregations
708+ result = df .groupby ().agg (["sum" , "mean" ])
709+ expected = df .agg (["sum" , "mean" ])
710+ tm .assert_frame_equal (result , expected )
711+
712+ # Test Series.groupby() without any fields
713+ s = Series ([1 , 2 , 3 , 4 ])
714+ result = s .groupby ().sum ()
715+ expected = Series ([10 ]) # Sum of the values
716+ tm .assert_series_equal (result , expected )
717+
718+ # Test with conditional logic - should work with None/empty list too
719+ groupby_fields = None
720+ result = df .groupby (groupby_fields ).sum ()
721+ expected = df .sum ().to_frame ().T
722+ tm .assert_frame_equal (result , expected )
723+
724+ # Test with empty list
725+ result = df .groupby ([]).sum ()
726+ tm .assert_frame_equal (result , expected )
727+
698728 def test_groupby_args (self , multiindex_dataframe_random_data ):
699729 # PR8618 and issue 8015
700730 frame = multiindex_dataframe_random_data
701731
702- msg = "You have to supply one of 'by' and 'level'"
703- with pytest .raises (TypeError , match = msg ):
704- frame .groupby ()
732+ # No longer expecting errors when groupby() is called with no arguments
733+ # This is now valid behavior that puts all rows in a single group
734+ result = frame .groupby ().sum ()
735+ expected = frame .sum ().to_frame ().T
736+ tm .assert_frame_equal (result , expected )
705737
706- msg = "You have to supply one of 'by' and 'level'"
707- with pytest .raises (TypeError , match = msg ):
708- frame .groupby (by = None , level = None )
738+ result = frame .groupby (by = None , level = None ).sum ()
739+ tm .assert_frame_equal (result , expected )
740+
741+ # def test_groupby_args(self, multiindex_dataframe_random_data):
742+ # # PR8618 and issue 8015
743+ # frame = multiindex_dataframe_random_data
744+
745+ # msg = "You have to supply one of 'by' and 'level'"
746+ # with pytest.raises(TypeError, match=msg):
747+ # frame.groupby()
748+
749+ # msg = "You have to supply one of 'by' and 'level'"
750+ # with pytest.raises(TypeError, match=msg):
751+ # frame.groupby(by=None, level=None)
709752
710753 @pytest .mark .parametrize (
711754 "sort,labels" ,
0 commit comments