Skip to content
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

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

Open
1 of 3 tasks
lucasjamar opened this issue Jan 4, 2025 · 1 comment
Open
1 of 3 tasks
Labels
Closing Candidate May be closeable, needs more eyeballs Enhancement IO Data IO issues that don't fit into a more specific label

Comments

@lucasjamar
Copy link

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

@lucasjamar lucasjamar added Enhancement Needs Triage Issue that has not been reviewed by a pandas team member labels Jan 4, 2025
@rhshadrach
Copy link
Member

Thanks for the request. I'm negative here - this can be done with two lines of code:

columns = df.select_dtypes("datetime").columns
df[columns] = df[columns].map(lambda x: x.isoformat())

Adding it to the API would increase the maintenance burden on pandas while not providing users with any new functionality that wasn't readily achievable.

@rhshadrach rhshadrach added IO Data IO issues that don't fit into a more specific label Closing Candidate May be closeable, needs more eyeballs and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Jan 4, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Closing Candidate May be closeable, needs more eyeballs Enhancement IO Data IO issues that don't fit into a more specific label
Projects
None yet
Development

No branches or pull requests

2 participants