Skip to content

ENH: Add date_format and date_unit to to_dict similar to what exists in to_json #60656

Open
@lucasjamar

Description

@lucasjamar

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:

  1. date_format. Possible values: None, Epoch, isoformat. Default: None. If None, do nothing.
  2. date_unit. One of ‘s’, ‘ms’, ‘us’, ‘ns’ . Default ms

Alternative Solutions

None really

Additional Context

Code for to_dict:


to_json: https://github.com/pandas-dev/pandas/blob/v2.2.3/pandas/core/generic.py#L2428-L2717

Metadata

Metadata

Assignees

No one assigned

    Labels

    Closing CandidateMay be closeable, needs more eyeballsEnhancementIO DataIO issues that don't fit into a more specific label

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions