Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ENH: groupby missing data in index #28097

Merged
merged 3 commits into from
Oct 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions doc/source/whatsnew/v1.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,8 @@ Plotting
Groupby/resample/rolling
^^^^^^^^^^^^^^^^^^^^^^^^

-
- Bug in :meth:`DataFrame.groupby` with multiple groups where an ``IndexError`` would be raised if any group contained all NA values (:issue:`20519`)
- Bug in :meth:`DataFrame.rolling` not allowing for rolling over datetimes when ``axis=1`` (:issue: `28192`)
- Bug in :meth:`DataFrame.rolling` not allowing rolling over multi-index levels (:issue: `15584`).
- Bug in :meth:`DataFrame.rolling` not allowing rolling on monotonic decreasing time indexes (:issue: `19248`).
Expand Down
5 changes: 4 additions & 1 deletion pandas/core/indexes/multi.py
Original file line number Diff line number Diff line change
Expand Up @@ -1308,7 +1308,10 @@ def _get_grouper_for_level(self, mapper, level):
# Remove unobserved levels from level_index
level_index = level_index.take(uniques)

grouper = level_index.take(codes)
if len(level_index):
grouper = level_index.take(codes)
else:
grouper = level_index.take(codes, fill_value=True)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm, can you eliminate the branch, and just always pass fill_value=True ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If remove the branch, for example,

df = DataFrame([["x", 1, 10], ["y", 2, 20]], columns=["A", "B", "C"]).set_index(["A", "B"])
result = df.groupby(level=["A", "B"]).sum() 

raise exception.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually i think this is incorrect

@WillAyd wasn’t done here

this branch can be simplified i think

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry misread the approval above

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@proost do you mind simplifying this in a follow up PR?


return grouper, codes, level_index

Expand Down
18 changes: 18 additions & 0 deletions pandas/tests/groupby/test_grouping.py
Original file line number Diff line number Diff line change
Expand Up @@ -628,6 +628,24 @@ def test_groupby_empty(self):
# check name
assert s.groupby(s).grouper.names == ["name"]

def test_groupby_level_index_value_all_na(self):
# issue 20519
df = DataFrame(
[["x", np.nan, 10], [None, np.nan, 20]], columns=["A", "B", "C"]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I cast df["B"] = df["B"].astype("datetime64") before doing the set_index, I get a different error on the groupby call (in master). Should that be fixed by this PR? If so, please test.

side-note, I'd find this easier to follow in smaller steps:

df = pd.DataFrame(...)
df = df.set_index(["A", "B"])
gb = df.groupby(level=["A", "B"])
result = gb.sum()

Especially relevant as it is the gb = ... line that raises in master

Copy link
Contributor Author

@proost proost Oct 13, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jbrockmendel
I'm bit confused. you df["B"] = df["B"].astype("datetime64") means df["B"] = df["B"].astype("datetime64[ns]") write?
in master,
df = pd.DataFrame(...)
df["B"] = df["B"].astype("datetime64[ns]")
df = df.set_index(["A", "B"])
gb = df.groupby(level=["A", "B"])
and
df = pd.DataFrame(...)
df = df.set_index(["A", "B"])
gb = df.groupby(level=["A", "B"])
raise same IndexError :cannot do a non-empty take from an empty axes.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm bit confused. you df["B"] = df["B"].astype("datetime64") means df["B"] = df["B"].astype("datetime64[ns]") write?

Yes, I meant to write "datetime64[ns]" and not just "datetime64".

raise same IndexError

Huh, not sure how I got to a different error. My bad.

).set_index(["A", "B"])
result = df.groupby(level=["A", "B"]).sum()
expected = DataFrame(
WillAyd marked this conversation as resolved.
Show resolved Hide resolved
data=[],
index=MultiIndex(
levels=[Index(["x"], dtype="object"), Index([], dtype="float64")],
codes=[[], []],
names=["A", "B"],
),
columns=["C"],
dtype="int64",
)
tm.assert_frame_equal(result, expected)


# get_group
# --------------------------------
Expand Down