我有微秒的csv文件作为时间。
Time,Bid
2014-03-03 23:30:30:224323224323,0.8925
2014-03-03 23:30:30:224390224390,0.892525
2014-03-03 23:30:30:224408224408,0.892525
2014-03-03 23:30:30:364299364299,0.892525
如何使用read_csv()或其他函数将微秒解析为时间索引 read_json可能吗?
谢谢!
答案 0 :(得分:4)
继@ Jeff的评论后,您可以执行以下操作:
In [29]:
import pandas as pd
# specifically set the Time column to object dtype
df = pd.read_csv(r'c:\data\temp1.txt', dtype={'Time':object})
df
Out[29]:
Time Bid
0 2014-03-03 23:30:30:224323224323 0.892500
1 2014-03-03 23:30:30:224390224390 0.892525
2 2014-03-03 23:30:30:224408224408 0.892525
3 2014-03-03 23:30:30:364299364299 0.892525
[4 rows x 2 columns]
In [32]:
# trim the erroneous data
df.Time=df.Time.apply(lambda x: x[:-6])
# now apply to_datetime and pass the format string
df.Time = pd.to_datetime(df.Time, format='%Y-%m-%d %H:%M:%S:%f')
df.dtypes
Out[32]:
Time datetime64[ns]
Bid float64
dtype: object