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')
Related
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')
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))
I'm trying to get a date from the user in dd/mm/yyyy format and want to store the day, month and year in a list. Is there someway in which this can be achieved?
Thanks,
Chandra.
You can access year,month and day using python datetime object
import datetime
d = "1/5/2020"
date = datetime.datetime.strptime(d,"%d/%m/%Y")
result = [date.year,date.month,date.day]
output
[2020, 5, 1]
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')
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)