Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
Python Pandas - Get the left bound for the interval
In Pandas, an Interval represents a range between two bounds. To get the left bound for the interval, use the interval.left property ?
Syntax
interval.left
Where interval is a Pandas Interval object.
Creating an Interval with Timestamps
Let's create a time interval using Timestamps as bounds ?
import pandas as pd
# Create a time interval with left-closed boundary
interval = pd.Interval(pd.Timestamp('2020-01-01 00:00:00'),
pd.Timestamp('2021-01-01 00:00:00'),
closed='left')
print("Interval...")
print(interval)
Interval... [2020-01-01, 2021-01-01)
Getting the Left Bound
Use the left property to access the left bound of the interval ?
import pandas as pd
# Create interval
interval = pd.Interval(pd.Timestamp('2020-01-01 00:00:00'),
pd.Timestamp('2021-01-01 00:00:00'),
closed='left')
# Get the left bound
left_bound = interval.left
print("The left bound for the Interval...")
print(left_bound)
print("Type:", type(left_bound))
The left bound for the Interval... 2020-01-01 00:00:00 Type: <class 'pandas._libs.tslibs.timestamps.Timestamp'>
Complete Example
Here's a comprehensive example showing interval creation and accessing its properties ?
import pandas as pd
# Create a time interval
interval = pd.Interval(pd.Timestamp('2020-01-01 00:00:00'),
pd.Timestamp('2021-01-01 00:00:00'),
closed='left')
# Display interval properties
print("Interval...")
print(interval)
print("\nInterval length...")
print(interval.length)
print("\nThe left bound for the Interval...")
print(interval.left)
print("\nThe right bound for the Interval...")
print(interval.right)
Interval... [2020-01-01, 2021-01-01) Interval length... 366 days 00:00:00 The left bound for the Interval... 2020-01-01 00:00:00 The right bound for the Interval... 2021-01-01 00:00:00
Key Points
- The
leftproperty returns the left boundary of the interval - For Timestamp intervals, it returns a Timestamp object
- The notation
[2020-01-01, 2021-01-01)shows a left-closed interval - Square bracket
[means the left bound is included - Parenthesis
)means the right bound is excluded
Conclusion
Use interval.left to access the left bound of a Pandas Interval. This property returns the same type as the original bound values, such as Timestamp objects for time intervals.
Advertisements
