时间序列处理

在处理时间序列时,由于秒级别的数据重复率非常高,所以想将秒数据的秒处理掉,只保留分精确度。
data['location_time'][1].strftime('%Y-%m-%d %H:%M')
遇到这种情况:
AttributeError: 'str' object has no attribute 'strftime'
stackoverflow中查询到You should use datetime object, not str.
所以这里我们需要将str转换为datetime类型
d= datetime.strptime(data['location_time'][1],'%Y/%m/%d %H:%M')这里是我的尝试。
但是无法对datetime对象无法去除后两位,所以需要将datetime转化为str
d.strftime('%Y/%m/%d %H:%M')

最后可以整合为

from datetime import datetime 
data_time = []
pattern = '%Y/%m/%d %H:%M'
for x in data['location_time']:
    d= datetime.strptime(x,pattern)
    d.strftime(pattern)
    data_time.append(d)
data['location_time'] = data_time

产生的结果:
datetime精确到分

后面又想到,经纬度小数点后两位还保持一致,那么认为这个误差应该是可以忽略的。这个时候就想做这样的处理:如果经纬度直到小数点后两位还一致,那么处理之后再去重

formator = '{0:.02f}'.format
data.iloc[:,3:5] = data.iloc[:,3:5].applymap(formator)

另一种方式
这里直观的可以看到,就实现了上述的效果。

参考链接:
1.Python中datetime时间戳精确到单位的用法
2.stackoverflow相关页
3.python 时间格式datetime、str与date的相互转换

猜你喜欢

转载自blog.csdn.net/LawenceRay/article/details/88812008