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

BUG: groupby().any() returns true for groups with timedelta all NaT #59782

Merged
merged 14 commits into from
Oct 1, 2024

Conversation

Petroncini
Copy link
Contributor

@Petroncini Petroncini commented Sep 11, 2024

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:

Modified the _call_cython_op function in the groupby aggregation to correctly handle Timedelta types. Now, when all values in a group are NaT, the any() method will return False.
This ensures consistent behavior across all dtypes, including Timedelta.

Tests Added:

A new test has been added to check that groupby().any() returns False for groups with only NaT Timedelta values and True for groups with at least one non-NaT Timedelta.

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!

@mroeschke
Copy link
Member

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

@mroeschke mroeschke closed this Sep 11, 2024
@Petroncini
Copy link
Contributor Author

Hi @mroeschke , could this be reopened?

@rhshadrach rhshadrach reopened this Sep 27, 2024
Comment on lines 378 to 382
# 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
Copy link
Member

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?

Copy link
Contributor Author

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?

Copy link
Member

@rhshadrach rhshadrach left a 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.

Copy link
Member

@rhshadrach rhshadrach left a 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
Copy link
Member

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I added these changes.

@Petroncini
Copy link
Contributor Author

Hi @rhshadrach, how's this?

Copy link
Member

@rhshadrach rhshadrach left a comment

Choose a reason for hiding this comment

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

lgtm

@rhshadrach rhshadrach changed the title BUG: Adressed issue where groupby().any() return true for groups with timedelta all NULL BUG: groupby().any() returns true for groups with timedelta all NaT Oct 1, 2024
@rhshadrach rhshadrach added Bug Groupby Timedelta Timedelta data type Reduction Operations sum, mean, min, max, etc. labels Oct 1, 2024
@rhshadrach rhshadrach added the Missing-data np.nan, pd.NaT, pd.NA, dropna, isnull, interpolate label Oct 1, 2024
@rhshadrach rhshadrach added this to the 3.0 milestone Oct 1, 2024
@rhshadrach rhshadrach merged commit f598670 into pandas-dev:main Oct 1, 2024
65 checks passed
@rhshadrach
Copy link
Member

Thanks @Petroncini!

@Petroncini
Copy link
Contributor Author

Thanks for all the help and guidance, @rhshadrach!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Groupby Missing-data np.nan, pd.NaT, pd.NA, dropna, isnull, interpolate Reduction Operations sum, mean, min, max, etc. Timedelta Timedelta data type
Projects
None yet
Development

Successfully merging this pull request may close these issues.

BUG: groupby().any() returns True instead of False for groups where timedelta column is all null
3 participants