Skip to content

Commit bddb335

Browse files
committed
Added enhancement feature to Group by without by raising TypError
1 parent dc8401a commit bddb335

File tree

3 files changed

+55
-10
lines changed

3 files changed

+55
-10
lines changed

pandas/core/frame.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9153,8 +9153,9 @@ def groupby(
91539153
) -> DataFrameGroupBy:
91549154
from pandas.core.groupby.generic import DataFrameGroupBy
91559155

9156-
if level is None and by is None:
9157-
raise TypeError("You have to supply one of 'by' and 'level'")
9156+
if level is None and (by is None or by == []):
9157+
by = Series(0, index=self.index)
9158+
# raise TypeError("You have to supply one of 'by' and 'level'")
91589159

91599160
return DataFrameGroupBy(
91609161
obj=self,

pandas/core/series.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1973,8 +1973,9 @@ def groupby(
19731973
) -> SeriesGroupBy:
19741974
from pandas.core.groupby.generic import SeriesGroupBy
19751975

1976-
if level is None and by is None:
1977-
raise TypeError("You have to supply one of 'by' and 'level'")
1976+
if level is None and (by is None or by == []):
1977+
by = Series(0, index=self.index)
1978+
# raise TypeError("You have to supply one of 'by' and 'level'")
19781979
if not as_index:
19791980
raise TypeError("as_index=False only valid with DataFrame")
19801981

pandas/tests/groupby/test_grouping.py

Lines changed: 49 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)