我的列格式如下:
01JAN2014:08:22:00
01JAN2014:09:01:00
01JAN2014:10:00:00
01JAN2014:10:32:00
Tableau无法读取此内容,因为我认为它的日期格式错误。我试过在画面中转换它但它仍然不起作用。所以我想通过解析月,日,年和时间戳来重新创建列。然后将列加入%Y-%m-%d %H:%M:%S
格式,Tableau和其他东西可以读取。
我试过在python中运行它
import pandas as pd
dates = pd.read_csv(filelocation)['ENTERED_TIME']
from datetime import datetime
for date in dates:
d = datetime.strptime(date,'%d-%m-%Y:%H:%M:%S')
但它不起作用,因为它说明如下:
time data '01JAN2014:08:22:00' does not match format '%d-%m-%Y:%H:%M:%S'
答案 0 :(得分:0)
试试这个:
In [1]: from datetime import datetime
In [2]: d = "01JAN2014:08:22:00"
In [3]: d_ = datetime.strptime(d, "%d%b%Y:%H:%M:%S")
In [4]: d_
Out[4]: datetime.datetime(2014, 1, 1, 8, 22)
在pandas
:
import pandas as pd
from datetime import datetime
s = ["01JAN2014:08:22:00",
"01JAN2014:09:01:00",
"01JAN2014:10:00:00",
"01JAN2014:10:32:00"]
s_ = pd.Series(s)
print s_.apply(lambda x:datetime.strptime(x,"%d%b%Y:%H:%M:%S"))
pandas
中的输出:
0 2014-01-01 08:22:00
1 2014-01-01 09:01:00
2 2014-01-01 10:00:00
3 2014-01-01 10:32:00
dtype: datetime64[ns]
[Finished in 0.5s]
答案 1 :(得分:0)
您可以在读取输入文件时解析日期列:
from datetime import datetime
import pandas as pd
series = pd.read_csv(filelocation, parse_dates=['ENTERED_TIME'],
date_parser=lambda s: datetime.strptime(s, "%d%b%Y:%H:%M:%S"))
请参阅read_csv() docs。