month begining dates 2 mnths back - python-3.x

I have dates in yyyy-dd-mm in a column(col 1).(type = pandas.core.series.Series and dtypes=datetime64[ns]). I need begining of the month date for 2 months prior(col 2)
col 1 col2
2021-01-07 2020-01-05
I have been trying to use timedelta & relative timedelta , but not working. can anyone help?

Since timedelta does not have months parameter, you can use dateutil module instead:
from dateutil.relativedelta import relativedelta
df['col2'] = df['col1'].apply(lambda x: x - relativedelta(months=2))

Related

Creating n quarter ahead date

I have following date object
import datetime as datetime
import pandas as pd
Date = pd.Timestamp(datetime.datetime(2000, 1, 31, 0, 0))
Now I want to create another date which is 5 quarters ahead from Date.
Is there any direct method to achieve this?
(pd.Period(Date, freq='Q')+5).to_timestamp()
Timestamp('2001-04-01 00:00:00')
to make it end of month
(pd.Period(Date, freq='Q')+5).to_timestamp(freq='M')
Timestamp('2001-04-30 00:00:00')

Pandas' offset.yearend is returning next year

I have observed that, pandas' offsets.YearEnd is returning next year if input date is year end. Below is one such example
import pandas as pd
pd.to_datetime('2000-12-31') + pd.offsets.YearEnd() ## Timestamp('2001-12-31 00:00:00')
Is it expected behaviour? How can I force to have same year if actual date itself is year-end?
You can explicitly specify the offset as 0 which will make sure that the calendar year does not gets incremented, See the below example,
>>> pd.to_datetime('2000-12-31') + pd.offsets.YearEnd(0)
Timestamp('2000-12-31 00:00:00')
>>> pd.to_datetime('2000-06-02') + pd.offsets.YearEnd(0)
Timestamp('2000-12-31 00:00:00')

Python - Calculate date difference in months

I need to get the difference between the days in months and days (eg. 3months 20days).
from datetime import datetime
from dateutil import relativedelta
date1 = datetime.strptime('2019-06-23', "%Y-%m-%d")
date2 = datetime.strptime('2018-04-17', '%Y-%m-%d')
r = relativedelta.relativedelta(date1, date2)
print(r)
This gives me result like relativedelta(years=+1, months=+2, days=+6) whereas I need result like 14 months 6 days
Thank you
Incorporate a minor modification to get the answer.
print(r.years, 'years,', r.months,'months and', r.days, 'days')

Compute difference in days between two date variables - Python

I have two date variables, and I tried to compute the difference in days between them with:
from datetime import date, timedelta,datetime
date_format = "%Y/%m/%d"
a = datetime.strptime(df.D1, date_format)
b = datetime.strptime(df.D2, date_format)
df['delta'] = b - a
print delta.days
But I'm getting this error:
TypeError: strptime() argument 1 must be str, not Series
How could I do this? The variables are objects, should I transform them in Datatime64?
Since you're working with pandas, you can use pd.to_datetime instead of the datetime package:
# Convert each date column to datetime:
df['D1'] = pd.to_datetime(df.D1,format='%Y/%m/%d')
df['D2'] = pd.to_datetime(df.D2,format='%Y/%m/%d')
# With 2 datetime Series, a simple subtraction will give you a Timedelta column:
df['delta'] = df.D1 - df.D2
For example:
>>> df
D1 D2
0 2015/05/18 2014/06/21
1 2015/10/18 2014/08/14
df['D1'] = pd.to_datetime(df.D1,format='%Y/%m/%d')
df['D2'] = pd.to_datetime(df.D2,format='%Y/%m/%d')
df['delta'] = df.D1 - df.D2
>>> df
D1 D2 delta
0 2015/05/18 2014/06/21 331 days
1 2015/10/18 2014/08/14 430 days

total number of days from current date if we want to know like after 2 weeks, 3 months , 1 year

I want to know the total number of days from current date.
if given date is 2 Feb 2018. and i want to calculate number of days after 2 weeks/3 months/1 year. keeping in mind of leap year or not.
Language - python3
Is there any library in python which give me this.
The dateutil module has a way to do this very conveniently. Sample code:
import datetime
import dateutil
today = datetime.date.today()
delta = dateutil.relativedelta.relativedelta(years=1, months=3, weeks=2)
desired_date = today + delta
desired_date
Output:
datetime.date(2019, 5, 16)

Resources