Skip to content

[Python] Handle NaN in DataFrame drop_duplicates, duplicated and nunique - #40571

Open
anishmehta24 wants to merge 1 commit into
apache:masterfrom
anishmehta24:fix/dataframe-duplicates-nan
Open

anishmehta24 wants to merge 1 commit into
apache:masterfrom
anishmehta24:fix/dataframe-duplicates-nan

Conversation

@anishmehta24

Copy link
Copy Markdown
Contributor

Fixes #40570

drop_duplicates and duplicated colocated duplicates with groupby, which drops NaN keys (and dropna=False doesn't work with multiple columns, #21014). They now add a hash of the compared columns as an index level and partition on that, so duplicates still end up together. -0.0 is normalized first since pandas treats it as a duplicate of 0.0 but it hashes differently. Series.nunique now honours dropna.

Added tests to frames_test.py (they fail on master with KeyError) and a CHANGES.md entry. frames_test.py and pandas_doctests_test.py pass.

Written with help from Claude.

@codecov

codecov Bot commented Oct 14, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 79.93%. Comparing base (76f6e6d) to head (4d80dbe).
⚠️ Report is 201 commits behind head on master.

Additional details and impacted files
@@              Coverage Diff              @@
##             master   #40571       +/-   ##
=============================================
+ Coverage     56.16%   79.93%   +23.76%     
=============================================
  Files          1124      488      -636     
  Lines        178336    82550    -95786     
  Branches       1489        0     -1489     
=============================================
- Hits         100170    65983    -34187     
+ Misses        75645    16567    -59078     
+ Partials       2521        0     -2521     
Flag Coverage Δ
go ?
java ?
python 79.93% <100.00%> (+0.37%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@github-actions

Copy link
Copy Markdown
Contributor

Assigning reviewers:

R: @shunping for label python.
Backup reviewer: claudevdm (if the main reviewer is unresponsive, you can escalate to them by tagging their username)

Note: If you would like to opt out of this review, comment assign to next reviewer.

Available commands:

  • stop reviewer notifications - opt out of the automated review tooling
  • remind me after tests pass - tag the comment author after tests pass
  • waiting on author - shift the attention set back to the author (any comment or push by the author will return the attention set to the reviewers)

The PR bot will only process comments in the main thread (not review comments).

@frame_base.args_to_kwargs(pd.Series)
@frame_base.populate_defaults(pd.Series)
def nunique(self, **kwargs):
def nunique(self, dropna, **kwargs):

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Looks like we changed the api signature here. Is it going to cause a compatibility issue?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

No, callers see the same signature as pandas, Series.nunique(dropna=True). args_to_kwargs maps arguments by the pandas signature, and populate_defaults only fills defaults for parameters the method names, so dropna has to be listed to get its default of True. Before, it went into **kwargs and was ignored. nunique(), nunique(False), nunique(True) and nunique(dropna=False) all match pandas (checked with _run_test). It's the same pattern as quantile(self, q, **kwargs).

Comment thread CHANGES.md Outdated
* (Python) Fixed stateful DoFns with side inputs sometimes taking the timer key coder from a side input instead of the main input, which could make the worker fail to decode timer keys with `Unknown type tag` ([#40374](https://github.com/apache/beam/issues/40374)).
* (Python) `Duration` built from float seconds now rounds to the nearest microsecond instead of truncating, which could lose a microsecond ([#40263](https://github.com/apache/beam/issues/40263)).
* (Python) `ReadFromText` with `escapechar` no longer skips a delimiter that directly follows an escaped delimiter, which merged two records into one ([#40459](https://github.com/apache/beam/issues/40459)).
<<<<<<< ours

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Conflict in CHANGES.md needs to be resolved.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Sorry, conflict markers slipped in when I rebased. Rebased on master and fixed in 4d80dbe.

@anishmehta24
anishmehta24 force-pushed the fix/dataframe-duplicates-nan branch from 37f569f to 4d80dbe Compare October 16, 2026 11:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: DataFrame API drop_duplicates and duplicated fail on rows with NaN

2 participants