-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
BUG: inconsistent index order when creating DataFrame from dictionary with only 1 column #49233
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
Comments
Temporary workaround:
|
This sorting of index when one index is present is happening in pandas/pandas/core/indexes/api.py Lines 205 to 227 in 295bc76
|
take |
… with only 1 column pandas-dev#49233 Removed sorted function for when the dataframe constructor is called with a python dictionary so the ordering does not surprise users. A test to verify this was also added.
… with only 1 column pandas-dev#49233 add style fixes for previous commit
… with only 1 column pandas-dev#49233 added fix to still complete sort when the sort param is true
I try to fix it but it breaks the existing test. It seems the sorted behavior is expected according to the test below: pandas/pandas/tests/frame/methods/test_rename.py Lines 50 to 60 in c426dc0
|
Seems this issue has been solved in #55696. |
Closed by #55696 |
Pandas version checks
I have checked that this issue has not already been reported.
I have confirmed this bug exists on the latest version of pandas.
I have confirmed this bug exists on the main branch of pandas.
Reproducible Example
Issue Description
Not sure if related to this: #24859 (comment)
When creating a DataFrame from a nested dictionary with only 1 column, the order of index keys is not kept. i.e.
pd.DataFrame({"a":{"c":2, "d":3, "b":1}})
should have index[c, d, b]
, not[b, c, d]
.However, the order is kept when there is more than 1 column.
Expected Behavior
Installed Versions
INSTALLED VERSIONS
commit : 87cfe4e
python : 3.10.6.final.0
python-bits : 64
OS : Linux
OS-release : 3.10.0-1160.11.1.el7.x86_64
Version : #1 SMP Fri Dec 18 16:34:56 UTC 2020
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8
pandas : 1.5.0
numpy : 1.23.3
pytz : 2022.2.1
dateutil : 2.8.2
setuptools : 65.4.0
pip : 22.2.2
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : 4.9.1
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 3.1.2
IPython : 8.5.0
pandas_datareader: None
bs4 : 4.11.1
bottleneck : None
brotli :
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : 3.6.0
numba : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : 1.9.1
snappy : None
sqlalchemy : 1.4.41
tables : None
tabulate : 0.8.10
xarray : 2022.9.0
xlrd : None
xlwt : None
zstandard : None
tzdata : None
The text was updated successfully, but these errors were encountered: