pandas.DataFrame.between_time#
- DataFrame.between_time(start_time, end_time, inclusive='both', axis=None)[源码]#
选择一天中特定时间段内的值(例如,上午 9:00-9:30)。
通过将
start_time设置得晚于end_time,可以获取不在这两个时间段内的时段。- 参数:
- start_timedatetime.time 或 str
作为时间过滤器限制的起始时间。
- end_timedatetime.time 或 str
作为时间过滤器限制的结束时间。
- inclusive{“both”, “neither”, “left”, “right”}, 默认值 “both”
包含边界;是否将每个边界设置为闭合或开放。
- axis{0 或 ‘index’, 1 或 ‘columns’}, 默认为 0
在索引或列值上确定范围时间。对于 Series,此参数未使用,默认为 0。
- 返回:
- Series 或 DataFrame
来自原始对象的、已按指定日期范围过滤的数据。
- 引发:
- TypeError
如果索引不是
DatetimeIndex
另请参阅
at_time选择一天中特定时间的数值。
first根据日期偏移量选择时间序列的初始周期。
last根据日期偏移量选择时间序列的最终周期。
DatetimeIndex.indexer_between_time获取一天中特定时间段内的数值的索引位置。
示例
>>> i = pd.date_range("2018-04-09", periods=4, freq="1D20min") >>> ts = pd.DataFrame({"A": [1, 2, 3, 4]}, index=i) >>> ts A 2018-04-09 00:00:00 1 2018-04-10 00:20:00 2 2018-04-11 00:40:00 3 2018-04-12 01:00:00 4
>>> ts.between_time("0:15", "0:45") A 2018-04-10 00:20:00 2 2018-04-11 00:40:00 3
通过将
start_time设置得晚于end_time,可以获取不在这两个时间段内的时段。>>> ts.between_time("0:45", "0:15") A 2018-04-09 00:00:00 1 2018-04-12 01:00:00 4