我有以下代码。
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
有人可以告诉我如何正确定义DataFrame吗?它说代码行太长了如果我想要数据帧的24列,我如何缩短它?
感谢
答案 0 :(得分:2)
你必须在切片中使用整数,如下所示:
Allhrs[0:10]
而不是:
Allhrs['2013/01/01':'2013/12/31']