【问题标题】:TypeError: unhashable type: 'slice'TypeError:不可散列的类型:'sl​​ice'
【发布时间】:2020-06-25 06:42:21
【问题描述】:

我正在尝试使用以下数据框 dfMyRoll 运行回归,数据框的头部如下所示:

                SCORE  SCORE_LAG
date                           
2007-10-29 -0.031551        NaN
2007-10-30  0.000100  -0.031551
2007-10-31  0.000100   0.000100
2007-11-01  0.000100   0.000100
2007-11-02  0.000100   0.000100 

我使用的代码是:

import glob
import pandas as pd 
import os.path
import scipy
from scipy.stats import linregress

def main():
        
    dataPath = "C:/Users/Stacey/Documents/data/Roll"
    roll = 4
    1ID = "BBG.XNGS.AAPL.S"
    2ID = "BBG.XNGS.AMAT.S"
    print(1ID,1ID)
    cointergration = getCointergration(dataPath,1ID,2ID,roll)
    
    return

def getCointergration(dataPath,1ID,2ID,roll):
   
    for myRoll in range((roll-4),roll,1):
        path = dataPath+str(myRoll)+'/'
        filename='PairData_'+1ID+'_'+2ID+'.csv'
        
        for fname in glob.iglob(path+filename):
            
            dfMyRoll = pd.read_csv(fname, header=0, usecols=[0,31],parse_dates=[0], dayfirst=True,index_col=[0], names=['date', 'SCORE'])
            dfMyRoll['SCORE_LAG'] = dfMyRoll['SCORE'].shift(1)
            print('cointergration',dfMyRoll.head())
         
            X = dfMyRoll[1:,'SCORE']
            
            Y = dfMyRoll[1:,'SCORE_LAG']
          slope,intercept,_,_,stderr=linregress(dfMyRoll[1:,'SCORE'],dfMyRoll[1:,'SCORE_LAG'])

if __name__ == "__main__":
    
    print ("CointergrationTest...19/05/17")

    try:
        
        main()

    except KeyboardInterrupt:
        
        print ("Ctrl+C pressed. Stopping...")  

  

我收到错误:TypeError: unhashable type: 'slice'。我查看了有关此主题的先前帖子,并尝试通过以下方式将 iloc 添加到 X 和 Y 时间序列:

        X = dfMyRoll.iloc[1:,'SCORE']
        
        Y = dfMyRoll.iloc[1:,'SCORE_LAG']

但不幸的是,我似乎找不到解决方案。请参阅下面的堆栈跟踪:

Traceback (most recent call last):

  File "<ipython-input-3-431422978139>", line 1, in <module>
    runfile('C:/Users/Stacey/Documents/scripts/cointergrationTest.py', wdir='C:/Users/Stacey/Documents/scripts')

  File "C:\Anaconda\lib\site-packages\spyder\utils\site\sitecustomize.py", line 866, in runfile
    execfile(filename, namespace)

  File "C:\Anaconda\lib\site-packages\spyder\utils\site\sitecustomize.py", line 102, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)

  File "C:/Users/Stacey/Documents/scripts/cointergrationTest.py", line 64, in <module>
    main()

  File "C:/Users/Stacey/Documents/scripts/cointergrationTest.py", line 23, in main
    cointergration = getCointergration(dataPath,1ID,2ID,roll)

  File "C:/Users/Stacey/Documents/scripts/cointergrationTest.py", line 42, in getCointergration
    X = dfMyRoll[1:,'SCORE']

  File "C:\Anaconda\lib\site-packages\pandas\core\frame.py", line 2059, in __getitem__
    return self._getitem_column(key)

  File "C:\Anaconda\lib\site-packages\pandas\core\frame.py", line 2066, in _getitem_column
    return self._get_item_cache(key)

  File "C:\Anaconda\lib\site-packages\pandas\core\generic.py", line 1384, in _get_item_cache
    res = cache.get(item)

TypeError: unhashable type: 'slice'

【问题讨论】:

  • 这不是正确的索引语法。 dfMyRoll[1:,'SCORE'] 实际上应该是 dfMyRoll.loc[1:,'SCORE'] 假设你有一个整数范围索引。

标签: python pandas


【解决方案1】:

你需要使用 loc 而不是 iloc:

X = dfMyRoll.loc[1:,'SCORE']

Y = dfMyRoll.loc[1:,'SCORE_LAG']

iloc 被读作“整数位置”,并且只接受整数位置。 loc 稍微宽容一些,允许两者兼有(你也可以使用ix)。

【讨论】:

  • 谢谢,我有一个日期时间索引 这意味着我收到一个新错误:TypeError: cannot do slice indexing on 的这些索引器 [1]。你能告诉我如何使用日期时间进行 iloc 吗?
  • @Stacey 请尝试使用 ix,看起来这种行为可能已经改变......嗯。
  • @Stacey 即dfMyRoll.ix[1:,'SCORE']
猜你喜欢
  • 1970-01-01
  • 2019-05-23
  • 1970-01-01
  • 2021-08-15
  • 2016-05-13
  • 2016-03-16
  • 1970-01-01
  • 2012-10-27
  • 2019-01-29
相关资源
最近更新 更多