- Comparison with SQL
- Python Pandas - Caveats & Gotchas
- Python Pandas - Sparse Data
- Python Pandas - IO Tools
- Python Pandas - Visualization
- Python Pandas - Categorical Data
- Python Pandas - Timedelta
- Python Pandas - Date Functionality
- Python Pandas - Concatenation
- Python Pandas - Merging/Joining
- Python Pandas - GroupBy
- Python Pandas - Missing Data
- Python Pandas - Aggregations
- Python Pandas - Window Functions
- Statistical Functions
- Indexing & Selecting Data
- Options & Customization
- Working with Text Data
- Python Pandas - Sorting
- Python Pandas - Iteration
- Python Pandas - Reindexing
- Function Application
- Descriptive Statistics
- Python Pandas - Basic Functionality
- Python Pandas - Panel
- Python Pandas - DataFrame
- Python Pandas - Series
- Introduction to Data Structures
- Python Pandas - Environment Setup
- Python Pandas - Introduction
- Python Pandas - Home
Python Pandas Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Python Pandas - Timedelta
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
By passing a string pteral, we can create a timedelta object.
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
By 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, milpseconds, 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, pst, 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-pke, 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-07Advertisements