我正在将.csv读入熊猫数据帧(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设置为等于第一列,所以我不确定我做错了什么。如果有人能提供一些指导,我们将不胜感激。
非常感谢
发布于 2016-04-24 00:55:20
更新:
我猜你的索引是string (object)类型的-因为下面的条件(CorpActionsDf.index >= startDate)会给你str() >= datetime.date()错误信息。
CorpActionsDf.index.dtype提供了什么作为输出?
的老答案:
确保您的startDate和endDate具有正确的数据类型:
startDate=pd.to_datetime('02-01-2008')
endDate=pd.to_datetime('20-02-2008')发布于 2016-04-24 01:00:05
您可以先尝试转换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 Cashhttps://stackoverflow.com/questions/36813653
复制相似问题