答案 0 :(得分:4)
使用mask
进行遮罩,然后使用cummin
。
# df.index = pd.to_datetime(df.index, errors='coerce')
df['cummin'] = df.number.mask(df.index.strftime('%H:%M') < '09:30').cummin()
您还可以查询索引的hour
和minute
属性来获取小时数:
df['cummin'] = df.loc[
(df.index.hour >= 9) & (df.index.minute > 30), 'number'].cummin()
MCVE:
df = pd.DataFrame([1.4, 4.5, 2.3],
index=['9:00', '9:31', '9:45'],
columns=['number'])
df.index = pd.to_datetime(df.index)
df
number
2018-12-21 09:00:00 1.4
2018-12-21 09:31:00 4.5
2018-12-21 09:45:00 2.3
df.assign(number=(
df.number.mask(df.index.strftime('%H:%M') < '09:30').cummin()))
number cummin
2018-12-21 09:00:00 NaN NaN
2018-12-21 09:31:00 4.5 4.5
2018-12-21 09:45:00 2.3 2.3
df.assign(number=df.loc[
(df.index.hour >= 9) & (df.index.minute > 30), 'number'].cummin())
number cummin
2018-12-21 09:00:00 NaN NaN
2018-12-21 09:31:00 4.5 4.5
2018-12-21 09:45:00 2.3 2.3
答案 1 :(得分:2)
将between_time
与expanding
一起使用
df['new']=df.between_time('09:30','23:59').expanding().min()
df
number new
2018-12-20 09:00:00 1.4 NaN
2018-12-20 09:31:00 4.5 4.5
2018-12-20 09:45:00 2.3 2.3