Skip to content
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

BUG: Regression in pandas 1.2.3 in dataframe.__setitem__ #40204

Closed
2 of 3 tasks
galipremsagar opened this issue Mar 3, 2021 · 13 comments
Closed
2 of 3 tasks

BUG: Regression in pandas 1.2.3 in dataframe.__setitem__ #40204

galipremsagar opened this issue Mar 3, 2021 · 13 comments
Labels
Bug Closing Candidate May be closeable, needs more eyeballs Indexing Related to indexing on series/frames, not to indexes themselves Needs Discussion Requires discussion from core team before further action Regression Functionality that used to work in a prior pandas version

Comments

@galipremsagar
Copy link

  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • (optional) I have confirmed this bug exists on the master branch of pandas.


Code Sample, a copy-pastable example

>>> import pandas as pd
>>> pd.__version__
'1.2.3'
>>> df = pd.DataFrame({"a": [1, 2, 3]})
>>> df[[True, False, True]] = pd.DataFrame({"a": [-1, -2]})
>>> df
     a
0 -1.0
1  2.0
2  NaN

Whereas this was fixed in 1.2.2:

>>> import pandas as pd
>>> pd.__version__
'1.2.2'
>>> df = pd.DataFrame({"a": [1, 2, 3]})
>>> df[[True, False, True]] = pd.DataFrame({"a": [-1, -2]})
>>> df
   a
0 -1
1  2
2 -2

Problem description

Setting a column based on boolean values seems to be broken in 1.2.3. This previously was broken and fixed in 1.2.2.

Expected Output

>>> import pandas as pd
>>> df = pd.DataFrame({"a": [1, 2, 3]})
>>> df[[True, False, True]] = pd.DataFrame({"a": [-1, -2]})
>>> df
   a
0 -1
1  2
2 -2

Output of pd.show_versions()

INSTALLED VERSIONS

commit : f2c8480
python : 3.7.3.final.0
python-bits : 64
OS : Darwin
OS-release : 20.3.0
Version : Darwin Kernel Version 20.3.0: Thu Jan 21 00:07:06 PST 2021; root:xnu-7195.81.3~1/RELEASE_X86_64
machine : x86_64
processor : i386
byteorder : little
LC_ALL : None
LANG : None
LOCALE : en_US.UTF-8
pandas : 1.2.3
numpy : 1.19.2
pytz : 2020.1
dateutil : 2.8.1
pip : 20.2.3
setuptools : 50.3.0
Cython : None
pytest : None
hypothesis : 5.29.0
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 2.11.2
IPython : None
pandas_datareader: None
bs4 : None
bottleneck : None
fsspec : None
fastparquet : None
gcsfs : None
matplotlib : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : 1.0.1
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
numba : None

@galipremsagar galipremsagar added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Mar 3, 2021
@jreback jreback added Regression Functionality that used to work in a prior pandas version Indexing Related to indexing on series/frames, not to indexes themselves and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Mar 4, 2021
@jreback jreback added this to the 1.2.4 milestone Mar 4, 2021
@jreback
Copy link
Contributor

jreback commented Mar 4, 2021

cc @phofl

@phofl
Copy link
Member

phofl commented Mar 4, 2021

I am not sure if this is a Regression, This was caused because of the fix for #39931

I think setitem should align the rhs? I would say the regression was in 1.2, that this stopped aligning

cc @jreback @jbrockmendel

@phofl phofl added the Needs Discussion Requires discussion from core team before further action label Mar 4, 2021
@jorisvandenbossche
Copy link
Member

The "new" behaviour was indeed was happened before as well, eg with pandas 1.1.5:

In [3]: df[[True, False, True]] = pd.DataFrame({"a": [-1, -2]})

In [4]: df
Out[4]: 
     a
0 -1.0
1  2.0
2  NaN

In [5]: pd.__version__
Out[5]: '1.1.5'

@phofl
Copy link
Member

phofl commented Mar 5, 2021

Yes, we go through iloc here, which aligned in before 1.2 and stopped with 1.2. After this the behavior changed, which I think was wrong then for setitem. In 1.2.3 we restored the previous behavior for setitem again, which is more correct I think

@jbrockmendel
Copy link
Member

@phofl @jorisvandenbossche it sounds like the consensus is that the behavior on master (and 1.2.3) is correct and the behavior on 1.2.0 was incorrect?

@phofl
Copy link
Member

phofl commented Mar 31, 2021

Is correct imho

@phofl
Copy link
Member

phofl commented Apr 9, 2021

If nobody disagrees I would close this, since the behavior is correct again in 1.2.3

@phofl phofl added the Closing Candidate May be closeable, needs more eyeballs label Apr 9, 2021
@jreback
Copy link
Contributor

jreback commented Apr 9, 2021

as long as we have a test ok to close

@phofl
Copy link
Member

phofl commented Apr 9, 2021

test_setitem_boolean_mask_aligning covers this

@jreback
Copy link
Contributor

jreback commented Apr 9, 2021

great

@jreback jreback closed this as completed Apr 9, 2021
@phofl phofl modified the milestones: 1.2.4, No action Apr 9, 2021
@isVoid
Copy link
Contributor

isVoid commented Feb 2, 2022

Hi devs, current behavior is inconsistent between series and dataframe. For series:

In [12]: s = pd.Series([1, 2, 3], dtype="int64")

In [13]: s[[True, False, True]] = [88, 99]

In [14]: s
Out[14]: 
0    88
1     2
2    99
dtype: int64

Is this intended behavior?

@phofl
Copy link
Member

phofl commented Feb 2, 2022

Please clarify, your example is correct

@isVoid
Copy link
Contributor

isVoid commented Feb 2, 2022

Actually, no issue here. I noticed the example I supplied above has no index on value. When I do s[[True, False, True]] = pd.Series([88, 99]) I get the expected result. Sorry about the confusion!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Closing Candidate May be closeable, needs more eyeballs Indexing Related to indexing on series/frames, not to indexes themselves Needs Discussion Requires discussion from core team before further action Regression Functionality that used to work in a prior pandas version
Projects
None yet
Development

No branches or pull requests

6 participants