-
-
Notifications
You must be signed in to change notification settings - Fork 18.2k
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
BUG: DataFrame.to_dict() converts Nullable Int types to numpy.int #34665
Labels
Comments
6 tasks
Minimal reproducer: In [3]: import pandas as pd
...:
...: df = pd.DataFrame({'A': [1, None, 2, 3, None]})
...: df['A'] = df['A'].astype('Int64')
...: dicts = df.to_dict(orient="records")
...: pd.DataFrame(
...: [[type(v) for k, v in row.items()] for row in dicts],
...: columns=dicts[0].keys())
Out[3]:
A
0 <class 'numpy.int64'>
1 <class 'pandas._libs.missing.NAType'>
2 <class 'numpy.int64'>
3 <class 'numpy.int64'>
4 <class 'pandas._libs.missing.NAType'> |
Even smaller: In [2]: import pandas as pd
...:
...: df = pd.DataFrame({'A': [1, None]})
...: df['A'] = df['A'].astype('Int64')
...: records_as_dicts = df.to_dict(orient="records")
...: pd.DataFrame([[type(v) for v in row.values()] for row in records_as_dicts], columns=records_as_dicts[0].keys())
Out[2]:
A
0 <class 'numpy.int64'>
1 <class 'pandas._libs.missing.NAType'> |
I am using Could not find a better approach so I used json.loads(dataframe.iloc[0, :].to_json()) |
3 tasks
5 tasks
5 tasks
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Problem description
DataFrame.to_dict()
method do not cast Nullable Int types (Int*Dtype
) into Pythonint
type. Instead, it unwrapping intonumpy.int*
types.Possibly related to: #27616, #25969, #21256
Expected Output
Native Python
int
type.Reproduction
Make some data:
value
have to be a nullableint
:Looks great. But convert a dataframe to
dict
:Output of
pd.show_versions()
The text was updated successfully, but these errors were encountered: