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

Skip to content

BUG: Fix unwrap function for object arrays with large numbers. #28911

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

sim0es
Copy link

@sim0es sim0es commented May 6, 2025

Implemented special handling for object arrays in np.unwrap to correctly unwrap sequences containing very large numbers (e.g., >10^50). The fix properly implements period-based unwrapping logic for object arrays, ensuring consistent increments that respect the specified period parameter. Fixes #27686.

Implemented special handling for object arrays in np.unwrap to correctly unwrap sequences containing very large numbers (e.g., >10^50).
The fix properly implements period-based unwrapping logic for object arrays, ensuring consistent increments that respect the specified period parameter.
Fixes numpy#27686.
dd = diff(p, axis=axis)

if p.dtype == np.object_:
initial_value = p[0]
Copy link
Contributor

Choose a reason for hiding this comment

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

What if p is empty? Or p is a 2D array?

initial_value = p[0]
unwrapped = [initial_value]
for val in p[1:]:
unwrapped.append(unwrapped[-1] + 1)
Copy link
Contributor

@eendebakpt eendebakpt May 6, 2025

Choose a reason for hiding this comment

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

Appending in a for loop will be much slower than the current implementation for larger arrays.

Do you know the root cause of the problem for bigints? Perhaps we can modify the algorithm at that point. I checked that the first step (e.g. np.diff(p, axis=axis)) works fine

# test with different magnitudes
arr6 = np.arange(0, 30, 5).astype(np.object_) + (10**100)
unwrapped6 = np.unwrap(arr6, period=4)
assert_array_equal(np.diff(unwrapped6), 1)
Copy link
Member

Choose a reason for hiding this comment

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

please use pytest parameterization to avoid repitition - this is very hard to read.

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: np.unwrap returns incorrect values for bigints
3 participants