Skip to content

The DataFrame.to_dict method is inconsistent when there are no columns and orient='records'. #26572

@jack-pace

Description

@jack-pace

Code Sample

import pandas as pd

df = pd.DataFrame([{}, {}, {}])
df.to_dict(orient='records')
# Output: []

Problem description

The DataFrame.to_dict method is inconsistent when there are no columns and orient='records'.

I think that this is a recent regression, since it was not a problem on pandas 0.22.0.
Searching through previous issues I see it might be related to a change in version 0.24.0.

I expect pandas to return a list of three empty dictionaries here: [{}, {}, {}].
That way, for example, doing a from_dict followed by a to_dict will round trip and vice versa. E.g.

pd.DataFrame.from_dict([{}, {}, {}], orient='records').to_dict(orient='records') == [{}, {}, {}]
# Output: False (I expected True)

Also, things like the length will be consistent:

df = pd.DataFrame([{}, {}, {}])
len(df)
# Output: 3
len(df.to_dict(orient='records'))
# Output: 0

Expected Output

See "Problem Description"

Output of pd.show_versions()

INSTALLED VERSIONS

commit: None
python: 3.7.3.final.0
python-bits: 64
OS: Linux
OS-release: 4.8.0-53-generic
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: None
LANG: en_GB.UTF-8
LOCALE: en_GB.UTF-8

pandas: 0.24.2
pytest: 3.3.2
pip: 19.1.1
setuptools: 41.0.1
Cython: None
numpy: 1.16.4
scipy: 1.3.0
pyarrow: None
xarray: None
IPython: 7.5.0
sphinx: 1.4.8
patsy: 0.5.1
dateutil: 2.8.0
pytz: 2019.1
blosc: None
bottleneck: None
tables: None
numexpr: None
feather: None
matplotlib: None
openpyxl: None
xlrd: None
xlwt: None
xlsxwriter: None
lxml.etree: None
bs4: None
html5lib: None
sqlalchemy: 1.2.9
pymysql: None
psycopg2: 2.7.5 (dt dec pq3 ext lo64)
jinja2: 2.10.1
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: None
gcsfs: None

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions