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: astype() on an integer DataFrame changes the order of data #42396

Closed
2 of 3 tasks
wjsi opened this issue Jul 6, 2021 · 4 comments · Fixed by #42475
Closed
2 of 3 tasks

BUG: astype() on an integer DataFrame changes the order of data #42396

wjsi opened this issue Jul 6, 2021 · 4 comments · Fixed by #42475
Labels
Dtype Conversions Unexpected or buggy dtype conversions Regression Functionality that used to work in a prior pandas version
Milestone

Comments

@wjsi
Copy link
Contributor

wjsi commented Jul 6, 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, a copy-pastable example

import numpy as np
import pandas as pd
npa = np.random.RandomState(0).randint(1000, size=(20, 8))
df = pd.DataFrame(npa, columns=[f'c{i}' for i in range(8)])
print(df.iloc[:6, :3])
#     c0   c1   c2
# 0  684  559  629
# 1    9  723  277
# 2  600  396  314
# 3  600  849  677
# 4  115  976  755
# 5   99  984  177
print(df.iloc[:6, :3].astype('int32'))
#     c0   c1   c2
# 0  684  600  115
# 1  559  396  976
# 2  629  314  755
# 3    9  600   99
# 4  723  849  984
# 5  277  677  177

Problem description

The order of output result is changed unexpectedly after astype is called. It may caused by data continuity as df.iloc[:6, :3].copy().astype('int32') works as expected.

Expected Output

print(df.iloc[:6, :3].astype('int32'))
#     c0   c1   c2
# 0  684  559  629
# 1    9  723  277
# 2  600  396  314
# 3  600  849  677
# 4  115  976  755
# 5   99  984  177

Output of pd.show_versions()

INSTALLED VERSIONS

commit : f00ed8f
python : 3.8.5.final.0
python-bits : 64
OS : Darwin
OS-release : 19.6.0
Version : Darwin Kernel Version 19.6.0: Thu May 6 00:48:39 PDT 2021; root:xnu-6153.141.33~1/RELEASE_X86_64
machine : x86_64
processor : i386
byteorder : little
LC_ALL : None
LANG : None
LOCALE : zh_CN.UTF-8
pandas : 1.3.0
numpy : 1.18.5
pytz : 2021.1
dateutil : 2.8.1
pip : 20.2.2
setuptools : 49.6.0.post20200814
Cython : 0.29.23
pytest : 6.2.2
hypothesis : 6.3.0
sphinx : 3.2.1
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 2.11.2
IPython : 7.17.0
pandas_datareader: None
bs4 : None
bottleneck : None
fsspec : None
fastparquet : 0.4.2
gcsfs : None
matplotlib : 3.2.2
numexpr : 2.7.1
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : 4.0.0
pyxlsb : None
s3fs : None
scipy : 1.6.0
sqlalchemy : 1.3.18
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
numba : 0.53.0

@wjsi wjsi added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Jul 6, 2021
@rhshadrach
Copy link
Member

Thanks for the report - seeing this on master but not 1.2.x.

@rhshadrach rhshadrach added Regression Functionality that used to work in a prior pandas version Dtype Conversions Unexpected or buggy dtype conversions and removed Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Jul 7, 2021
@rhshadrach rhshadrach added this to the 1.3.1 milestone Jul 7, 2021
@debnathshoham
Copy link
Member

take

@debnathshoham
Copy link
Member

This appears to be a 'deeper' issue.

So, the dataframe is converted to a BlockManager. And then to ndarray (with both c_contiguous and f_contiguous flags False). And then finally during reshape, it is reshaped with order = 'C'. If we use order = 'F', the reshape works as expected, but breaks test pandas/tests/window/test_online.py::TestEWM::test_online_vs_non_online_mean for dataframes.

@mzeitlin11
Copy link
Member

mzeitlin11 commented Jul 9, 2021

A bisect indicates the cause as #38507, xref #38592 also involved that pr

@debnathshoham debnathshoham removed their assignment Jul 10, 2021
simonjayhawkins added a commit to simonjayhawkins/pandas that referenced this issue Jul 10, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Dtype Conversions Unexpected or buggy dtype conversions Regression Functionality that used to work in a prior pandas version
Projects
None yet
4 participants