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:
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.
The month is really simple, just call .dt.day.
For the year case, you subtract the date from Jan 1 of the same year, and count the number of days.
Assuming opendate is already of type Timestamp:
df['Days since BOM'] = df['opendate'].dt.day
df['Days since BOY'] = (df['opendate'] - (df['opendate'] - pd.tseries.offsets.YearBegin())).dt.days
Thanks to @ChrisA, there's an even simpler solution for the year case:
df['Days since BOY'] = df['opendate'].dt.dayofyear
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With