Open
Description
Feature Type
-
Adding new functionality to pandas
-
Changing existing functionality in pandas
-
Removing existing functionality in pandas
Problem Description
df.to_dict
is often (maybe even mostly) to post data via REST API.
My usual workflow for posting data looks like this:
df = pd.DataFrame({"date": pd.to_daterange("2021-01-01", "2024-01-01")})
df["values"] = 1
df["date"] = df["date"].map(lambda x: x.isoformat())
df = df.to_dict("records")
df["some_key"] = "some_value"
response= requests.post(url, data=df)
Ideally, the datetime to string could happen inside of to_dict
via an optional date_format
and date_unit
parameter similar to df.to_json
.
However, df.to_json
is not a suitable alternative because often I need to add an additional key as seen above.
If this seems like an appropriate new feature, I will dig into how to implement this.
Feature Description
Add 2 new parameters to to_dict
:
date_format
. Possible values: None, Epoch, isoformat. Default: None. If None, do nothing.date_unit
. One of ‘s’, ‘ms’, ‘us’, ‘ns’ . Default ms
Alternative Solutions
None really
Additional Context
Code for to_dict:
pandas/pandas/core/methods/to_dict.py
Line 56 in 1be2637
to_json: https://github.com/pandas-dev/pandas/blob/v2.2.3/pandas/core/generic.py#L2428-L2717