-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
cross section coercion with output iterating #12859
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
Nothing to do with the index, just the fact that you have any float dtypes in the data
If you look at the code, we use We probably don't need to use |
Thanks for your response. It there a possible workaround that I can use in the meantime? |
Something like In [28]: [x._asdict() for x in df.itertuples()]
Out[28]:
[OrderedDict([('Index', 0), ('a', 1.0), ('b', 90)]),
OrderedDict([('Index', 1), ('a', 2.0), ('b', 80)]),
OrderedDict([('Index', 2), ('a', 3.0), ('b', 40)]),
OrderedDict([('Index', 3), ('a', 4.0), ('b', 60)]),
OrderedDict([('Index', 4), ('a', 5.0), ('b', 30)])] That's an OrderedDict using |
Thanks :) |
Though one could argue that this result is correct as we don't support mixed types in int-float when doing a cross-section, IOW:
this is somewhat related to #12532, meaning that we should be iterating directly over (which already does the proper coercion), rather that doing a specific coercion in |
giong to mark this an an API issue that needs discussion. This would actually be a fairly large change to correctly change this (though to be honest I think the current behavior is fine). |
Note for future seekers - I'm trying to combine multiple pandas objects into one nested json structure. Since to_json doesn't work in this case (manipulating json strings is hard), you might try to do to_dict(orient="records"), and combine the results of the to_dict()s into a bigger object, and do So then you might try doing @TomAugspurger's solution but you might find that for some reason it won't convert numpy types to python types, like to_dict() does, which makes json.dumps() fail. My workaround solution is to do to_json() which gives you a correct json string with correct types, then do json.loads() on that to get python objects corresponding to that string, which you then put together whichever way you want (e.g. def to_records(df):
"""Replacement for pandas' to_dict(orient="records") which has trouble with
upcasting ints to floats in the case of other floats being there.
https://github.com/pandas-dev/pandas/issues/12859
"""
import json
return json.loads(df.to_json(orient="records")) |
👍 There is an issue somewhere |
Really good workaround is found here: https://stackoverflow.com/a/31375241/1838257
Using the workaround:
Could this be implemented in a flag of |
The original example has the correct behavior of the index values remaining as integer. Could use a test
|
I'm am trying to call the to_dict function on the following DataFrame:
However my problem occurs if I perform a float operation on the dataframe, which mutates the index into a float:
Can anyone explain the above behaviour or recommend a workaround, or verify whether or not this could be a pandas bug? None of the other outtypes in the to_dict method mutates the index as shown above.
I've replicated this on both pandas 0.14 and 0.18 (latest)
Many thanks!
link to stackoverflow: http://stackoverflow.com/questions/36548151/pandas-to-dict-changes-index-type-with-outtype-records
The text was updated successfully, but these errors were encountered: