Skip to content

Fix median of datetime64 data - #11965

Open
Swayam-27 wants to merge 4 commits into
pydata:mainfrom
Swayam-27:fix-datetime-median
Open

Swayam-27 wants to merge 4 commits into
pydata:mainfrom
Swayam-27:fix-datetime-median

Conversation

@Swayam-27

Copy link
Copy Markdown

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

AI Disclosure

  • This PR contains AI-generated content.
    • I have tested any AI-generated content in my PR.
    • I take responsibility for any AI-generated content in my PR.
      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.

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
@github-actions github-actions Bot added the topic-arrays related to flexible array support label Dec 1, 2026
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>
@headtr1ck

Copy link
Copy Markdown
Collaborator

Thanks @Swayam-27! I pushed a commit on top that changes how the offset is taken and shares the code with mean. What we found while reviewing:

  • _datetime_nanreduce takes the minimum of the whole array, computed in float64. Slices far away from that minimum get large distances, which lose precision as floats: the median of ["2030-01-01T00:00:00.000000001", "2030-01-01T00:00:00.000000003"] next to a row in 2000 came out as ...000 instead of ...002. With dask, every output chunk also depends on a reduction over the whole array.
  • The offset is now the minimum along the reduced axes, taken exactly on the int64 ticks. The distances are computed as uint64, which holds the full datetime64 range (e.g. 1678 to 2260 at ns resolution).
  • mean and median of both datetime64 and timedelta64 now go through the same helper. This also fixes median of timedelta64, which didn't skip NaT by default (unlike mean) and overflowed for large values, and makes mean more precise.
  • We also considered going through quantile or an exact integer median. Both were slower or more code, so this keeps the float median, which is exact for spans up to about 104 days per slice at ns resolution.

[This is Claude Code on behalf of Michael Niklas]

@Swayam-27

Copy link
Copy Markdown
Author

Thanks @headtr1ck, makes sense

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@headtr1ck headtr1ck left a comment

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.

Thanks

@headtr1ck headtr1ck added the plan to merge Final call for comments label Dec 2, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

plan to merge Final call for comments topic-arrays related to flexible array support

Projects

None yet

Development

Successfully merging this pull request may close these issues.

median() raises on datetime64 data and breaks Dataset.median()

2 participants