Skip to content

order not preserved when constructing DataFrame from a list of OrderedDicts #13304

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

Closed
ilrico opened this issue May 27, 2016 · 4 comments
Closed
Labels
Enhancement Reshaping Concat, Merge/Join, Stack/Unstack, Explode
Milestone

Comments

@ilrico
Copy link

ilrico commented May 27, 2016

issue is explained here:
http://stackoverflow.com/questions/37484738/pandas-dataframe-construction-from-a-list-of-ordereddict-preserving-columns-ord?noredirect=1#comment62465925_37484738

workaround with constructor option "columns" works but not elegant.

@jreback
Copy link
Contributor

jreback commented May 27, 2016

pls post a copy-pastable reproducible example at the top of the PR.

yes this could be an enhancement. pull-requests are welcome.

@jreback jreback added Enhancement Reshaping Concat, Merge/Join, Stack/Unstack, Explode Difficulty Novice labels May 27, 2016
@jreback jreback added this to the Next Major Release milestone May 27, 2016
@gfyoung
Copy link
Member

gfyoung commented May 27, 2016

Here's one:

>>> from pandas.compat import OrderedDict
>>> from pandas import DataFrame
>>>
>>> data = OrderedDict()
>>> data['c'] = 2
>>> data['b'] = 1
>>>
>>> data
OrderedDict([('c', 2), ('b', 1)])
>>>
>>> DataFrame([data])
   b  c
0  1  2

@gfyoung
Copy link
Member

gfyoung commented May 27, 2016

I should point out though that if you pass in the OrderedDict with arrays as the values, the order is in fact preserved!

>>> from pandas.compat import OrderedDict
>>> from pandas import DataFrame
>>>
>>> data = OrderedDict()
>>> data['c'] = [2]
>>> data['b'] = [1]
>>>
>>> data
OrderedDict([('c', [2]), ('b', [1])])
>>>
>>> DataFrame(data)
   c  b
0  2  1

@gfyoung
Copy link
Member

gfyoung commented May 27, 2016

The "bug" traces back to the initialization of arrays from the list of dictionaries. The initialization sorts the keys at the end, so we just need a boolean to tell it not to sort when we have OrderedDict. Can take this one home. A welcomed change of scenery from the land of read_csv! 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement Reshaping Concat, Merge/Join, Stack/Unstack, Explode
Projects
None yet
Development

No branches or pull requests

3 participants