Repository navigation
Conversation
median() passed datetime64 arrays straight to numpy, which can't add datetimes, so it raised for DataArrays of dates and for any Dataset containing a datetime variable. Take the median of the distances to the earliest date instead and add it back, as mean() already does. Closes pydata#11964
5 tasks done
6 tasks done
Reduce the float distances to the minimum along the reduced axes instead of a global offset. The minimum is taken exactly on the int64 ticks, and the distances are computed as uint64 so they hold the full datetime64 range. This keeps slices that are far apart exact, avoids an extra pass over the whole array with dask, makes timedelta median skip NaT like mean and fixes overflows for large timedelta values. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Collaborator
|
Thanks @Swayam-27! I pushed a commit on top that changes how the offset is taken and shares the code with
[This is Claude Code on behalf of Michael Niklas] |
Author
|
Thanks @headtr1ck, makes sense |
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
This fixes median() on datetime64 data, which raised an error for every input, and
Dataset.median(), which failed completely if any variable was a date.
It works the same way mean() does. Each date is turned into its distance from the earliest
date, the median of those distances is taken, and then the earliest date is added back to
get a date again.
I used the earliest date as the reference point rather than mean's offset, which gets
rounded to the start of the year. With nanosecond data, that rounded offset makes the
distances big enough that float maths loses precision, and the median came out one
nanosecond off. Using the earliest date keeps the distances small, so the result stays exact.
Timedelta and numeric data still go through the old code path, so nothing changes for them.
Fixes #11964
Checklist
median()raises on datetime64 data and breaksDataset.median()#11964whats-new.rstAI Disclosure
Tools: Claude Code. It found the bug and wrote the fix and tests at my direction. I reviewed every change, ran the tests and understand the code.