0

I am finding that the output of the pandas dataframe classes to_dict method is different for columns of the same dtype in different dataframes. I have one dataframe where all columns are of type int64. to_dict on this dataframe will print values of type int64. In another dataframe where there is a column of type int64 and another column of a different type, the int64 column values, after to_dict, are represented as type int. Is there an explanation why this method should be producing a representation of a value that is dependent on types of other values w/in the same dataframe?

import pandas as pd

df1 = pd.DataFrame([{'a': 1, 'b': 'str'}])
df2 = pd.DataFrame([{'a': 1, 'b': 2}])

l1 = df1.to_dict(orient='records')
l2 = df2.to_dict(orient='records')

for i in l1:
    for k in i:
        print(type(i[k]))


for i in l2:
    for k in i:
        print(type(i[k]))

output:

<class 'int'>
<class 'str'>
<class 'numpy.int64'>
<class 'numpy.int64'>

Ideally, I would like some consistency. Whatever that may be. Thanks in advance for any insight!

scagnetti
  • 1,435
  • 3
  • 20
  • 38
  • Possible duplicate of [Converting numpy dtypes to native python types](https://stackoverflow.com/questions/9452775/converting-numpy-dtypes-to-native-python-types) – Jonathan Gagne Sep 28 '18 at 18:12
  • 1
    Seems to be a duplicate of that https://stackoverflow.com/questions/9452775/converting-numpy-dtypes-to-native-python-types – Jonathan Gagne Sep 28 '18 at 18:13
  • 1
    A workaround would be to cast the DataFrame as object first: `df2.astype(object).to_dict(orient='records')` – ayhan Sep 28 '18 at 18:14
  • Thank you Jonathan Gagne & ayhan! I'll definitely apply one of these solutions. I'm mainly curious why to_dict behaves differently in these two separate contexts. – scagnetti Sep 28 '18 at 18:36
  • 1
    It seems like a bug to me. Raise an issue on github maybe? – ayhan Sep 28 '18 at 18:45
  • 1
    @ayhan, as always, looks like i'm a bit late: https://github.com/pandas-dev/pandas/issues/22620 – scagnetti Sep 28 '18 at 18:52

0 Answers0