python 对时间戳进行转换,且选定一个时间范围的数据
df1['ts_date'] = df1['ts'].map(lambda x: int(time.mktime(datetime.strptime(x, '%Y-%m-%d %H:%M:%S+0800').timetuple())))
#print(time.mktime(datetime.strptime(df1['ts'][0], '%Y-%m-%d %H:%M:%S+0800').timetuple()))
df2['st_date'] = df2['st_ts'].map(lambda x: int(time.mktime(datetime.strptime(x, '%Y-%m-%d %H:%M:%S+0800').timetuple())))
df2['ed_date'] = df2['ed_ts'].map(lambda x: int(time.mktime(datetime.strptime(x, '%Y-%m-%d %H:%M:%S+0800').timetuple())))
#print (df2)
for idx,row1 in df2.iterrows():
stt=row1['st_date']
ett=row1['ed_date']
yt = row1['seqIdx']
yyt = row1['p']
temp1 = df1[((df1['ts_date'] - stt) >= 0) & ((df1['ts_date'] - ett) <= 0)]
x = list(map(lambda it: datetime.strptime(it, '%Y-%m-%d %H:%M:%S+0800'), temp1['ts']))
y = list(temp1['p_filter'])
if row1['p'] > 0:
plt.plot(x, y, 'b')
if row1['p'] < 0:
plt.plot(x, y, 'green' )
plt.title('events')
plt.xlabel("x", fontsize=16)
plt.ylabel('y', fontsize=16)
if len(x) > 0 and len(y) > 0:
plt.text(x[0], y[0], str(yyt),ha='left', va='bottom', fontsize=9,rotation=-5,fontdict={'size': 8, 'color': 'black'})
plt.text(x[0], y[0], str(yt), ha='right', va='bottom', fontsize=9, rotation=5,fontdict={'size': 8, 'color': 'indigo'})