Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
BUG: Fix copy semantics in
__array__
#60046New 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
BUG: Fix copy semantics in
__array__
#60046Changes from 6 commits
1c5195c
2183861
404827b
ec08728
4ac6323
9b6c209
77058df
6799f55
4217baf
5e4cb87
357f8a0
9927903
3b000be
421f904
d70405e
5289a82
File filter
Filter by extension
Conversations
Jump to
There are no files selected for viewing
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If
lib.is_range_indexer(self._codes)
i.e. theself.categories._values
are all unique then, a copy could be avoided?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably, but I left it out, it doesn't seem vital to try and improve it. Also fixed things up, because
take_nd
should presumably always return a copy.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I did not understand why this is needed. If dtypes match, NumPy should make it a no-op? If dtype is None, it is the same as not passing.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, also not sure why this is here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One more issue I noticed while doing the backport: for a similar case in generic.py we changed the
copy is False
tocopy is not True
, which I think we should have done here as well.Will try to come up with a test case that would fail because of this, and do a follow-up tomorrow.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, ouch. This is clearly hard to get right without tests :/.
The release snippet looks fine to me.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It also only matters for the
copy=None
case, which AFAIU is quite new, and so we didn't cover that when adding the logic here for making the resulting array read-onlyThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking closer, I think this is right? Note that it says
copy is False or ...
. Ifcopy is False
we don't have to check whether it is a view (because otherwise it would have errored)?EDIT: Not sure if it's worth to skip the check though. It seems like it may be more confusing then anything...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, that's a good point. I assume the
arr = np.array(values, dtype=dtype, copy=copy)
line above will already have errored in case ofcopy=False
if a zero-copy conversion was not possible.So indeed the
or
is fine: ifcopy=False
we knowarr
is always a view, and forcopy=None
(the one remaining case) we haveastype_is_view
to check it.Maybe it's then in
generic.py
that we could change thecopy is not True and ..
tocopy is False or ..
(just to use a similar pattern in both cases)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking once more at it, we actually already have tests for this, because we test that we get a read-only array using
np.asarray(ser)
, which essentially means passingcopy=None
.I expanded the test with
np.array(ser, copy=False)
to also explicitly cover thecopy=False
case (and ensure this branch setting writeable to False is covered for that case) -> #60191