pandas.Interval#

class pandas.Interval#

实现区间的不可变对象,区间是有界的类似切片的区间。

参数
left可排序标量

中场休息时的左路。

right可排序标量

中场休息时的右界。

closed{‘右’,‘左’,‘两者’,‘都不’},默认‘右’

间隔是左侧闭合、右侧闭合、两者都闭合还是两者都不闭合。有关更详细的说明,请参阅注释。

参见

IntervalIndex

所有在同一侧闭合的间隔对象的索引。

cut

将连续数据转换为离散箱(按区间对象分类)。

qcut

将连续数据转换为基于分位数的箱(区间对象的分类)。

Period

表示一段时间。

注意事项

这些参数 leftright 必须来自同一类型,您必须能够比较它们,并且它们必须满足 left <= right

A closed interval (in mathematics denoted by square brackets) contains its endpoints, i.e. the closed interval [0, 5] is characterized by the conditions 0 <= x <= 5. This is what closed='both' stands for. An open interval (in mathematics denoted by parentheses) does not contain its endpoints, i.e. the open interval (0, 5) is characterized by the conditions 0 < x < 5. This is what closed='neither' stands for. Intervals can also be half-open or half-closed, i.e. [0, 5) is described by 0 <= x < 5 (closed='left') and (0, 5] is described by 0 < x <= 5 (closed='right').

示例

可以构建不同类型的间隔,如数字间隔:

>>> iv = pd.Interval(left=0, right=5)
>>> iv
Interval(0, 5, closed='right')

您可以检查元素是否属于该元素

>>> 2.5 in iv
True

你可以测试一下边界 (closed='right' ,所以 0 < x <= 5 ):

>>> 0 in iv
False
>>> 5 in iv
True
>>> 0.0001 in iv
True

计算它的长度

>>> iv.length
5

您可以通过以下方式操作 +* 并将运算应用于其每个边界,因此结果取决于绑定元素的类型

>>> shifted_iv = iv + 3
>>> shifted_iv
Interval(3, 8, closed='right')
>>> extended_iv = iv * 10.0
>>> extended_iv
Interval(0.0, 50.0, closed='right')

要创建时间间隔,可以使用时间戳作为界限

>>> year_2017 = pd.Interval(pd.Timestamp('2017-01-01 00:00:00'),
...                         pd.Timestamp('2018-01-01 00:00:00'),
...                         closed='left')
>>> pd.Timestamp('2017-01-01 00:00') in year_2017
True
>>> year_2017.length
Timedelta('365 days 00:00:00')

属性

closed 

间隔是左侧闭合、右侧闭合、两者都闭合还是两者都不闭合。

closed_left 

检查左侧的间隔是否闭合。

closed_right 

检查右侧的间隔是否闭合。

is_empty 

指示间隔是否为空,表示该间隔不包含任何点。

left 

中场休息时的左路。

length 

返回间隔的长度。

mid 

返回间隔的中点。

open_left 

检查左侧的间隔是否打开。

open_right 

检查右侧的间隔是否打开。

right 

中场休息时的右界。

方法:

overlaps 

检查两个间隔对象是否重叠。