我正在以5分钟的间隔(:05,:10,:15等)分析带有带时间戳的数据的数据帧。一些测量延迟了一分钟我想纠正以便能够将这些数据与另一个数据帧匹配(例如'2010-11-12 10:16'应该是'2010-11-12 10:15'。
到目前为止我的代码:
for index, row in trafficdata.iterrows():
text=row['periodStart']
if text[15]=='1':
text=text[:15]+'0'+text[16:]
if text[15]=='6':
text=text[:15]+'5'+text[16:]
row['periodStart']=text
print(row['periodStart'])
etc etc
数据框似乎已更改(最后一行中的print命令给出了更改的时间戳),但是当我将数据帧保存到csv文件时,旧值仍然存在。
我做错了什么?在此先感谢您的帮助!
答案 0 :(得分:0)
将日期设为日期时间,然后使用datetime replace method。
import pandas as pd
my_times = ['2015-01-31 01:01:00','2015-01-31 01:05:00','2015-01-31 01:11:00','2015-01-31 01:15:00','2015-01-31 01:16:00',
'2015-01-31 01:21:00','2015-01-31 01:25:00','2015-01-31 01:30:00','2015-01-31 01:35:00','2015-01-31 01:41:00',
'2015-01-31 01:45:00','2015-01-31 01:50:00','2015-01-31 01:55:00']
my_vals = range(260)[::20]
data = zip(my_times,my_vals)
df = pd.DataFrame(data,columns=['my_times','my_vals'])
df.my_times = pd.to_datetime(df.my_times)
def min_replace(x):
if x.minute % 5 != 0:
new_minute = int(round(x.minute/5.0)*5)
x = my_time.replace(minute = new_minute)
return x
else:
return x
df['my_times'] = df['my_times'].apply(min_replace)
来变换:
my_times my_vals
0 2015-01-31 01:01:00 0
1 2015-01-31 01:05:00 20
2 2015-01-31 01:11:00 40
3 2015-01-31 01:15:00 60
4 2015-01-31 01:16:00 80
5 2015-01-31 01:21:00 100
6 2015-01-31 01:25:00 120
7 2015-01-31 01:30:00 140
8 2015-01-31 01:35:00 160
9 2015-01-31 01:41:00 180
10 2015-01-31 01:45:00 200
11 2015-01-31 01:50:00 220
12 2015-01-31 01:55:00 240
分为:
my_times my_vals
0 2015-01-31 01:00:00 0
1 2015-01-31 01:05:00 20
2 2015-01-31 01:10:00 40
3 2015-01-31 01:15:00 60
4 2015-01-31 01:15:00 80
5 2015-01-31 01:20:00 100
6 2015-01-31 01:25:00 120
7 2015-01-31 01:30:00 140
8 2015-01-31 01:35:00 160
9 2015-01-31 01:40:00 180
10 2015-01-31 01:45:00 200
11 2015-01-31 01:50:00 220
12 2015-01-31 01:55:00 240