-
-
Notifications
You must be signed in to change notification settings - Fork 18k
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
BUG: groupby().any() returns true for groups with timedelta all NaT #59782
Conversation
Thanks for the pull request but this is already being worked on in #59750. Closing to let that contributor finish the effort, but we can reopen this if it goes stale |
Hi @mroeschke , could this be reopened? |
pandas/core/groupby/ops.py
Outdated
# Fix for NaT handling: ensure NaT is treated as False in any() and all() | ||
if self.how in ["any", "all"]: | ||
# Set NaT (which is represented as the smallest int64) to False (0) | ||
nat_mask = values == np.iinfo(np.int64).min | ||
values[nat_mask] = 0 # Treat NaT as False |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The linked issue suggests moving up the determination of mask
below to be prior to viewing the values as int64
on L375. Is there a reason this doesn't work?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, it appears i went in the wrong direction with my implementation. Just did a commit with this better solution. Could you give me some pointers on all these check failures?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Merging main should fix the CI issues. However, I do not believe the changes here will fix the issue and will break other tests. We need to move the determination of mask = isna(values)
to happen before values = values.view("int64")
but not change the order of any other logic.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking good! This needs a line in the whatsnew. Can you add a line to docs/source/whatsnew/v3.0.0.rst
in the groupby subsection of the bug fixes.
|
||
|
||
def test_groupby_any_with_timedelta(): | ||
# Create a DataFrame with a single column containing a Timedelta and NaT |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The comments in this test repeat the code verbatim. Can you remove them.
In addition, can you add a reference to the issue as the first line of this test.
def test_groupby_any_with_timedelta():
# GH#59712
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added these changes.
Hi @rhshadrach, how's this? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm
Thanks @Petroncini! |
Thanks for all the help and guidance, @rhshadrach! |
PR Summary:
This PR addresses an issue where groupby().any() returns True for groups where all Timedelta values are NaT, which is inconsistent with other dtypes (like integers and strings). For non-Timedelta dtypes, groupby().any() correctly returns False when all values in a group are null.
Reproduction:
python
import pandas as pd
Current behavior: incorrect output for all NaT groups
df = pd.DataFrame([pd.Timedelta(1), pd.NaT]).groupby([0, 1]).any()
For other dtypes (integers and strings):
python
df = pd.DataFrame([1, None]).groupby([0, 1]).any() # Returns False
df = pd.DataFrame(["a", None]).groupby([0, 1]).any() # Returns False
Expected behavior for Timedelta values: groupby().any() should return False for groups where all values are NaT.
Fix Details:
Tests Added:
Closing:
This PR fixes the inconsistent behavior of groupby().any() for Timedelta values and improves the overall correctness of the method. Let me know if there are any additional changes needed.
Resolves: #59712
Note: I am a complete novice to open source and this is my first ever PR. I'm taking an open source systems class is college and we're suppposed to attempt to contribute to a existing project we use and I chose pandas as it is a tool that has helped me alot. So i ask for any guidance anyone can give me about any potential mistakes or improvements i can make here. Appreciate it!