-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
DataFrame.to_dict not using python types #19381
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
Please make a reproducible example - in some cases at least we are returning python types. (agree we should in all) In [210]: df = pd.DataFrame({'a': [1,2,3], 'b': [4,5,6]})
In [211]: df.to_dict()
Out[211]: {'a': {0: 1, 1: 2, 2: 3}, 'b': {0: 4, 1: 5, 2: 6}}
In [213]: type(df.to_dict()['a'][0])
Out[213]: int |
I am loading data from a database. I then call import pandas as pd
import json
import datetime
data = [
datetime.date(1987, 2, 12),
datetime.date(1987, 2, 12),
datetime.date(1987, 2, 12),
None,
None,
datetime.date(1989, 6, 15),
datetime.date(1989, 6, 15),
datetime.date(1989, 6, 15),
datetime.date(1989, 6, 15),
datetime.date(1989, 6, 15),
datetime.date(1989, 6, 15),
datetime.date(1989, 6, 15),
datetime.date(1989, 6, 15),
datetime.date(1989, 6, 15),
datetime.date(1989, 6, 15)
]
df = pd.DataFrame(columns=['foo'])
df['foo'] = data
ds = df['foo'].describe()
d = ds.to_dict()
j = json.dumps(d)
print(j) |
show your versions this is fixed in 0.21 iirc |
Thanks for the example - this is actually a symptom / dupe of #15385 - some of the aggregations used in |
I have a dataframe that I need to convert to a
dict
and add it to anotherdict
which is converted to json in the end. Unfortunately since.to_dict()
does not use standard python types I get the following:TypeError: Object of type 'int32' is not JSON serializable
My code is something like the following:
Using
dumps
results in the above error. This is related to #13258 where an 'easy way' was mentioned but I'm not sure what it is.Would appreciate any help
The text was updated successfully, but these errors were encountered: