You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Pandas DataFrame中提取特定时分及时段的数据?

提取指定时间/时段的股票数据

1. 提取每日09:30:00的数据行

由于你的DataFrame索引是datetime类型,直接利用时间索引的time属性筛选即可:

import pandas as pd

# 筛选每日09:30:00的数据
df_930 = df[df.index.time == pd.to_datetime('09:30:00').time()]

也可以用更简洁的between_time写法,将起止时间设为同一值:

df_930 = df.between_time('09:30:00', '09:30:00')

2. 提取09:30:00至09:35:00的时段数据

使用Pandas内置的between_time方法,它会自动匹配所有日期内的目标时段:

# 筛选每日09:30到09:35的数据
df_930_935 = df.between_time('09:30:00', '09:35:00')

如果你的datetime索引带时区,注意确保时间字符串与索引时区一致,或在调用between_time时指定timezone参数。

内容的提问来源于stack exchange,提问作者Wenjie Yu

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.12 18:12:00