
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Python Pandas - Create a time interval and use Timestamps as the bounds
To create a time interval and use Timestamps as the bounds, use pandas.Interval and set timestamp within it using pandas.Timestamp.
At first, import the required libraries −
import pandas as pd
Use Timestamps as the bounds to create a time interval. Closed interval set using the "closed" parameter with value "left"
interval = pd.Interval(pd.Timestamp('2020-01-01 00:00:00'), pd.Timestamp('2021-01-01 00:00:00'), closed='left')
Above, we have used Timestamps as the bounds. Display the interval
print("Interval...\n",interval)
Example
Following is the code
import pandas as pd # Use Timestamps as the bounds to create a time interval # closed interval set using the "closed" parameter with value "left" interval = pd.Interval(pd.Timestamp('2020-01-01 00:00:00'), pd.Timestamp('2021-01-01 00:00:00'), closed='left') # display the interval print("Interval...\n",interval) # display the interval length print("\nInterval length...\n",interval.length)
Output
This will produce the following code
Interval... [2020-01-01, 2021-01-01) Interval length... 366 days 00:00:00
- Related Articles
- Python Pandas - Create a closed time interval and check for existence of both the endpoints
- Python Pandas - Create a half-open time interval and check for existence of endpoints
- Python Pandas - Create a half-closed time interval and check for existence of endpoints
- Python Pandas - Create an open time interval and check for existence of both the endpoints
- Python - Create a Pandas array for interval data
- Finding difference in Timestamps – Python Pandas
- Compare specific Timestamps for a Pandas DataFrame – Python
- Create a Pivot Table as a DataFrame – Python Pandas
- Python Pandas - Check if an interval set as open is empty
- Python Pandas - Return the midpoint of each Interval in the IntervalArray as an Index
- Python Pandas - Check if the Pandas Index holds Interval objects
- Python Pandas - Get the length of the Interval
- Python Pandas - Return the midpoint of the Interval
- python Pandas - Return the left endpoints of each Interval in the IntervalArray as an Index
- Python Pandas - Return the right endpoints of each Interval in the IntervalArray as an Index

Advertisements