pandas.interval_range#
- pandas.interval_range(start=None, end=None, periods=None, freq=None, name=None, closed=NoDefault.no_default, inclusive=None)[源代码]#
返回固定频率的IntervalIndex。
- 参数
- start数字或类似日期时间,默认为无
生成间隔的左界。
- end数字或类似日期时间,默认为无
生成区间的右界。
- periodsInt,默认为无
要生成的期间数。
- freq数字、字符串或日期偏移量,默认为无
每个间隔的长度。必须与开始和结束类型一致,例如,2表示数字,或‘5H’表示类似日期时间。对于数字,缺省值为1,对于类似日期时间,缺省值为‘D’。
- name字符串,默认为无
生成的IntervalIndex的名称。
- closed{‘左’,‘右’,‘两者’,‘都不’},默认‘右’
间隔是在左侧闭合、右侧闭合、两者都闭合还是都不闭合。
1.5.0 版后已移除: 论据 closed 已被弃用,以标准化边界输入。使用 inclusive 而是将每个边界设置为关闭或打开。
- inclusive{“两者”,“都不”,“左”,“右”},默认为“两者”
包括边界;是否将每个边界设置为闭合或开放。
1.5.0 新版功能.
- 退货
- IntervalIndex
参见
IntervalIndex
所有在同一侧闭合的区间的索引。
注意事项
四个参数中的
start
,end
,periods
,以及freq
,则必须恰好指定三个。如果freq
被省略,则会导致IntervalIndex
将会有periods
之间的线性间距元素start
和end
,包括。要了解有关类似DateTime的频率字符串的更多信息,请参阅 this link 。
示例
数字
start
和end
是受支持的。>>> pd.interval_range(start=0, end=5, inclusive="right") IntervalIndex([(0, 1], (1, 2], (2, 3], (3, 4], (4, 5]], dtype='interval[int64, right]')
此外,还支持类似DateTime的输入。
>>> pd.interval_range(start=pd.Timestamp('2017-01-01'), ... end=pd.Timestamp('2017-01-04'), inclusive="right") IntervalIndex([(2017-01-01, 2017-01-02], (2017-01-02, 2017-01-03], (2017-01-03, 2017-01-04]], dtype='interval[datetime64[ns], right]')
这个
freq
参数指定左侧和右侧之间的频率。中的各个间隔的端点IntervalIndex
。对于数字start
和end
,频率也必须是数字。>>> pd.interval_range(start=0, periods=4, freq=1.5, inclusive="right") IntervalIndex([(0.0, 1.5], (1.5, 3.0], (3.0, 4.5], (4.5, 6.0]], dtype='interval[float64, right]')
类似地,对于类似DateTime的
start
和end
,频率必须可转换为DateOffset。>>> pd.interval_range(start=pd.Timestamp('2017-01-01'), ... periods=3, freq='MS', inclusive="right") IntervalIndex([(2017-01-01, 2017-02-01], (2017-02-01, 2017-03-01], (2017-03-01, 2017-04-01]], dtype='interval[datetime64[ns], right]')
指定
start
,end
,以及periods
;频率是自动生成的(线性间隔)。>>> pd.interval_range(start=0, end=6, periods=4, inclusive="right") IntervalIndex([(0.0, 1.5], (1.5, 3.0], (3.0, 4.5], (4.5, 6.0]], dtype='interval[float64, right]')
这个
inclusive
参数指定每个间隔的哪些端点位于IntervalIndex
已经关门了。>>> pd.interval_range(end=5, periods=4, inclusive='both') IntervalIndex([[1, 2], [2, 3], [3, 4], [4, 5]], dtype='interval[int64, both]')