Skip to content

Boolean indexing into a series with assignment #30567

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

Closed
IvanUkhov opened this issue Dec 30, 2019 · 2 comments · Fixed by #30580
Closed

Boolean indexing into a series with assignment #30567

IvanUkhov opened this issue Dec 30, 2019 · 2 comments · Fixed by #30580
Assignees
Labels
Bug Indexing Related to indexing on series/frames, not to indexes themselves
Milestone

Comments

@IvanUkhov
Copy link

I am curious if the following is the intended behavior:

x = pd.Series([None] * 10)
y = [False] * 3 + [True] * 5 + [False] * 2
x[y] = range(5)
x

# 0     None
# 1     None
# 2     None
# 3        3
# 4        4
# 5        0
# 6        1
# 7        2
# 8     None
# 9     None

The expectation was that it would work as with x.loc[y] = range(5):

# 0    None
# 1    None
# 2    None
# 3       0
# 4       1
# 5       2
# 6       3
# 7       4
# 8    None
# 9    None

If so, where can one read about the logic behind?

LANG : en_US.UTF-8 LOCALE : en_US.UTF-8

pandas : 0.25.3
numpy : 1.17.4
pytz : 2018.4
dateutil : 2.5.0
pip : 19.2.3
setuptools : 41.2.0
Cython : None
pytest : 5.1.2
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : 3.8.0
html5lib : 1.0.1
pymysql : None
psycopg2 : None
jinja2 : 2.8
IPython : 6.5.0
pandas_datareader: None
xlsxwriter : None
pandas_gbq : None
pyarrow : 0.15.1
bs4 : 4.7.1
numexpr : None
xarray : None
fastparquet : None
tables : None
s3fs : None
odfpy : None
pytables : None
xlrd : None
gcsfs : None
xlwt : None
sqlalchemy : 1.3.4
scipy : 1.3.1
openpyxl : None
lxml.etree : 3.8.0
matplotlib : 3.0.3
bottleneck : None

@fujiaxiang
Copy link
Member

I believe this is a bug.

A quick investigation shows this is caused by the use of np.putmask to set the values. This is an issue when the values provided (i.e range(5)) has different shape with the entire series (array of 10 elements), and numpy extend and repeats the values, the we are actually using [0, 1, 2, 3, 4, 0, 1, 2, 3, 4] to replace the original series and hence caused this issue.

Will take a look at the fixes.

@fujiaxiang
Copy link
Member

take

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 a pull request may close this issue.

3 participants