我正在将.csv读入pandas数据帧(CorpActionsDf)。其负责人是:
date factor_value reference factor
unique_id
BBG.XAMS.ASML.S 24/04/2015 0.70 Annual Regular Cash
BBG.XAMS.ASML.S 25/04/2014 0.61 Annual Regular Cash
BBG.XAMS.ASML.S 26/04/2013 0.53 Annual Regular Cash
BBG.XAMS.ASML.S 26/11/2012 9.18 None Return of Capital
BBG.XAMS.ASML.S 27/04/2012 0.46 Annual Regular Cash
然后我尝试过滤数据帧,所以我只保留两个日期之间的数据。
startDate=02-01-2008
endDate=20-02-2008
但是我收到以下错误:
TypeError: <class 'datetime.date'> type object 2008-01-02
我有另一个使用startDate和endDate来过滤信息的进程,但由于某种原因,这次我无法使过滤工作。我的代码如下:
def getCorpActionsData(rawStaticDataPath,startDate,endDate):
pattern = 'CorporateActions'+ '.csv'
staticPath = rawStaticDataPath
with open(staticPath+pattern,'rt') as f:
CorpActionsDf = pd.read_csv(f,engine='c',header=None,usecols=[0,1,2,3,4],parse_dates=[1],
dayfirst=True,index_col=[1],names=['unique_id', 'date','factor_value','reference','factor'])
print(CorpActionsDf.head())
CorpActionsDf = CorpActionsDf[(CorpActionsDf.index >= startDate) & (CorpActionsDf.index <= endDate)]
我将parse_dates设置为等于第1列,因此我不确定我做错了什么。如果有人能提供一些指导,我们将不胜感激。
非常感谢
答案 0 :(得分:1)
<强>更新强>
我猜您的索引是字符串(对象)类型 - 因为以下条件(CorpActionsDf.index >= startDate)
会给您str() >= datetime.date()
错误消息。
CorpActionsDf.index.dtype
作为输出提供什么?
OLD回答:
确保您的startDate
和endDate
具有正确的数据类型:
startDate=pd.to_datetime('02-01-2008')
endDate=pd.to_datetime('20-02-2008')
答案 1 :(得分:0)
您可以先尝试转换strings
to_datetime
,然后按此值使用索引:
import pandas as pd
import io
temp=u"""
BBG.XAMS.ASML.S,24/04/2015,0.70,Annual,Regular Cash
BBG.XAMS.ASML.S,25/04/2014,0.61,Annual,Regular Cash
BBG.XAMS.ASML.S,26/04/2013,0.53,Annual,Regular Cash
BBG.XAMS.ASML.S,26/11/2012,9.18,None,Return of Capital
BBG.XAMS.ASML.S,27/04/2012,0.46,Annual,Regular Cash
"""
#after testing replace io.StringIO(temp) to filename
CorpActionsDf = pd.read_csv(io.StringIO(temp),
header=None,
usecols=[0,1,2,3,4],
parse_dates=[1],
dayfirst=True,
index_col=[1],
names=['unique_id', 'date','factor_value','reference','factor'])
print CorpActionsDf
unique_id factor_value reference factor
date
2015-04-24 BBG.XAMS.ASML.S 0.70 Annual Regular Cash
2014-04-25 BBG.XAMS.ASML.S 0.61 Annual Regular Cash
2013-04-26 BBG.XAMS.ASML.S 0.53 Annual Regular Cash
2012-11-26 BBG.XAMS.ASML.S 9.18 None Return of Capital
2012-04-27 BBG.XAMS.ASML.S 0.46 Annual Regular Cash
startDate=pd.to_datetime('2014-04-25')
endDate=pd.to_datetime('2012-11-26')
print CorpActionsDf[startDate:endDate]
unique_id factor_value reference factor
date
2014-04-25 BBG.XAMS.ASML.S 0.61 Annual Regular Cash
2013-04-26 BBG.XAMS.ASML.S 0.53 Annual Regular Cash
2012-11-26 BBG.XAMS.ASML.S 9.18 None Return of Capital
有趣的是,如果使用strings
,则省略最后一行:
print CorpActionsDf['2014-04-25':'2012-11-26']
unique_id factor_value reference factor
date
2014-04-25 BBG.XAMS.ASML.S 0.61 Annual Regular Cash
2013-04-26 BBG.XAMS.ASML.S 0.53 Annual Regular Cash
编辑:
您必须sort_index
才能正确选择:
print CorpActionsDf
unique_id factor_value reference factor
date
2015-04-24 BBG.XAMS.ASML.S 0.70 Annual Regular Cash
2014-04-25 BBG.XAMS.ASML.S 0.61 Annual Regular Cash
2013-04-26 BBG.XAMS.ASML.S 0.53 Annual Regular Cash
2012-11-26 BBG.XAMS.ASML.S 9.18 None Return of Capital
2012-04-27 BBG.XAMS.ASML.S 0.46 Annual Regular Cash
CorpActionsDf = CorpActionsDf.sort_index()
print CorpActionsDf
date
2012-04-27 BBG.XAMS.ASML.S 0.46 Annual Regular Cash
2012-11-26 BBG.XAMS.ASML.S 9.18 None Return of Capital
2013-04-26 BBG.XAMS.ASML.S 0.53 Annual Regular Cash
2014-04-25 BBG.XAMS.ASML.S 0.61 Annual Regular Cash
2015-04-24 BBG.XAMS.ASML.S 0.70 Annual Regular Cash
print CorpActionsDf['2012-11-2':'2014-04-25']
unique_id factor_value reference factor
date
2012-11-26 BBG.XAMS.ASML.S 9.18 None Return of Capital
2013-04-26 BBG.XAMS.ASML.S 0.53 Annual Regular Cash
2014-04-25 BBG.XAMS.ASML.S 0.61 Annual Regular Cash
truncate
的另一个解决方案:
print CorpActionsDf.truncate(before='2012-11-2', after='2014-04-25')
unique_id factor_value reference factor
date
2012-11-26 BBG.XAMS.ASML.S 9.18 None Return of Capital
2013-04-26 BBG.XAMS.ASML.S 0.53 Annual Regular Cash
2014-04-25 BBG.XAMS.ASML.S 0.61 Annual Regular Cash