Pab*_* V. 5 python datetime numpy
我正在尝试从某些文件加载时间序列数据。数据有这种格式
04/02/2015 19:07:53.951,3195,1751,-44,-25
Run Code Online (Sandbox Code Playgroud)
我正在使用此代码将整个文件作为 numpy 对象加载。
content = np.loadtxt(filename, dtype={'names': ('timestamp', 'tick', 'ch', 'NodeI', 'Base'),
'formats': ('datetime64[us]', 'i4', 'i4', 'i4', 'i4')}, delimiter=',', skiprows=27)
Run Code Online (Sandbox Code Playgroud)
但我的日期时间格式出错
ValueError: Error parsing datetime string "04/02/2015 19:07:53.951" at position 2
Run Code Online (Sandbox Code Playgroud)
有一种简单的方法可以定义我正在阅读的日期时间格式吗?有包含大量数据的文件,所以我尽量不要多次遍历文件。
使用converters参数将转换器函数应用于第一列的数据:
import datetime
def parsetime(v):
return np.datetime64(
datetime.datetime.strptime(v, '%d/%m/%Y %H:%M:%S.%f')
)
content = np.loadtxt(
filename,
dtype={
'names': ('timestamp', 'tick', 'ch', 'NodeI', 'Base'),
'formats': ('datetime64[us]', 'i4', 'i4', 'i4', 'i4')
},
delimiter=',',
skiprows=27,
converters={0: parsetime},
)
Run Code Online (Sandbox Code Playgroud)
我假设您的数据文件正在使用D/M/Y,如果您正在使用,请相应地调整格式字符串M/D/Y。
| 归档时间: |
|
| 查看次数: |
2797 次 |
| 最近记录: |