-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
using to_dict with the 'records' orient produces different results from the default one #29824
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
You can use this function if you really need that: def to_dict(df, orient="dict"):
if orient=="records":
columns = df.columns.tolist()
rows = (
dict(zip(columns, row)) for row in df.itertuples(
index=False,
name=None
)
)
return [x for x in rows]
else:
return df.to_dict(orient) You can use it like this: import pandas as pd
df = pd.DataFrame({"d": pd.date_range("2018-01-01", freq="12h", periods=2)})
for col in df.select_dtypes(["datetime"]):
df[col] = pd.Series(df[col].dt.to_pydatetime(), dtype = "object")
print(to_dict(df, "records")) This will result as you want it:
|
@jbrockmendel can you take a look at this since I think you have thought about datetime issues? I think this is a bug, or API consistency - not sure which label to use |
Labelled as API-Consistency. cc @WillAyd would it make sense to have to_dict re-use the json code? |
maybe add an argument for native types? @jbrockmendel the |
There might be a creative way to do this, but JSON is going to have a different set of requirements to serialize to than native Python types |
as of #37648 xref #39389 (comment) people express a preference for returning standard library |
@arw2019 Do you happen to Know about |
would you mind opening a separate issue for that? It's related but will be a separate fix |
@arw2019 not sure what the issue should be about? a feature request ? |
Just for information I have similar issue with I have checked types and found this conversion: import pandas as pd
import datetime as dt
df__ = pd.DataFrame([[dt.datetime.utcnow()]], columns=['date'])
next(df__.iterrows())[1]
# OK
# date 2022-02-15 05:05:20.881479
# Name: 0, dtype: datetime64[ns]
next(df__.iterrows())[1]['date']
# NOK
# Timestamp('2022-02-15 09:04:14.469187') I have tried to save |
consider the following
df
using the normal
to_dict
returns the wanted results with the native python typeshowever doing it with a different orient, produces different results
as a side note, it would be nice to have the ability to convert a dataframe or a series into a native types structure (no unlike
to_json
)versions:
pandas : 0.25.2
numpy : 1.17.3
The text was updated successfully, but these errors were encountered: