Skip to content

Commit 2bcbd25

Browse files
authored
Bug: GroupBy raising error with None in first level of MultiIndex (#47351)
* Bug: GroupBy raising error with None in first level of MultiIndex * Add test * Change whatsnew
1 parent 1bc9197 commit 2bcbd25

File tree

3 files changed

+24
-0
lines changed

3 files changed

+24
-0
lines changed

doc/source/whatsnew/v1.5.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -927,6 +927,7 @@ Groupby/resample/rolling
927927
- Bug in :meth:`DataFrameGroupBy.cumsum` with ``skipna=False`` giving incorrect results (:issue:`46216`)
928928
- Bug in :meth:`.GroupBy.cumsum` with ``timedelta64[ns]`` dtype failing to recognize ``NaT`` as a null value (:issue:`46216`)
929929
- Bug in :meth:`.GroupBy.cummin` and :meth:`.GroupBy.cummax` with nullable dtypes incorrectly altering the original data in place (:issue:`46220`)
930+
- Bug in :meth:`DataFrame.groupby` raising error when ``None`` is in first level of :class:`MultiIndex` (:issue:`47348`)
930931
- Bug in :meth:`.GroupBy.cummax` with ``int64`` dtype with leading value being the smallest possible int64 (:issue:`46382`)
931932
- Bug in :meth:`.GroupBy.max` with empty groups and ``uint64`` dtype incorrectly raising ``RuntimeError`` (:issue:`46408`)
932933
- Bug in :meth:`.GroupBy.apply` would fail when ``func`` was a string and args or kwargs were supplied (:issue:`46479`)

pandas/core/groupby/grouper.py

+4
Original file line numberDiff line numberDiff line change
@@ -831,7 +831,11 @@ def get_grouper(
831831

832832
# if the actual grouper should be obj[key]
833833
def is_in_axis(key) -> bool:
834+
834835
if not _is_label_like(key):
836+
if obj.ndim == 1:
837+
return False
838+
835839
# items -> .columns for DataFrame, .index for Series
836840
items = obj.axes[-1]
837841
try:

pandas/tests/groupby/test_groupby.py

+19
Original file line numberDiff line numberDiff line change
@@ -2776,3 +2776,22 @@ def test_by_column_values_with_same_starting_value():
27762776
).set_index("Name")
27772777

27782778
tm.assert_frame_equal(result, expected_result)
2779+
2780+
2781+
def test_groupby_none_in_first_mi_level():
2782+
# GH#47348
2783+
arr = [[None, 1, 0, 1], [2, 3, 2, 3]]
2784+
ser = Series(1, index=MultiIndex.from_arrays(arr, names=["a", "b"]))
2785+
result = ser.groupby(level=[0, 1]).sum()
2786+
expected = Series(
2787+
[1, 2], MultiIndex.from_tuples([(0.0, 2), (1.0, 3)], names=["a", "b"])
2788+
)
2789+
tm.assert_series_equal(result, expected)
2790+
2791+
2792+
def test_groupby_none_column_name():
2793+
# GH#47348
2794+
df = DataFrame({None: [1, 1, 2, 2], "b": [1, 1, 2, 3], "c": [4, 5, 6, 7]})
2795+
result = df.groupby(by=[None]).sum()
2796+
expected = DataFrame({"b": [2, 5], "c": [9, 13]}, index=Index([1, 2], name=None))
2797+
tm.assert_frame_equal(result, expected)

0 commit comments

Comments
 (0)