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

DataFrame constructor ignores key order when data is an OrderedDict and orient is 'columns' #10514

Closed
alan-wong opened this issue Jul 6, 2015 · 1 comment
Labels
Bug Reshaping Concat, Merge/Join, Stack/Unstack, Explode

Comments

@alan-wong
Copy link

Whilst answering this question: http://stackoverflow.com/questions/31242021/python-3-4-pandas-dataframe-not-responding-to-ordered-dictionary

I had look at this and found the error is in index.py line 5746 on pandas 0.16.2:

def _union_indexes(indexes):
    if len(indexes) == 0:
        raise AssertionError('Must have at least 1 Index to union')
    if len(indexes) == 1:
        result = indexes[0]
        if isinstance(result, list):
            result = Index(sorted(result)) #<---- here
        return result

minimal example:

In [38]:
import pandas as pd
from collections import OrderedDict
d = OrderedDict([('XXX', OrderedDict([('B', 1), ('A', 2)]))])
pd.DataFrame(d)

Out[38]:
   XXX
A    2
B    1

The same thing occurs if you do:

pd.DataFrame.from_dict(d)

Note that passing 'orient='index'' preserves the order:

In [40]:
pd.DataFrame.from_dict(d, orient='index')

Out[40]:
     B  A
XXX  1  2
@jreback
Copy link
Contributor

jreback commented Jul 6, 2015

dupe of #8425

pull-requests welcome to fix of course!

@jreback jreback closed this as completed Jul 6, 2015
@jreback jreback added Reshaping Concat, Merge/Join, Stack/Unstack, Explode Bug labels Jul 6, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Reshaping Concat, Merge/Join, Stack/Unstack, Explode
Projects
None yet
Development

No branches or pull requests

2 participants