2014-10-10 68 views
0

我收到了以下代碼。IndexError:invalid slice

pd.__version__ 

hr1 = np.loadtxt("Spot_2013_Hour1.txt") 
hr2 = np.loadtxt("Spot_2013_Hour2.txt") 
hr3 = np.loadtxt("Spot_2013_Hour3.txt") 
hr4 = np.loadtxt("Spot_2013_Hour4.txt") 
hr5 = np.loadtxt("Spot_2013_Hour5.txt") 
hr6 = np.loadtxt("Spot_2013_Hour6.txt") 
hr7 = np.loadtxt("Spot_2013_Hour7.txt") 
hr8 = np.loadtxt("Spot_2013_Hour8.txt") 
hr9 = np.loadtxt("Spot_2013_Hour9.txt") 
hr10 = np.loadtxt("Spot_2013_Hour10.txt") 
hr11 = np.loadtxt("Spot_2013_Hour11.txt") 
hr12 = np.loadtxt("Spot_2013_Hour12.txt") 
hr13 = np.loadtxt("Spot_2013_Hour13.txt") 
hr14 = np.loadtxt("Spot_2013_Hour14.txt") 
hr15 = np.loadtxt("Spot_2013_Hour15.txt") 
hr16 = np.loadtxt("Spot_2013_Hour16.txt") 
hr17 = np.loadtxt("Spot_2013_Hour17.txt") 
hr18 = np.loadtxt("Spot_2013_Hour18.txt") 
hr19 = np.loadtxt("Spot_2013_Hour19.txt") 
hr20 = np.loadtxt("Spot_2013_Hour20.txt") 
hr21 = np.loadtxt("Spot_2013_Hour21.txt") 
hr22 = np.loadtxt("Spot_2013_Hour22.txt") 
hr23 = np.loadtxt("Spot_2013_Hour23.txt") 
hr24 = np.loadtxt("Spot_2013_Hour24.txt") 

# ao[0:2] 

# ao.shape 

dates = pd.date_range('2013/01/01', '2013/12/31', freq='D') 
# dates.shape 

Allhrs = Series(index=dates) 

Allhrs = DataFrame({'hr1': hr1,'hr2': hr2, 'hr3': hr3, 'hr4': hr4, 'hr5': hr5, 
'hr6': hr6, 'hr7': hr7, 'hr8': hr8, 'hr9': hr9, 'hr10': hr10, 'hr11': hr11, 
'hr12': hr12, 'hr13': hr13, 'hr14': hr14, 'hr15': hr15, 'hr16': hr16, 
'hr17': hr17, 'hr18': hr18, 'hr19': hr19, 'hr20': hr20, 'hr21': hr21, 
'hr22': hr22, 'hr23': hr23, 'hr24': hr24}) 


Allhrs.head() 
# plotting for specific time frames as seen below 

Allhrs['2013/01/01':'2013/12/31'].plot() 

這是結果:

Traceback (most recent call last): 

    File "<ipython-input-101-f4dabb5e6973>", line 52, in <module> 
    Allhrs['2013/01/01':'2013/12/31'].plot() 

    File "/Applications/anaconda/lib/python2.7/site-packages/pandas/core/frame.py", line 1674, in __getitem__ 
    return self._getitem_slice(indexer) 

    File "/Applications/anaconda/lib/python2.7/site-packages/pandas/core/frame.py", line 1701, in _getitem_slice 
    return self._slice(key, axis=0) 

    File "/Applications/anaconda/lib/python2.7/site-packages/pandas/core/generic.py", line 1136, in _slice 
    return self._constructor(self._data.get_slice(slobj, axis=axis)) 

    File "/Applications/anaconda/lib/python2.7/site-packages/pandas/core/internals.py", line 2350, in get_slice 
    new_blocks = [blk.getitem_block(slicer) for blk in self.blocks] 

    File "/Applications/anaconda/lib/python2.7/site-packages/pandas/core/internals.py", line 164, in getitem_block 
    new_values = self._slice(slicer) 

    File "/Applications/anaconda/lib/python2.7/site-packages/pandas/core/internals.py", line 148, in _slice 
    return self.values[slicer] 

IndexError: invalid slice 

可有人請告訴我如何正確定義數據幀?它說代碼行太長)如果我想爲我的數據框填充24列,我如何縮短它?

感謝

回答

2

你在片中使用一個整數,像這樣:

Allhrs[0:10] 

相反的:

Allhrs['2013/01/01':'2013/12/31'] 
+0

它的作品!謝謝你幫助Ethan。 – 2014-10-11 12:28:13

+0

@PeterKnutsen如果答案有幫助,您可以點擊箭頭下的複選標記來接受它。謝謝! – 2014-10-11 13:55:02