DataFrame.to_timedelta()

This converts arguments to timedelta format ( timedelta64[ns] is a dtype in Pandas ). We will use this for addition of different date and time components.

Options

arg: the data to be converted.
unit: default is ns, possible values are 'Y','M','W','D',days, day,hours, hour, minute, sec, milliseconds,millis, micro, microseconds, ns, nanoseconds,nano,nanoseconds
errors:Default value is 'raise'. We can set it to errors='ignore'. If we set to coerce errors='coerce' then invalid parsing will be set as NaT.

We will use present date and time and to it we will add different components. We used to_datetime() to get present date time.

Adding 10 days to today

import pandas as pd 
present_day=pd.to_datetime('now') # current day 
future_day=present_day + pd.to_timedelta(10,unit='days')
print("Present Date and time :", present_day)
print("Future Date and time : ",future_day)
Output
Present Date and time : 2020-04-13 13:35:44.877832
Future Date and time :  2020-04-23 13:35:44.877832

Adding 4 months to today

import pandas as pd 
present_day=pd.to_datetime('now') # current day 
future_day=present_day + pd.to_timedelta(4,unit='M')
print("Present Date and time :", present_day)
print("Future Date and time : ",future_day)
Output
Present Date and time : 2020-04-13 13:48:30.168673
Future Date and time :  2020-08-13 07:44:54.168673

Adding 4 years to today

import pandas as pd 
present_day=pd.to_datetime('now') # current day 
future_day=present_day + pd.to_timedelta(4,unit='Y')
print("Present Date and time :", present_day)
print("Future Date and time : ",future_day)
Output
Present Date and time : 2020-04-13 13:50:29.531906
Future Date and time :  2024-04-13 13:07:17.531906

Adding 2 weeks to today

import pandas as pd 
present_day=pd.to_datetime('now') # current day 
future_day=present_day + pd.to_timedelta(2,unit='W')
print("Present Date and time :", present_day)
print("Future Date and time : ",future_day)
Output
Present Date and time : 2020-04-13 13:50:55.697808
Future Date and time :  2020-04-27 13:50:55.697808

Adding 2 hours to current time

import pandas as pd 
present_day=pd.to_datetime('now') # current day 
future_day=present_day + pd.to_timedelta(2,unit='H')
print("Present Date and time :", present_day)
print("Future Date and time : ",future_day)
Output
Present Date and time : 2020-04-13 13:51:58.302150
Future Date and time :  2020-04-13 15:51:58.302150

Adding 10 minutes to present time

import pandas as pd 
present_day=pd.to_datetime('now') # current day 
future_day=present_day + pd.to_timedelta(10,unit='minutes')
print("Present Date and time :", present_day)
print("Future Date and time : ",future_day)
Output
Present Date and time : 2020-04-13 13:52:47.943290
Future Date and time :  2020-04-13 14:02:47.943290

Adding 10 seconds to present time

import pandas as pd 
present_day=pd.to_datetime('now') # current day 
future_day=present_day + pd.to_timedelta(10,unit='seconds')
print("Present Date and time :", present_day)
print("Future Date and time : ",future_day)
Output
Present Date and time : 2020-04-13 13:53:34.288719
Future Date and time :  2020-04-13 13:53:44.288719

adding 100 milli seconds to present time

import pandas as pd 
present_day=pd.to_datetime('now') # current day 
future_day=present_day + pd.to_timedelta(100,unit='millis')
print("Present Date and time :", present_day)
print("Future Date and time : ",future_day)
Output
Present Date and time : 2020-04-13 13:54:26.743903
Future Date and time :  2020-04-13 13:54:26.843903

adding 100 micro seconds to present time

import pandas as pd 
present_day=pd.to_datetime('now') # current day 
future_day=present_day + pd.to_timedelta(100,unit='micros')
print("Present Date and time :", present_day)
print("Future Date and time : ",future_day)
Output
Present Date and time : 2020-04-13 13:55:07.402857
Future Date and time :  2020-04-13 13:55:07.402957

With any datetime

In above code we have used current date and time and applied timedelta to it. We can also use any date and time to in our future date time calculation. We are using to_datetime() to create datetime
import pandas as pd
present_day=pd.to_datetime('12252020:233445',format='%m%d%Y:%H%M%S')
future_day=present_day + pd.to_timedelta(5,unit='Y')
print("Present Date and time :", present_day)
print("Future Date and time : ",future_day)
Output
Present Date and time : 2020-12-25 23:34:45
Future Date and time :  2025-12-26 04:40:45
Adding day hour minute and secondary
import pandas as pd 
dt_delta=pd.to_timedelta('1 days 5 hours 22 minute 34.006 second')
print(dt_delta)
dt_bd=pd.to_datetime('2001-11-27 16:20:05')
print(dt_bd + dt_delta)
Output
1 days 05:22:34.006000
2001-11-28 21:42:39.006000
td = pd.Series([pd.Timedelta(days=i) for i in range(6)]) 
print(td)
print(td.dtypes)
Output
0   0 days
1   1 days
2   2 days
3   3 days
4   4 days
5   5 days
dtype: timedelta64[ns]
timedelta64[ns]
Pandas date & time date_range() period_range() to_excel()
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