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

Add argument to df.to_dicts() and df.to_dict() - maintain_column_order: bool #16414

Closed
DeflateAwning opened this issue May 22, 2024 · 3 comments
Labels
enhancement New feature or an improvement of an existing feature

Comments

@DeflateAwning
Copy link
Contributor

Description

It appears that df.to_dict() and to_dicts don't maintain the column order of the keys in the dictionary/dictionaries. This is presumably an optimization.

It would be helpful if you could enforce maintaining the order from the dataframe with an argument.

@DeflateAwning DeflateAwning added the enhancement New feature or an improvement of an existing feature label May 22, 2024
@cmdlineluser
Copy link
Contributor

Do you happen to have an example?

Anything I've tried so far results in a dict with the same order.

@alexander-beedie
Copy link
Collaborator

alexander-beedie commented May 22, 2024

Likewise; so far I haven't been able to reproduce - and looking at the code it does look like it should always return in-order keys. Definitely need a reproducible example to see if there's an edge-case somehow/somewhere (the keys appear to be inserted in frame-order and Python has had a hard guarantee since 3.6/3.7 that insertion order is maintained) 🤔

@DeflateAwning
Copy link
Contributor Author

Darn, thanks for checking! Sorry to waste your time.

The actual issue was in the way Pytest compares dicts by taking them out-of-order: https://stackoverflow.com/questions/70023936/pytest-dict-equality-preserving-order-and-nice-diff-output-python-3-7

That's totally my bad; sorry for not vetting the issue better before opening it here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or an improvement of an existing feature
Projects
None yet
Development

No branches or pull requests

3 participants