到目前为止,这是我的代码:
df_date = dataframe['date'] # get date column
df_date = datedf.to_frame() #convert series to df
for index, row in df_date.iterrows():
if (index % 3 == 0):
#minute +1 f. e. from 12:00:40 to 12:01:0
elif (index % 3 == 1) or (index % 3 == 2):
# seconds + 20
# from 12:01:00 to 12:01:20
# or from 12:01:20 to 12:01:40
数据帧df_date:
原始:
0 2017-03-10 01:00:00
1 2017-03-10 01:00:00
2 2017-03-10 01:00:00
3 2017-03-10 01:00:00
4 2017-03-10 01:00:00
...
预期:
0 2017-03-10 01:00:20
1 2017-03-10 01:00:40
2 2017-03-10 01:01:00
3 2017-03-10 01:00:20
4 2017-03-10 01:00:40
...
任何帮助,将不胜感激!
答案 0 :(得分:3)
添加由np.arange
和20
与to_timedelta
的倍数创建的时间增量:
td = pd.to_timedelta(np.arange(1, len(dataframe) + 1) * 20, unit='s')
或由timedelta_range
创建:
td = pd.timedelta_range(0, periods=len(dataframe), freq='20s')
dataframe['date'] = pd.to_datetime(dataframe['date']) + td
print (dataframe)
date
0 2017-03-10 01:00:20
1 2017-03-10 01:00:40
2 2017-03-10 01:01:00
3 2017-03-10 01:01:20
4 2017-03-10 01:01:40