Python Pandas - 创建半开时间区间并检查端点是否存在
要创建半开时间区间,请使用 **pandas.Interval()** 并将 closed 参数设置为 right。要检查端点是否存在,请使用 in 属性。
首先,导入所需的库:
import pandas as pd
使用值为“right”的“closed”参数设置半开区间。半开区间,即 (0, 5],描述为 0 < x <= 5 (closed='right')
interval = pd.Interval(left=0, right=20, closed='right')
显示区间
print("Interval...\n",interval)检查区间中是否存在元素。这表明 closed = right 只包含最右端的端点。
print("\nThe left-most element exists in the Interval? = \n",0 in interval)
print("\nThe right-most element exists in the Interval? = \n",20 in interval)示例
以下是代码:
import pandas as pd
# Half-Closed interval set using the "closed" parameter with value "right"
# Half-Closed i.e. (0, 5] is described by 0 < x <= 5 (closed='right').
interval = pd.Interval(left=0, right=20, closed='right')
# display the interval
print("Interval...\n",interval)
# display the interval length
print("\nInterval length...\n",interval.length)
# check for the existence of an element in an Interval
# This shows that closed = right contain only the right-most endpoint
print("\nThe left-most element exists in the Interval? = \n",0 in interval)
print("\nThe right-most element exists in the Interval? = \n",20 in interval)输出
这将产生以下代码:
Interval... (0, 20] Interval length... 20
广告
数据结构
网络
关系数据库管理系统 (RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP