看过来
《pandas 教程》 持续更新中,提供建议、纠错、催更等加作者微信: gr99123(备注:pandas教程)和关注公众号「盖若」ID: gairuo。跟作者学习,请进入 Python学习课程。欢迎关注作者出版的书籍:《深入浅出Pandas》 和 《Python之光》。
(编码题)公司 UPS 机房每 30 分钟自动记录一次温湿度。数据已对齐到半点或整点时间:
import pandas as pd
temp = pd.Series(
[23.1, 23.4, 24.0, 24.2, 23.8],
index=pd.to_datetime([
'2024-08-12 00:00', '2024-08-12 00:30',
'2024-08-12 01:00', '2024-08-13 00:00',
'2024-08-13 00:30']),
name='温度(°C)'
)
temp
'''
2024-08-12 00:00:00 23.1
2024-08-12 00:30:00 23.4
2024-08-12 01:00:00 24.0
2024-08-13 00:00:00 24.2
2024-08-13 00:30:00 23.8
Name: 温度(°C), dtype: float64
'''
用 一行代码(必须使用 at_time()
)取出 所有日期 00:00 整点的温度,并打印结果。
(跨日、多行、一次取准)
代码如下:
temp.at_time('00:00')
'''
2024-08-12 23.1
2024-08-13 24.2
Name: 温度(°C), dtype: float64
'''
(完)
更新时间:2025-08-18 17:07:38 标签:pandas python 零点 时间