-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
Ordering should be kept when OrderedDict records are passed #10056
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
Comments
Can you be a bit more specific? Passed to what? (DataFrame?) |
Sorry, I pressed submit by mistake. Hope it's clear now. |
It does work, but you are passing a list of dicts, which is quite different.
I am +0 about supporting a nested version of this. I don't see the utility. |
My use case is: def results_table(results):
records = [OrderedDict([
('Observable' , result.obs),
('PDF' , result.pdf),
('Collaboration' , result.pdf.collaboration),
('alpha_sMref' , result.pdf.AlphaS_MZ),
('PDF_OrderQCD' , result.pdf.oqcd_str),
('NumFlavors' , result.pdf.NumFlavors),
('CV' , result.central_value),
('Up68' , result.error68['max']),
('Down68' , result.error68['min']),
('Remarks' , [], )
]) for result in results]
return pd.DataFrame(records) That is, I have a list of objects and I would like to turn them into a DataFrame to be able to display and aggregate them. Doing a list comprehension for each column doesn't look very nice. |
This seems similar to passing namedtuples. Where I think it's clearer to be more explicit:
|
One workaround: data=((1,2),(3,4))
records = [pd.Series(OrderedDict([('a',d[0]),('b',d[1])])) for d in data]
records = pd.DataFrame(records) |
Since ordering is now guaranteed on Expected: In[2]: DataFrame([{'First': 1, 'Second': 2, 'Third': 3, 'Fourth': 4}])
Out[2]:
First Second Third Fourth
0 1 2 3 4 Actual (pandas 0.24.1 on Python 3.6.7) (sometimes - the output ordering is inconsistent): In[2]: DataFrame([{'First': 1, 'Second': 2, 'Third': 3, 'Fourth': 4}])
Out[2]:
First Fourth Second Third
0 1 4 2 3 |
I was never sure if ordering was guaranteed with literals. It is if you use dict(a=a, b=b) or insert line by line. |
The thread I just linked to is entitled "Guarantee ordered dict literals in v3.7?" so I would say yes. A quick test in 3.6 vs 2.7 seemed to corroborate this too. |
to be clear, the quick test I did was based on list({1:2, 3:4, 5:6, 7:8, 'nine': 10, 'eleven': 12}.items())
# in Python 3.6 but not 2.7, always produces
[(1, 2), (3, 4), (5, 6), (7, 8), ('nine', 10), ('eleven', 12)] Hard to pin down these issues though, as often it'll look ordered for a bunch of examples and then you'll change something and the order will change. Is this related to #22708 ? |
When passing a list OrderedDict objects to
pd.DataFrame
, the ordering of the columns is not kept:The text was updated successfully, but these errors were encountered: