pandas.Interval#
- class pandas.Interval[source]#
Immutable object implementing an Interval, a bounded slice-like interval。
属性
另请参阅
IntervalIndexAn Index of Interval objects that are all closed on the same side。
cutConvert continuous data into discrete bins (Categorical of Interval objects)。
qcutConvert continuous data into bins (Categorical of Interval objects) based on quantiles。
Period表示一个时间段。
注意
The parameters left and right must be from the same type, you must be able to compare them and they must satisfy
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 conditions0 <= x <= 5. This is whatclosed='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 conditions0 < x < 5. This is whatclosed='neither'stands for. Intervals can also be half-open or half-closed, i.e.[0, 5)is described by0 <= x < 5(closed='left') and(0, 5]is described by0 < x <= 5(closed='right')。示例
It is possible to build Intervals of different types, like numeric ones
>>> iv = pd.Interval(left=0, right=5) >>> iv Interval(0, 5, closed='right')
You can check if an element belongs to it, or if it contains another interval
>>> 2.5 in iv True >>> pd.Interval(left=2, right=5, closed='both') in iv True
You can test the bounds (
closed='right', so0 < x <= 5)>>> 0 in iv False >>> 5 in iv True >>> 0.0001 in iv True
Calculate its length
>>> iv.length 5
You can operate with + and * over an Interval and the operation is applied to each of its bounds, so the result depends on the type of the bound elements
>>> 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')
To create a time interval you can use Timestamps as the bounds
>>> 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')
属性
描述区间包含的边的字符串。
检查区间左侧是否包含。
检查区间右侧是否包含。
指示区间是否为空,意味着它不包含任何点。
区间的左边界。
返回区间的长度。
返回区间的中间点。
检查区间左侧是否开放。
检查区间右侧是否开放。
区间的右边界。
Methods
overlaps(other)检查两个 Interval 对象是否重叠。