我想在满足特定条件时增加一个数字,而在其他部分,我想减少该数字,但这应该在指定的限制之内。条件就像有一个整天的时间戳记,间隔15分钟,因此24小时总共有96个区块。现在,当时间在早上6点至晚上7点之间时,该数字应增加,但应达到一个特定的限制;当时间在晚上7点至第二天早上6点之间时,该数量应减少。我的代码是:
import pandas as pd
import datetime
timeStamp = pd.date_range(start='1/1/2020' , end = '1/1/2020', freq = '15min')
num = 0
numLowerLimit = 0
numHigherLimit = 1
chargeTime = 6
dischargeTime = 19
for t in timeStamp:
if chargeTime <= t.hour < dischargeTime:
if numLowerLimit <= num <= numHigherLimit:
num = num + 0.23
print(num)
else:
num = 1
print(num)
else:
if numLowerLimit <= num <= numHigherLimit:
num = num - 0.23
print(num)
else:
num = 0
print(num)
错误:
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
预期输出:
2020-01-01 06:00:00' - 0.23
2020-01-01 06:15:00' - 0.46
2020-01-01 06:30:00' - 0.69
2020-01-01 06:45:00' - 0.92
2020-01-01 07:00:00' - 1
2020-01-01 07:15:00' - 1
2020-01-01 07:30:00' - 1
.
.
.
2020-01-01 19:00:00' - 1
2020-01-01 19:15:00' - 0.77
2020-01-01 19:30:00' - 0.54
2020-01-01 19:45:00' - 0.31
2020-01-01 20:00:00' - 0.54
2020-01-01 20:15:00' - 0.31
2020-01-01 20:30:00' - 0.08
2020-01-01 20:45:00' - 0
2020-01-01 20:00:00' - 0
答案 0 :(得分:1)
尝试一下:
import pandas as pd
import datetime
timeStamp = pd.date_range(start='1/1/2020 06:00:00' , end = '1/1/2020 23:59:00', freq = '15min')
num = 0
numLowerLimit = 0
numHigherLimit = 1
chargeTime = 6
dischargeTime = 19
for t in timeStamp:
num = min([num + 0.23, 1]) if chargeTime <= t.hour < dischargeTime else max([num - 0.23, 0])
print(f'{t} {num:.2f}')