Open mcrumiller opened 1 year ago
I don't know how to monkey patch this into the dt
namespace but otherwise, here ya go
def replace(self, year=None, month=None, day=None):
"""Mimic the datetime replace method"""
if year is None and month is None and day is None:
raise ValueError("don't you want to replace something, specify a year, month, and/or day")
if year is None:
outyear=self.dt.year()
else:
outyear=year
if month is None:
outmonth=self.dt.month()
else:
outmonth=month
if day is None:
outday=self.dt.day()
else:
outday=day
return pl.date(outyear, outmonth, outday)
pl.Expr.replace=replace
then you can do
df.select(
col("my_date").replace(year=col("year"))
)
Thanks @deanm0000 . Yeah, I currently have my own monkeypatched methods but I felt it would be a good addition to the API.
FYI, when you post code in github, you can fence it with a language, e.g. ```python
and you'll get syntax highlighting.
@MarcoGorelli does someone want to accept this feature request? I can work on it but I don't want to spend the time if it's not desired/will be rejected.
(FYI this is the original one :D)
sure, marking as 'accepted', feel free to ping me for review
Problem description
datetime.date
has areplace
function that lets you immediately set the day/month/year:This would be a nice feature for series/expressions, especially if the input arg could also be an expression: