read_csv 似乎不起作用。为什么呢?
这不起作用:
from pandas import read_csv
from StringIO import StringIO
data = """Data,Sample\n9-Oct-14,Sample 1\n,Sample 2\n"""
read_csv(StringIO(data), parse_dates=True)
Out[19]:
Data Sample
0 9-Oct-14 Sample 1
1 NaN Sample 2
这有效:
read_csv(StringIO(data), parse_dates=True, index_col=0)
Out[20]:
Sample
Data
2014-10-09 Sample 1
NaT Sample 2
答案 0 :(得分:0)
在Mac OS X上使用Python 3.4.1以及稍微改编的代码,我使用提供的数据流或代码片段获得完全相同的输出。
CODE:
from pandas import read_csv
import io
data1 = """Data,Sample\n09-Oct-14,Sample 1\n,Sample 2\n"""
data2 = """Data\tSample\n01-AUG-09\tSample 1\n\tSample 2\n"""
df1 = read_csv(io.StringIO(data1),parse_dates=True, index_col=0)
df2 = read_csv(io.StringIO(data2),sep='\t',parse_dates=True, index_col=0)
print(df1)
print('\n')
print(df2)
输出:
Sample
Data
2014-10-09 Sample 1
NaT Sample 2
Sample
Data
2009-08-01 Sample 1
NaT Sample 2
答案 1 :(得分:0)
知道了。
read_csv(StringIO(data),parse_dates=['Data'])
Out[27]:
Data Sample
0 2014-10-09 Sample 1
1 NaT Sample 2