首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >TypeError:不可排序的类型: str() >= datetime.date()

TypeError:不可排序的类型: str() >= datetime.date()
EN

Stack Overflow用户
提问于 2016-04-24 00:49:36
回答 2查看 5.7K关注 0票数 3

我正在将.csv读入熊猫数据帧(CorpActionsDf)。它的负责人是:

代码语言:javascript
复制
                       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

然后,我尝试过滤数据帧,以便只保留两个日期之间的数据。

代码语言:javascript
复制
startDate=02-01-2008
endDate=20-02-2008

但我得到以下错误:

代码语言:javascript
复制
TypeError: <class 'datetime.date'> type object 2008-01-02

我还有另一个进程,它使用startDate和endDate成功地过滤信息,但是由于某些原因,这一次我不能让过滤工作。我的代码如下:

代码语言:javascript
复制
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设置为等于第一列,所以我不确定我做错了什么。如果有人能提供一些指导,我们将不胜感激。

非常感谢

EN

回答 2

Stack Overflow用户

发布于 2016-04-24 00:55:20

更新:

我猜你的索引是string (object)类型的-因为下面的条件(CorpActionsDf.index >= startDate)会给你str() >= datetime.date()错误信息。

CorpActionsDf.index.dtype提供了什么作为输出?

的老答案:

确保您的startDateendDate具有正确的数据类型:

代码语言:javascript
复制
startDate=pd.to_datetime('02-01-2008')
endDate=pd.to_datetime('20-02-2008')
票数 2
EN

Stack Overflow用户

发布于 2016-04-24 01:00:05

您可以先尝试转换strings to_datetime,然后按以下值使用索引:

代码语言:javascript
复制
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,最后一行将被省略:

代码语言:javascript
复制
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才能正确选择:

代码语言:javascript
复制
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的解决方案

代码语言:javascript
复制
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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36813653

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档