DataFrame.DateOffset()

Add or subtract date and time parts.
import pandas as pd 
my_dict={'NAME':['Ravi','Raju','Alex'],
         'dt_start':['31/1/2020','29/2/2020','28/2/2019']
         }
df = pd.DataFrame(data=my_dict)
df['dt_start'] = pd.to_datetime(df['dt_start'])
print(df)
We created one date timedelta64 column by using to_datetime(). Here is the output
   NAME   dt_start
0  Ravi 2020-01-31
1  Raju 2020-02-29
2  Alex 2019-02-28
We can add ( or subtract ) dates from above values by using keywords years, months, weeks, days, hours, minutes, seconds, microseconds, nanoseconds
We can REPLACE part of the date object also.

Adding or subtracting days

Let us add 365 days to our DataFrame.
df['dt_end']=df['dt_start']+pd.DateOffset(days=365)
print(df)
Output ( one new column dt_end is added )
   NAME   dt_start     dt_end
0  Ravi 2020-01-31 2021-01-30
1  Raju 2020-02-29 2021-02-28
2  Alex 2019-02-28 2020-02-28
Subtracting days
df['dt_end']=df['dt_start']-pd.DateOffset(days=365)

Adding Year

df['dt_end']=df['dt_start']+pd.DateOffset(years=2)

Adding Months

df['dt_end']=df['dt_start']+pd.DateOffset(months=3)

Adding Year month and days

df['dt_end']=df['dt_start']+\
  pd.DateOffset(years=2,months=3,days=13)

Adding Hours

df['dt_end']=df['dt_start']+pd.DateOffset(hours=2)

Adding Hours Minutes and seconds

df['dt_end']=df['dt_start']+\
  pd.DateOffset(hours=2,minutes=50,seconds=43)
Output is here
  NAME   dt_start              dt_end
0  Ravi 2020-01-31 2020-01-31 02:50:43
1  Raju 2020-02-29 2020-02-29 02:50:43
2  Alex 2019-02-28 2019-02-28 02:50:43
Similarly we can add microseconds and nanoseconds

Replace

In above code we have added ( or subtracted ) the date and time parts. We can replace the parts by using different set of keywords.
Note that year ( used above ) is not same as years.
year month day weekday 
hour minute second microsecond nanosecond
We are updating the year part only ( not adding or subtracting )
df['dt_end']=df['dt_start']-pd.DateOffset(year=2023)
PerformanceWarning: Non-vectorized DateOffset being applied to Series or DatetimeIndex
  PerformanceWarning,
In above case better to use timedelta64. Check the sample code below.

Difference between day and days

compare the two outputs, when we use day=15, we are replacing the day part. In second case days=15 we are subtracting 15 days.
import pandas as pd
dt='2021-02-25'
print(pd.Timestamp(dt)-pd.DateOffset(day=15)) # 2021-02-15 00:00:00
print(pd.Timestamp(dt)-pd.DateOffset(days=15))# 2021-02-10 00:00:00

DateOffset and Timestamp

We can add or subtract to any Timestamp by using DateOffset.
tm=pd.Timestamp(year=2020,month=12,day=23,hour=18,minute=20,second=5)
print(tm+pd.offsets.DateOffset(months=2))
Output
2021-02-23 18:20:05
Adding Dateoffset to current date and time.
tm=pd.Timestamp('now') # current timestamp
x=pd.offsets.DateOffset(years=1,months=2,days=3,\
                        hours=4,minutes=40,seconds=20)
print(tm+x)
Output
2022-08-07 14:48:00.151036
Using timedelta64 we can add or subtact date parts.
import pandas as pd
import numpy as np
tm=pd.Timestamp('now') # current timestamp
y=np.timedelta64(1,'M') # adding one month
z=np.timedelta64(1,'Y') # adding one year 
print(tm+y+z)
Output
2022-07-05 02:35:38.978611
Pandas date & time to_datetime() period_range() date_range()
Subscribe to our YouTube Channel here


Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com



    Post your comments , suggestion , error , requirements etc here





    Python Video Tutorials
    Python SQLite Video Tutorials
    Python MySQL Video Tutorials
    Python Tkinter Video Tutorials
    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer