Description
Feature Type
-
Adding new functionality to pandas
-
Changing existing functionality in pandas
-
Removing existing functionality in pandas
Problem Description
I am working with a widely used index for many industries which has week of year as index. Here is an example:
"2020-01"
"2020-02"
...
"2020-52"
"2021-01"
...
"2021-52"
dtype: str
Right now there is no feature to present this kind of Period and I have to use it like this:
2019-12-29
2020-01-05
...
2020-12-20
2020-12-27
...
2021-12-19
dtype: period[7D]
What I suggest is a new period, say YW, which is formatted as YYYY-UU as below:
2020-01
2020-02
...
2020-52
2021-01
...
2021-52
dtype: period[YW]
Feature Description
def to_period(freq="YW"):
""""Gets a single index as "2021-52"
returns Period (2021-52, YW)
""""
Alternative Solutions
def convert_weekstr_to_period(idx: str) -> pd.Period:
"""Converts single string week to Period.
The weeks start from 00 and the offset of one week will be applied.
Args:
idx (str): str of format yyyy-ww, e.g., 2022-51
Returns:
Pandas Period: e.g., Period('2022-12-18', 'D')
"""
current_idx_dt = pd.to_datetime(idx + "-0", format="%Y-%U-%w")
one_week_offset = current_idx_dt - pd.Timedelta(weeks=1)
return one_week_offset.to_period("D")
def pd_series_weekstr_to_period(dsw: pd.Series) -> pd.Series:
"""Converts a Pandas Series string week to Period.
Args:
dsw (pd.Series): the Pandas Series with strings of format yyyy-Www
Returns:
Pandas Series: a Pandas Series with Period of format Period('2022-12-18', 'D')
"""
return dsw.map(convert_weekstr_to_period)
Additional Context
The current format has a day attached that is redundant.