Skip to content

ENH: Allow NEP 42 dtypes to use np.save and np.load - #24142

Merged
seberg merged 6 commits into
numpy:mainfrom
ngoldbaum:save-load-nep42-dtypes
Jul 12, 2023
Merged

seberg merged 6 commits into
numpy:mainfrom
ngoldbaum:save-load-nep42-dtypes

Conversation

@ngoldbaum

Copy link
Copy Markdown
Member

Fixes #24110

First, this makes it so that by default NEP 42 dtypes can't be pickled unless the dtype has a pickle implementation. Currently numpy will pickle them, but won't be able to unpickle them because the type code written to disk is invalid. Erroring is an improvement over writing corrupt files, I think.

Second, if a type can be pickled, this makes it so that np.save will save the array using pickle and will lie that the dtype is object (see @rkern's suggestion). I've made it so if this happens a UserWarning will get printed. Unfortunately there's no way to indicate in the file that this really isn't an object array, so I can't do much on the load side to detect when this happens. Hopefully the UserWarning at save time is enough? I think adding a way to indicate in the file that we're not really storing an object array would require a revision to the npy file format, which ideally I'd like to avoid.

Last, added a pickle implementation to the scaled float test dtype and then added a test doing a round-trip save and load with a scale float array.

@seberg

seberg commented Jul 10, 2023

Copy link
Copy Markdown
Member

Tests are unhappy. @rkern would you prefer more here? I would be fine with doing this this even if it is not ideal; since I don't think it blocks the right solution and there is that warning anyway.
(Also, this does seem like the right thing to do for a DType which fails to implement whatever future solution we implement.)

@rkern

rkern commented Jul 10, 2023

Copy link
Copy Markdown
Member

Yeah, I think it's a fine short-term workaround. The warnings are appreciated.

@ngoldbaum

Copy link
Copy Markdown
Member Author

Tests are unhappy.

Good now, I was unknowingly using a too-new pytest feature.

@seberg seberg left a comment

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.

I think the test/SF reduce implementation can be drastically simplified. The rest looks nice good though, thanks.

@seberg
seberg merged commit c6a449c into numpy:main Jul 12, 2023
@seberg

seberg commented Jul 12, 2023

Copy link
Copy Markdown
Member

Thanks Nathan.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ENH: dtype API needs a hook in np.save and np.load

3 participants