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

Skip to content

BUG: Fix #28730 histogram error with nan objects #28781

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

R1nesh
Copy link

@R1nesh R1nesh commented Apr 20, 2025

Description

BUG: Fix #28730 histogram error when input has object dtype with NaNs

This Fixes a bug in np.histogram where object-dtyped arrays containing np.nan would cause a wrong output. Now such object arrays are cast to float before processing, fixing the bug.

Example

Before fix:

print(np.histogram(np.asarray([0., 1., np.nan], dtype=object), bins=np.arange(0, 5))[0])

print(np.histogram(np.asarray([0., 1., np.nan], dtype=float), bins=np.arange(0, 5))[0])

Output:
[1 1 0 1]
[1 1 0 0]

Returned incorrect bins because of improper handling of np.nan in object-dtype arrays

After fix:

print(np.histogram(np.asarray([0., 1., np.nan], dtype=object), bins=np.arange(0, 5))[0])

print(np.histogram(np.asarray([0., 1., np.nan], dtype=float), bins=np.arange(0, 5))[0])

Output:
[1 1 0 0]
[1 1 0 0]

Closes: #28730

There was a bug in histogram if the dtype of "a" (array like) was an object and contains nans.
@ngoldbaum
Copy link
Member

This needs tests. Given that CI passed that indicates that our test coverage for this case is bad. Please add a test that would fail on main but that your code change will fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

BUG: histogram error with nan objects
2 participants