在Python中格式化时间戳

时间:2018-04-14 14:32:03

标签: python numpy

我收到格式错误..

我的代码:

20180406    00:48.9 1.22394 1.22417
20180406    00:48.9 1.22394 1.22417
20180406    00:53.3 1.2239  1.22421
20180406    00:54.6 1.22391 0
20180406    01:51.8 0       1.2241
20180406    02:19.4 1.22396 1.22404
20180406    02:49.8 1.22391 1.22399

错误:

  

ValueError:时间数据'20180406,00:48.9,1.22394,1.22417 ,,“'与格式'%Y%m%d%H%M%S'不匹配

csv文件中的数据显示为(四列):

mixedorder

如何从时间戳中删除冒号和句号?

1 个答案:

答案 0 :(得分:0)

numpy对许多事情都有好处。但对于混合类型数据pandas通常更方便。

这是使用datetime将数据转换为pandas的一种方式。

import pandas as pd
from io import StringIO

mystr = StringIO("""20180406    00:48.9 1.22394 1.22417
20180406    00:48.9 1.22394 1.22417
20180406    00:53.3 1.2239  1.22421
20180406    00:54.6 1.22391 0
20180406    01:51.8 0       1.2241
20180406    02:19.4 1.22396 1.22404
20180406    02:49.8 1.22391 1.22399""")

# replace mystr with 'file.csv'
df = pd.read_csv(mystr, delim_whitespace=True, header=None,
                 names=['Date', 'Time', 'Bid', 'Ask'])

# create datetime  column
df['DateTime'] = pd.to_datetime(df['Date'].map(str) + ' ' + df['Time'])

print(df)

#        Date     Time      Bid      Ask            DateTime
# 0  20180406  00:48.9  1.22394  1.22417 2018-04-06 00:48:53
# 1  20180406  00:48.9  1.22394  1.22417 2018-04-06 00:48:53
# 2  20180406  00:53.3  1.22390  1.22421 2018-04-06 00:53:17
# 3  20180406  00:54.6  1.22391  0.00000 2018-04-06 00:54:36
# 4  20180406  01:51.8  0.00000  1.22410 2018-04-06 01:51:47
# 5  20180406  02:19.4  1.22396  1.22404 2018-04-06 02:19:23
# 6  20180406  02:49.8  1.22391  1.22399 2018-04-06 02:49:47

结果中的数据类型:

print(df.dtypes)

# Date                 int64
# Time                object
# Bid                float64
# Ask                float64
# DateTime    datetime64[ns]
# dtype: object