Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Fix Axes.hist crash for numpy timedelta64 inputs#31203

Merged
timhoffm merged 10 commits intomatplotlib:mainfrom
jayaprajapatii:fix-hist-timedelta-npinf
Mar 11, 2026
Merged

Fix Axes.hist crash for numpy timedelta64 inputs#31203
timhoffm merged 10 commits intomatplotlib:mainfrom
jayaprajapatii:fix-hist-timedelta-npinf

Conversation

@jayaprajapatii
Copy link
Copy Markdown
Contributor

Closes #31182

Summary

Axes.hist fails when passed arrays of numpy.timedelta64 This happens because the histogram implementation performs numeric operations (range estimation, binning) that are not directly compatible with timedelta64 dtypes.

What this PR does:

  • Detects numpy.timedelta64 inputs in Axes.hist.
  • Converts timedelta64 inputs to a numeric representation prior to range estimation and binning.
  • Preserves existing histogram behavior for all other numeric types.
  • Adds a regression test to ensure timedelta64 inputs no longer crash.
  • Timedelta values are normalized to days for the purpose of histogram range estimation and binning.

Example:

import numpy as np
import matplotlib.pyplot as plt
arr = np.array([1,2,5,7], dtype="timedelta64[D]")
plt.hist(arr)
plt.show()

  • Previously this resulted in a failure. With this change, the histogram renders correctly.

@github-actions
Copy link
Copy Markdown

Thank you for opening your first PR into Matplotlib!

If you have not heard from us in a week or so, please leave a new comment below and that should bring it to our attention. Most of our reviewers are volunteers and sometimes things fall through the cracks.

You can also join us on gitter for real-time discussion.

For details on testing, writing docs, and our review process, please see the developer guide.
Please let us know if (and how) you use AI, it will help us give you better feedback on your PR.

We strive to be a welcoming and open project. Please follow our Code of Conduct.

Comment thread lib/matplotlib/axes/_axes.py Outdated

new_x = []
for xi in x:
arr = np.asarray(xi)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This is unnecessary. cbook._reshape_2D() already returns a list of arrays.

So at least

x = [arr / np.timedelta64(1, 'D') if np.issubdtype(arr.dtype, np.timedelta64) else arr
     for arr in x]

is possible.


Another question to be investigated: Is arr / np.timedelta64(1, 'D') equivalent to arr.astype(float) for timedelta arrays? If so, would a general x = [arr.astype(float) for arr in x] be reasonable?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Is arr / np.timedelta64(1, 'D') equivalent to arr.astype(float) for timedelta arrays?

No, which is the whole problem here. If it was possible to cast timedelta64 to float, the comparison would work.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

astype(float) is valid, and I think is the better solution. Dividing by one day is going to give you tiny numbers if you started with seconds, and is going to error if you started with months or years.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

astype(float) is unsafe so if you choose to cast in this function you should be explicit in the documentation

Comment thread lib/matplotlib/axes/_axes.py Outdated
Comment thread lib/matplotlib/axes/_axes.py Outdated
Comment thread lib/matplotlib/axes/_axes.py Outdated
Comment thread lib/matplotlib/axes/_axes.py Outdated
nx = len(x) # number of datasets
nx = len(x)

for arr in x:
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Overall, I'm not in favor of checking for all kinds of erronous inputs. This is not good, but it's possibly the least bad option (other than making timedelta work properlyI) because timedelta is an accepted input in other cases.

@jayaprajapatii
Copy link
Copy Markdown
Contributor Author

The windows Tk backend test (test_canvas_focus) timed out in CI. This seems unrelated to the histogram changes.
please let me know if you would like me to investigate further or if the failed Azure jobs could be re-run.

@timhoffm
Copy link
Copy Markdown
Member

timhoffm commented Mar 3, 2026

The timeouts are a known CI issue and unrelated: #30851

Comment thread lib/matplotlib/axes/_axes.py Outdated
Copy link
Copy Markdown
Contributor

@scottshambaugh scottshambaugh left a comment

Choose a reason for hiding this comment

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

Please correct the timedelata spelling typo, and indent the closing parens to align with the raise blocks.

I'm a little wary of an O(n) type check for object arrays. I think checking just the first element is fine, as mixed object arrays fail anyways.

Eg this fails:

import matplotlib.pyplot as plt
import numpy as np

data = np.array([1, 'hello', 3.5], dtype=object)
fig, ax = plt.subplots()
ax.hist(data)  # Fails

print(np.array([1, 'hello', 3.5]))
# Note that without an explicit dtype, these all get cast to strings
# ['1' 'hello' '3.5']

@timhoffm timhoffm self-requested a review March 5, 2026 05:55
Comment thread lib/matplotlib/axes/_axes.py Outdated
Comment thread lib/matplotlib/axes/_axes.py Outdated
Comment thread lib/matplotlib/tests/test_axes.py
Comment thread lib/matplotlib/axes/_axes.py Outdated
Comment thread lib/matplotlib/tests/test_axes.py Outdated
Comment thread lib/matplotlib/axes/_axes.py Outdated
@timhoffm timhoffm merged commit c722367 into matplotlib:main Mar 11, 2026
38 of 40 checks passed
@github-project-automation github-project-automation Bot moved this from Needs decision to Merged in First Time Contributors Mar 11, 2026
@timhoffm
Copy link
Copy Markdown
Member

Thanks @jayaprajapatii !

@QuLogic QuLogic added this to the v3.11.0 milestone Mar 11, 2026
@jayaprajapatii
Copy link
Copy Markdown
Contributor Author

Thankyouu:) for the review and merge ! Looking forward to contributing more to matplotlib.

andreas16700 added a commit to andreas16700/matplotlib that referenced this pull request Mar 16, 2026
andreas16700 added a commit to andreas16700/matplotlib that referenced this pull request Mar 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Development

Successfully merging this pull request may close these issues.

[Bug]: ax.hist() fails on sequence of timedeltas due to comparison with np.inf

8 participants