I'm not sure if this is design intent and my expectations are wrong but it seems to me that the type conversion of numpy types to python types from Series.to_dict() is inconsistent. By comparison, a similar experiment with DataFrame.to_dict() results does what I expect.
venv running: Python 3.7.5, pandas==0.25.3
Here is the example code to illustrate:
import pandas as pd
if __name__ == "__main__":
data = {'A': 1, 'B': None}
df = pd.DataFrame([data])
print("original DataFrame:")
print(df)
print("Series.to_dict():")
res = df.loc[0,:].to_dict()
for i in res.items():
print(type(i[1]))
print("DataFrame.to_dict('records'):")
res = df.to_dict('records')[0]
for i in res.items():
print(type(i[1]))
This code produces the following output: original DataFrame:
A B
0 1 None
Series.to_dict():
<class 'numpy.int64'>
<class 'NoneType'>
DataFrame.to_dict('records'):
<class 'int'>
<class 'NoneType'>
Notice that the value for column A is coming out as a numpy.int64 in the case of Series.to_dict() whereas exporting the DataFrame.to_dict('records') and taking the first row from the list of records has the column A value coming out as a python int.
Interestingly, if I remove the None value from column B and replace it with another integer value, I get different behavior in the type conversion. As follows:
import pandas as pd
if __name__ == "__main__":
data = {'A': 1, 'B': 2}
df = pd.DataFrame([data])
print("original DataFrame:")
print(df)
print("Series.to_dict():")
res = df.loc[0,:].to_dict()
for i in res.items():
print(type(i[1]))
print("DataFrame.to_dict('records'):")
res = df.to_dict('records')[0]
for i in res.items():
print(type(i[1]))
The above code yields the following output:
A B
0 1 2
Series.to_dict():
<class 'int'>
<class 'int'>
DataFrame.to_dict('records'):
<class 'int'>
<class 'int'>
In this case, both the Series.to_dict() and the DataFrame.to_dict() behave the same way.
Please clarify this for me. Thanks!