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: Index alignment behaviour #39931

Closed
3 tasks done
complexsum opened this issue Feb 20, 2021 · 5 comments · Fixed by #39944
Closed
3 tasks done

BUG: Index alignment behaviour #39931

complexsum opened this issue Feb 20, 2021 · 5 comments · Fixed by #39944
Labels
Bug Indexing Related to indexing on series/frames, not to indexes themselves Regression Functionality that used to work in a prior pandas version
Milestone

Comments

@complexsum
Copy link

complexsum commented Feb 20, 2021

  • 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.


Note: Please read this guide detailing how to provide the necessary information for us to reproduce your bug.

Code Sample

np.random.seed(1)
df = pd.DataFrame(np.random.randint(0, 100, (10, 2)), columns=['A', 'B'])

>>> df
    A   B
0  37  12
1  72   9
2  75   5
3  79  64
4  16   1
5  76  71
6   6  25
7  50  20
8  18  84
9  11  28

mask = df['A'] >= 40
>>> df[mask]
    A   B
1  72   9
2  75   5
3  79  64
5  76  71
7  50  20

>>> df[mask].sort_values('A')
    A   B
7  50  20
1  72   9
2  75   5
5  76  71
3  79  64

CASE 1: Assignment with .loc

No problem here this is expected behaviour as pandas align data on indices.

df.loc[mask] = df[mask].sort_values('A')
>>> df[mask] 

    A   B
1  72   9
2  75   5
3  79  64
5  76  71
7  50  20

CASE 2: Assignment without .loc

The problem is here. In pandas version 1.2.x assignment without using .loc no longer respects index alignment behaviour which was previously respected in version 1.1.x.

df[mask] = df[mask].sort_values('A')
>>> df[mask]

    A   B
1  50  20
2  72   9
3  75   5
5  76  71
7  79  64

Problem description

Lets say we want to sort the values of the slice of the dataframe and assign the sorted values back to the original dataframe in-place.

  • In CASE 1 when using assignment with .loc no effect of sorting the slice is observed in original dataframe which is expected as pandas align data on indices.
  • In CASE 2 when using assignment without .loc the assignment operation no longer respects the alignment of indices which is unexpected behaviour in pandas version 1.2.x

Prior to pandas version 1.2.x lets say in pandas version 1.1.5 the assignment operation even without using the loc respected the index alignment behaviour.

Expected Output

In CASE 2 the expected output should be:

>>> df[mask]
    A   B
1  72   9
2  75   5
3  79  64
5  76  71
7  50  20

As the assignment operation should respect the pandas alignment of indices behaviour.

Output of pd.show_versions()

INSTALLED VERSIONS

commit : 7d32926
python : 3.8.6.final.0
python-bits : 64
OS : Linux
OS-release : 5.8.0-44-generic
Version : #50-Ubuntu SMP Tue Feb 9 06:29:41 UTC 2021
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : en_IN
LOCALE : en_IN.ISO8859-1

pandas : 1.2.2
numpy : 1.18.5
pytz : 2020.1
dateutil : 2.8.1
pip : 20.1.1
setuptools : 44.0.0
Cython : None
pytest : 6.2.1
hypothesis : None
sphinx : 3.4.3
blosc : None
feather : None
xlsxwriter : 1.3.5
lxml.etree : 4.6.2
html5lib : 1.1
pymysql : None
psycopg2 : None
jinja2 : 2.11.2
IPython : 7.18.1
pandas_datareader: 0.9.0
bs4 : 4.9.1
bottleneck : None
fsspec : 0.8.5
fastparquet : 0.5.0
gcsfs : None
matplotlib : 3.3.2
numexpr : None
odfpy : None
openpyxl : 3.0.5
pandas_gbq : None
pyarrow : 2.0.0
pyxlsb : None
s3fs : None
scipy : 1.5.2
sqlalchemy : 1.3.22
tables : None
tabulate : 0.8.7
xarray : 0.16.2
xlrd : 1.2.0
xlwt : None
numba : 0.50.1

@complexsum complexsum added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Feb 20, 2021
@complexsum complexsum changed the title BUG: BUG: Index alignment behaviour Feb 20, 2021
@phofl
Copy link
Member

phofl commented Feb 20, 2021

Context: We are dispatching to iloc under the hood. Iloc stopped aligning in 1.2

@phofl phofl added 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 Feb 20, 2021
@rhshadrach
Copy link
Member

@phofl - It seems to me iloc not aligning is the correct behavior, but this induced a regression in loc not aligning - is that accurate?

@phofl
Copy link
Member

phofl commented Feb 20, 2021

DataFrame.setitem does not align, loc behaves fine, but yes this is a regression

@phofl phofl added the Regression Functionality that used to work in a prior pandas version label Feb 20, 2021
@rhshadrach
Copy link
Member

Ah thanks - makes sense. I got the two cases in the OP mixed up.

@phofl phofl added this to the 1.2.3 milestone Feb 20, 2021
simonjayhawkins added a commit to simonjayhawkins/pandas that referenced this issue Feb 22, 2021
@simonjayhawkins
Copy link
Member

Context: We are dispatching to iloc under the hood. Iloc stopped aligning in 1.2

first bad commit: [3d259e6] Bug in iloc aligned objects (#37728)

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 Regression Functionality that used to work in a prior pandas version
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants