Issue
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?
Solution
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
Answered By - Ynjxsjmh
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.