简化代码:def generate_time_series(start,end,t1,t2): current = start end_item = end time_delta = datetime.timedelta(days=t1,hours=t2) time_series = [current] while current < end_item: next1 = current + time_delta time_series.append(next1) current = next1 return time_series time_hour=generate_time_series(data1.loc[0]['FDT_TIME'],data1.loc[len(data1)-1]['FDT_TIME'],0,1) time_day=generate_time_series(data1.loc[0]['FDT_TIME'],data1.loc[len(data1)-1]['FDT_TIME'],1,0)
时间: 2023-06-09 16:05:35 浏览: 147
可以将代码简化为以下形式:
```
import datetime
def generate_time_series(start, end, days=0, hours=0):
current = start
time_delta = datetime.timedelta(days=days, hours=hours)
time_series = [current]
while current < end:
current += time_delta
time_series.append(current)
return time_series
time_hour= generate_time_series(data1.iloc[0]['FDT_TIME'],
data1.iloc[-1]['FDT_TIME'], hours=1)
time_day = generate_time_series(data1.iloc[0]['FDT_TIME'],
data1.iloc[-1]['FDT_TIME'], days=1)
```
注意,这里使用了 `iloc` 替代了原先的 `loc`,这样可以保证索引的正确性。同时,`generate_time_series` 函数增加了默认的 `days` 和 `hours` 参数,方便使用。
相关问题
请问pandas中有什么可以代替这个函数:def generate_time_series(start,end,t1,t2): current = start end_item = end time_delta = datetime.timedelta(days=t1,hours=t2) time_series = [current] while current < end_item: next1 = current + time_delta time_series.append(next1) current = next1 return time_series
你可以使用pandas中的date_range函数代替该函数。它可以生成一个时间序列,可以指定开始时间、结束时间以及时间间隔。例如,可以使用以下代码生成与您的函数相同的时间序列:
import pandas as pd
import datetime
start = datetime.datetime(2022, 1, 1)
end = datetime.datetime(2022, 1, 10)
t1 = 1
t2 = 0
time_series = pd.date_range(start=start, end=end, freq=f'{t1}D{t2}H').tolist()
print(time_series)
用pandas函数代替下列语句:def generate_time_series(start,end,t1,t2): current = start end_item = end time_delta = datetime.timedelta(days=t1,hours=t2) time_series = [current] while current < end_item: next1 = current + time_delta time_series.append(next1) current = next1 return time_series time_hour=generate_time_series(data1.loc[0]['FDT_TIME'],data1.loc[len(data1)-1]['FDT_TIME'],0,1) time_require=[time_hour[160],time_hour[162]] data1_require=data1.drop(data1[(data1['FDT_TIME']<time_require[0])].index) data1_require=data1_require.drop(data1_require[(data1_require['FDT_TIME']>time_require[1])].index) data1_require=data1_require[['FINT_SPEED']]
可以使用pandas.date_range()函数替换generate_time_series()函数来生成时间序列,用pandas.DataFrame.loc[]函数替换data1的切片操作,用pandas.DataFrame.drop()函数替换删除操作,代码如下:
import pandas as pd
import datetime
start = data1.loc[0]['FDT_TIME']
end = data1.loc[len(data1)-1]['FDT_TIME']
time_delta = datetime.timedelta(days=0, hours=1)
time_hour = pd.date_range(start=start, end=end, freq=time_delta)
time_require = [time_hour[160], time_hour[162]]
data1_require = data1.loc[(data1['FDT_TIME'] >= time_require[0]) & (data1['FDT_TIME'] <= time_require[1])]
data1_require = data1_require[['FINT_SPEED']]
阅读全文