ENH: Add accessor for isocalendar returning iso year, week, and day · Issue #33206 · pandas-dev/pandas (original) (raw)

This enhancement request is to add an accessor to DatetimeProperties for the ISO year, which is the year corresponding to the week calculated via ISO 8601.

Currently, we can access the ISO week via week and weekofyear, and Timestamp objects provide isocalendar, which returns the correct ISO year, week, and day.

Examples

For example:

df = pandas.DataFrame()

df["event_date"] = ["2020-01-01", "2019-12-31"] df["event_date"] = df.event_date.astype("datetime64[D]")

df.event_date.dt.weekofyear.values, df.event_date.dt.year.values

outputs:

(array([1, 1]), array([2020, 2019]))

and accessing the Timestamp objects

df.event_date[0].isocalendar(), df.event_date[1].isocalendar()

yields

((2020, 1, 3), (2020, 1, 2))

It would be great to have an accessor to get access this isocalendar year value for a series, e.g.:

df.event_date.dt.year_for_weekofyear.values

or maybe even better to have an isocalendar accessor where the same iso values are available like in Timestamp.isocalendar, e.g.

df.event_date.dt.isocalendar.values

array([[2020, 1, 3], [2020, 1, 2]]))

I am happy to try a PR here, but some input would be great as to: