Home > Software design >  How to set the value of a pandas dataframe column with an operation over the same column
How to set the value of a pandas dataframe column with an operation over the same column

Time:08-06

I am trying to execute the following sentence in a python script over a Pandas dataframe:

boxes_df["delivery_date"] = (dateutil.parser.parse(boxes_df["delivery_date"])   datetime.timedelta(weeks=weeks_before)).strftime("%Y-%m-%d")

But I'm getting this error:

TypeError: Parser must be a string or character stream, not Series

How can I make the such a calculation?

CodePudding user response:

You may want

df["delivery_date"] = (df["delivery_date"].apply(dateutil.parser.parse)   datetime.timedelta(weeks=weeks_before)).dt.strftime("%Y-%m-%d")

But more pandas way is

df["delivery_date"] = (pd.to_datetime(df["delivery_date"])   pd.Timedelta(weeks=weeks_before)).dt.strftime("%Y-%m-%d")

.dt.strftime("%Y-%m-%d") can be replace by .dt.date

  • Related