0

I am trying to get the next month first date based on billDate in a dataframe.

I did this:

import pandas as pd import datetime from datetime import timedelta dt = pd.to_datetime('15/4/2019', errors='coerce') print(dt) print((dt.replace(day=1) + datetime.timedelta(days=32)).replace(day=1)) 

It is working perfectly, and the output is :

2019-04-15 00:00:00 2019-05-01 00:00:00 

Now, I am applying same logic in my dataframe in the below code

df[comNewColName] = (pd.to_datetime(df['billDate'], errors='coerce').replace(day=1) + datetime.timedelta(days=32)).replace(day=1) 

But I am getting error like this:

---> 69 df[comNewColName] = (pd.to_datetime(df['billDate'], errors='coerce').replace(day=1) + datetime.timedelta(days=32)).replace(day=1) 70 '''print(df[['billDate']])''' 71 '''df = df.assign(Product=lambda x: (x['Field_1'] * x['Field_2'] * x['Field_3']))''' 

TypeError: replace() got an unexpected keyword argument 'day'

2

1 Answer 1

2

You can use Series.to_period for month periods, add 1 for next month and then convert back to datetimes by Series.dt.to_timestamp:

print (df) billDate 0 15/4/2019 1 30/4/2019 2 15/8/2019 df['billDate'] = (pd.to_datetime(df['billDate'], errors='coerce', dayfirst=True) .dt.to_period('m') .add(1) .dt.to_timestamp()) print (df) billDate 0 2019-05-01 1 2019-05-01 2 2019-09-01 
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, it perfectly working. but logic is complex. that logic should appear like a pseudo code. Then I have to place that expression in my json. My BA team also able to understand.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.