All Courses

Python Pandas - Timedelta

Neha Kumawat

2 years ago

Python Pandas - Timedelta | insideaiml
Table of Contents
  • String
  • Integer
  • Data Offsets
  • to_timedelta()
  • Operations
  • Addition Operation
  • Subtraction Operation
   
          Timedeltas are differences in times, expressed in difference units, for example, days, hours, minutes, seconds. They can be both positive and negative.
We can create Timedelta objects using various arguments as shown below −

String

Passing a string literal, we can create a timedelta object. This can be done as shown below:
import pandas as pd

print(pd.Timedelta('2 days 2 hours 15 minutes 30 seconds'))
Its output is as follows −

2 days 02:15:30

Integer

Passing an integer value with the unit, an argument creates a Timedelta object.
import pandas as pd

print(pd.Timedelta(6,unit='h'))
Its output is as follows −

0 days 06:00:00

Data Offsets

          Data offsets such as - weeks, days, hours, minutes, seconds, milliseconds, microseconds, nanoseconds can also be used in construction.
import pandas as pd

print(pd.Timedelta(days=2))
Its output is as follows −

2 days 00:00:00

to_timedelta()

          Using the top-level pd.to_timedelta, you can convert a scalar, array, list, or series from a recognized timedelta format/ value into a Timedelta type. It will construct Series if the input is a Series, a scalar if the input is scalar-like, otherwise will output a TimedeltaIndex.
import pandas as pd

print(pd.Timedelta(days=2))
Its output is as follows −

2 days 00:00:00

Operations

             You can operate on Series/ DataFrames and construct timedelta64[ns] Series through subtraction operations on datetime64[ns] Series, or Timestamps.
Let us now create a DataFrame with Timedelta and datetime objects and perform some arithmetic operations on it −
import pandas as pd

s = pd.Series(pd.date_range('2012-1-1', periods=3, freq='D'))
td = pd.Series([ pd.Timedelta(days=i) for i in range(3) ])
df = pd.DataFrame(dict(A = s, B = td))

print(df)
Its output is as follows −

            A      B
0  2012-01-01 0 days
1  2012-01-02 1 days
2  2012-01-03 2 days

Addition Operations

import pandas as pd

s = pd.Series(pd.date_range('2012-1-1', periods=3, freq='D'))
td = pd.Series([ pd.Timedelta(days=i) for i in range(3) ])
df = pd.DataFrame(dict(A = s, B = td))
df['C']=df['A']+df['B']

print(df)
Its output is as follows −

           A      B          C
0 2012-01-01 0 days 2012-01-01
1 2012-01-02 1 days 2012-01-03
2 2012-01-03 2 days 2012-01-05

Subtraction Operation

import pandas as pd

s = pd.Series(pd.date_range('2012-1-1', periods=3, freq='D'))
td = pd.Series([ pd.Timedelta(days=i) for i in range(3) ])
df = pd.DataFrame(dict(A = s, B = td))
df['C']=df['A']+df['B']
df['D']=df['C']+df['B']

print(df)
Its output is as follows −

           A      B          C          D
0 2012-01-01 0 days 2012-01-01 2012-01-01
1 2012-01-02 1 days 2012-01-03 2012-01-04
2 2012-01-03 2 days 2012-01-05 2012-01-07
 I hope you enjoyed reading this article and finally, you came to know about Python Pandas - Timedelta.
For more such blogs/courses on data science, machine learning, artificial intelligence and emerging new technologies do visit us at InsideAIML.
Thanks for reading…
Happy Learning…

Submit Review