Skip to content

BUG: Series __setitem__ gives wrong result with bool indexer #30580

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

Merged

Conversation

fujiaxiang
Copy link
Member

@fujiaxiang fujiaxiang commented Dec 31, 2019

Series.setitem gives wrong result with bool indexer and when length of new data matches the number of Trues and new data is neither a Series nor a numpy array

Series.__setitem__ gives wrong result with bool indexer and when length of new data matches the number of Trues and new data is neither a Series nor a numpy array
or len(new) == 1
):
if mask[mask].shape[-1] == len(new): # GH 30567
np.place(new_values, mask, new)
Copy link
Member

Choose a reason for hiding this comment

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

i had to look up what np.place was. is there a more common idiom we can use here?

Copy link
Member Author

Choose a reason for hiding this comment

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

I can't find any close alternative from numpy. I think np.place in the right choice as it does exactly what we need here (it places all given values to the masked locations rather than assigning masked values to masked locations). Besides, it is designed to specifically work with book masks and that's what we are dealing with here.

@jreback jreback added Bug Indexing Related to indexing on series/frames, not to indexes themselves labels Jan 1, 2020
@jreback jreback added this to the 1.0 milestone Jan 2, 2020
@jreback jreback merged commit 6b4ee6e into pandas-dev:master Jan 2, 2020
@jreback
Copy link
Contributor

jreback commented Jan 2, 2020

thanks @fujiaxiang very nice

@IvanUkhov
Copy link

@fujiaxiang, thank you so much for this!

@fujiaxiang fujiaxiang deleted the bug_series_setitem_with_bool_indexer branch January 3, 2020 11:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Indexing Related to indexing on series/frames, not to indexes themselves
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Boolean indexing into a series with assignment
4 participants