Skip to content

from_dict(..., orient='index') row order preservation inconsistent #24859

Closed
@tdamsma

Description

@tdamsma

Code Sample

data = {'B' : [1], 'A' : [2], 'C' :[3]}
print(pd.DataFrame.from_dict(data, orient='index'))

#    0
# B  1
# A  2
# C  3

data = {'B' : dict(col1=1), 'A' : dict(col1=2), 'C' :dict(col1=3)}
print(pd.DataFrame.from_dict(data, orient='index'))

#    col1
# A     2
# B     1
# C     3

Problem description

If dictionaries are passed for the column values in a call to pd.DataFrame.from_dict(data, orient='index'), then the df index is sorted (Not Expected). If the column values are lists, then the index is not sorted (Expected)

Expected Output

data = {'B' : dict(col1=1), 'A' : dict(col1=2), 'C' :dict(col1=3)}
print(pd.DataFrame.from_dict(data, orient='index'))

#    col1
# B     1
# A     2
# C     3

Output of pd.show_versions()

INSTALLED VERSIONS

commit: None
python: 3.7.0.final.0
python-bits: 64
OS: Darwin
OS-release: 18.2.0
machine: x86_64
processor: i386
byteorder: little
LC_ALL: en_US.UTF-8
LANG: en_US.UTF-8
LOCALE: en_US.UTF-8

pandas: 0.23.4
pytest: 4.0.1
pip: 18.1
setuptools: 40.6.2
Cython: 0.29.1
numpy: 1.15.4
scipy: None
pyarrow: None
xarray: None
IPython: 7.2.0
sphinx: 1.8.2
patsy: None
dateutil: 2.7.5
pytz: 2018.7
blosc: None
bottleneck: None
tables: None
numexpr: None
feather: None
matplotlib: 3.0.2
openpyxl: None
xlrd: 1.1.0
xlwt: None
xlsxwriter: 1.1.2
lxml: None
bs4: 4.6.3
html5lib: None
sqlalchemy: 1.2.14
pymysql: None
psycopg2: 2.7.6.1 (dt dec pq3 ext lo64)
jinja2: 2.10
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: None

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugDataFrameDataFrame data structure

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions