-
-
Notifications
You must be signed in to change notification settings - Fork 18k
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: Decimal year #60391
Comments
Thanks for the request. Can you provide input, a proposed syntax for the operation, and what your expected output would be. |
Sure. Something like Would convert column of datetime64 (e.g., |
@rhshadrach if you don't mind, I would love to work on this issue |
Here is a vectorized version: dates = ["2024-05-30", "2025-05-30"]
df = pd.DataFrame({"date": dates})
df["date"] = pd.to_datetime(df["date"], format="%Y-%m-%d")
year = df["date"].dt.year
days = (pd.to_datetime(year+1, format='%Y') - pd.to_datetime(year, format='%Y')).dt.days
result = year + (df["date"] - pd.to_datetime(year, format='%Y')) / (days * pd.to_timedelta(1, unit="D"))
print(result)
# 0 2024.409836
# 1 2025.408219
# Name: date, dtype: float64 @AryanK1511 - I think this needs discussion from the core team. It seems straightforward to calculate this from the existing API, I'm not sure it warrants inclusion. |
Thanks @rhshadrach. Nice simple solution. My only suggestion would be to include timestamps as well.
I agree this is a straightforward calculation. The request is mostly one of convenience and centralization, so each user doesn't have to implement their own function or include those 3 lines whenever they want to do this. |
I do not think using such a criteria is sustainable for the pandas API. Rather, it should be the goal of pandas to provide an API with the fundamental tools so that users can combine various operations in a short and straight forward manner to accomplish their needs. I believe that is already being done here. |
Feature Type
Adding new functionality to pandas
Changing existing functionality in pandas
Removing existing functionality in pandas
Problem Description
I wish I could use pandas to quickly convert datetime/Timestamp objects to "decimal year" floating point numbers for subsequent visualization and analysis.
A number of plotting packages (e.g., GeoPandas, matplotlib) encounter issues when casting datetime/Timestamp objects to float. For example, I often encounter errors when trying to create a choropleth map to visualize a GeoDataFrame column containing datetime objects. Decimal years also simplify the legend/colorbar labels.
Feature Description
This is a simple function to accomplish this. It's not perfect, but does the job. Would need to re-implement as a Timestamp and/or dt accessor property (
dt.decyear
). Should be relatively simple, I think.Alternative Solutions
Define and apply a custom function:
df['dt_col_decyear'] = df['dt_col'].apply(toYearFraction)
Additional Context
When attempting to plot column containing datetime values...
gdf.plot(column='dt_col', legend=True)
The text was updated successfully, but these errors were encountered: