python - Round Date in Pandas Dataframe -
this question has answer here:
i'm attempting round date dataframe below 1st day of next month - i.e. 1997-10-10 result in 1997-11-01:
date 1997-10-10 1997-05-27 1997-04-30 1997-12-19 1997-08-12
currently code looks like:
df = pd.read_csv(xxx) df['date'] = pd.to_datetime(df.date) df['date'] = df['date'].dt.date.replace(day = 1)
according python library documentation,
date.replace(year=self.year, month=self.month, day=self.day) return date same value, except parameters given new values whichever keyword arguments specified. example, if d == date(2002, 12, 31), d.replace(day=26) == date(2002, 12, 26).
i had presumed use .replace
1 argument - day -however i'm receiving number of errors.
i think need monthbegin(0)
:
df['date'] = pd.to_datetime(df.date) + pd.offsets.monthbegin(0) print (df) date 0 1997-11-01 1 1997-06-01 2 1997-05-01 3 1998-01-01 4 1997-09-01
Comments
Post a Comment