Skip to content

BUG: Order of dict items in DataFrame.from_dict changes the result dataframe #50461

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

Open
3 tasks done
Itayazolay opened this issue Dec 28, 2022 · 2 comments
Open
3 tasks done
Labels
Bug Constructors Series/DataFrame/Index/pd.array Constructors DataFrame DataFrame data structure

Comments

@Itayazolay
Copy link
Contributor

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

df1 = pd.DataFrame.from_dict({'a': ['A1', 'A2'], 'b': 'B'}, orient='index')
df2 = pd.DataFrame.from_dict({'b': 'B', 'a': ['A1', 'A2']}, orient='index')

Issue Description

df1 has columns [0, 1] with the array exploded
df2 has only one column(0) with the array as value

Expected Behavior

Expected the order of the items of the dictionary to not impact on the result of the data frame.
Since it's possible to pass a combination of scalars and lists, I would expect it to work the same regardless the dict items order.

Installed Versions

INSTALLED VERSIONS

commit : 8dab54d
python : 3.11.0.final.0
python-bits : 64
OS : Linux
OS-release : 5.15.0-56-generic
Version : #62-Ubuntu SMP Tue Nov 22 19:54:14 UTC 2022
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 1.5.2
numpy : 1.24.1
pytz : 2022.7
dateutil : 2.8.2
setuptools : 65.6.3
pip : 22.3.1
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : None
IPython : None
pandas_datareader: None
bs4 : None
bottleneck : None
brotli : None
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : None
numba : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : None
snappy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
zstandard : None
tzdata : None

@Itayazolay Itayazolay added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Dec 28, 2022
@dicristina
Copy link
Contributor

Make the values of your dictionaries array-like:

df1 = pd.DataFrame.from_dict({'a': ['A1', 'A2'], 'b': ['B']}, orient='index')
df2 = pd.DataFrame.from_dict({'b': ['B'], 'a': ['A1', 'A2']}, orient='index')

The documentation states that pd.DataFrame.from_dict is to be used to "Construct DataFrame from dict of array-like or dicts". A string is not considered array-like in this case.

@topper-123
Copy link
Contributor

topper-123 commented May 6, 2023

I agree those two operation should give the same rows, so this is a bug.

More generally, we normally treat listlikes in DataFrame/series constructors and I think we should to that here too, though that may be an enhancement, so goes beyond this concrete issue. Also I'd expect a scalar to explode rowwise , so I'd actually expect df1 to be:

    0   1
a  A1  A2
b   B   B

But that's also en enhancement,maybe.

@topper-123 topper-123 added DataFrame DataFrame data structure Constructors Series/DataFrame/Index/pd.array Constructors and removed Needs Triage Issue that has not been reviewed by a pandas team member labels May 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Constructors Series/DataFrame/Index/pd.array Constructors DataFrame DataFrame data structure
Projects
None yet
Development

No branches or pull requests

3 participants