您需要:
CSV的無頭:
import pandas as pd
from pandas.compat import StringIO
temp=u"""
20140804:10:00:13.281486,782.83,443355
20140804:10:00:13.400113,955.71,348603"""
#after testing replace 'StringIO(temp)' to 'filename.csv'
df = pd.read_csv(StringIO(temp),
#parse first columns
parse_dates=[0],
#custom parse function
date_parser = lambda x: pd.datetime.strptime(x, '%Y%m%d:%H:%M:%S.%f'),
#no header of csv
header=None)
print (df)
0 1 2
0 2014-08-04 10:00:13.281486 782.83 443355
1 2014-08-04 10:00:13.400113 955.71 348603
print (df.dtypes)
0 datetime64[ns]
1 float64
2 int64
dtype: object
CSV
import pandas as pd
from pandas.compat import StringIO
temp=u"""dates,a,b
20140804:10:00:13.281486,782.83,443355
20140804:10:00:13.400113,955.71,348603"""
#after testing replace 'StringIO(temp)' to 'filename.csv'
df = pd.read_csv(StringIO(temp),
parse_dates=[0],
date_parser = lambda x: pd.datetime.strptime(x, '%Y%m%d:%H:%M:%S.%f'))
print (df)
dates a b
0 2014-08-04 10:00:13.281486 782.83 443355
1 2014-08-04 10:00:13.400113 955.71 348603
print (df.dtypes)
dates datetime64[ns]
a float64
b int64
dtype: object
可以更新你的問題的標題?因爲評論的格式有問題。 – jezrael
感謝您的幫助!我現在有一個如上所示的表單中的數據,但以Timestamp作爲索引。我想將每列,(asset_price)和b(units_traded)繪製爲時間序列,因此時間戳記在x軸上。我試圖使用DataFrame.plot(),但我得到的時間戳遠遠超出了我的數據,所以它沒有任何意義;我如何將上述數據作爲時間序列來繪製? – sjc725
難以回答的問題,但似乎matplotlib管理它,並創建索引值的最小值和最大值之間的'axis'值,可能有助於將索引值轉換爲字符串''df.index = df.index.strftime('%Y - %m-%d')'然後調用'df.plot()' – jezrael