Say we have a dataframe (df):
opendate
2020-08-04
2018-06-24
2011-03-17
2019-11-20
I want to do two things:
- For each date, count the number of days from beginning of the particular year to the date
- For each date, count the number of days from beginning of the particular month to the date
In R, I can do this by the following code:
Year_Month_Diff <- function(x, start) as.numeric(x - as.Date(cut(start, "year")));
df = transform(df, Year_day_played = Year_Month_Diff(opendate, opendate));
Month_Diff <- function(x, start) as.numeric(x - as.Date(cut(start, "month")));
df= transform(df, Month_day_played = Month_Diff(opendate, opendate));
Any help for the python equivalent will be appreciated.