日期时间多指标上的IndexSlice无法正常工作,但与正常工作的玩具等效似乎没什么不同

时间:2019-06-16 07:04:11

标签: python pandas

我习惯在IndexSlice索引上使用datetime。这相当于我的multindex DataFrame的玩具,您可以看到切片工作

#slicing works on a simple DateTime index
qf = pd.DataFrame(index=pd.date_range(start="1Jan2019",freq="d",periods=30))
qf.loc[idx['2019-1-15':None]]   #works


#the same slicing works on a multindex
qf.reset_index(inplace=True)
qf['foo']="bar"
qf['other']=range(len(qf))
qf['filler']="egbdf"
qf.set_index(['index','foo', 'other'], inplace=True)

qf.loc[idx['2019-1-15':'2019-1-20',:,:],:] #wrks    
qf.loc[idx['2019-1-15':None,'bar',:],:]   #works

但是我的真实DataFrame发生了某些事情。我看不出有什么区别。

xf.loc[idx['2019-5-1':'2019-6-1',"squat",:],:]     # This works ok
xf.loc[idx['2019-5-1':None,"squat",:],:]           # This fails

'2019-5-1':None切片时出现的错误是

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-280-b0dce8e9e337> in <module>
      1 xf.loc[idx['2019-5-1':'2019-6-1',"squat",:],:]     # This works ok
----> 2 xf.loc[idx['2019-5-1':None,"squat",:],:]           # This fails
      3 #xf

C:\ProgramData\Anaconda3\envs\nambu\lib\site-packages\pandas\core\indexing.py in __getitem__(self, key)
   1492             except (KeyError, IndexError, AttributeError):
   1493                 pass
-> 1494             return self._getitem_tuple(key)
   1495         else:
   1496             # we by definition only have the 0th axis

C:\ProgramData\Anaconda3\envs\nambu\lib\site-packages\pandas\core\indexing.py in _getitem_tuple(self, tup)
    866     def _getitem_tuple(self, tup):
    867         try:
--> 868             return self._getitem_lowerdim(tup)
    869         except IndexingError:
    870             pass

C:\ProgramData\Anaconda3\envs\nambu\lib\site-packages\pandas\core\indexing.py in _getitem_lowerdim(self, tup)
    967         # we may have a nested tuples indexer here
    968         if self._is_nested_tuple_indexer(tup):
--> 969             return self._getitem_nested_tuple(tup)
    970 
    971         # we maybe be using a tuple to represent multiple dimensions here

C:\ProgramData\Anaconda3\envs\nambu\lib\site-packages\pandas\core\indexing.py in _getitem_nested_tuple(self, tup)
   1046 
   1047             current_ndim = obj.ndim
-> 1048             obj = getattr(obj, self.name)._getitem_axis(key, axis=axis)
   1049             axis += 1
   1050 

C:\ProgramData\Anaconda3\envs\nambu\lib\site-packages\pandas\core\indexing.py in _getitem_axis(self, key, axis)
   1904             # nested tuple slicing
   1905             if is_nested_tuple(key, labels):
-> 1906                 locs = labels.get_locs(key)
   1907                 indexer = [slice(None)] * self.ndim
   1908                 indexer[axis] = locs

C:\ProgramData\Anaconda3\envs\nambu\lib\site-packages\pandas\core\indexes\multi.py in get_locs(self, seq)
   2774                 # a slice, include BOTH of the labels
   2775                 indexer = _update_indexer(_convert_to_indexer(
-> 2776                     self._get_level_indexer(k, level=i, indexer=indexer)),
   2777                     indexer=indexer)
   2778             else:

C:\ProgramData\Anaconda3\envs\nambu\lib\site-packages\pandas\core\indexes\multi.py in _get_level_indexer(self, key, level, indexer)
   2635                 # note that the stop ALREADY includes the stopped point (if
   2636                 # it was a string sliced)
-> 2637                 return convert_indexer(start.start, stop.stop, step)
   2638 
   2639             elif level > 0 or self.lexsort_depth == 0 or step is not None:

AttributeError: 'int' object has no attribute 'stop'

我看不到玩具索引和真实索引之间的任何实质性区别,也看不到错误消息如何导致将None传递给切片器。

enter image description here

enter image description here

================================================ =========

在不同的示例中,我发现了为什么它起作用/不起作用。

当索引完全为dates时,代码可以正常工作。但是,如果索引中包含datetimes,它将失败。

#this index is solely dates,  not dateTimes, and everything works
dt_index = pd.date_range(start="1jan2019",periods=100,freq="d")
zf = pd.DataFrame(index=dt_index)
zf['foo']=10
zf['bar']="squat"
zf['zaa']=range(len(dt_index))
zf.index.name="date"
zf = zf.reset_index().set_index(["date", "bar", "zaa"])


zf.loc[idx['2019-1-1':'2019-1-3',"squat",:],:]     # This works ok
zf.loc[idx['2019-1-1':,"squat",:],:]     # This works
zf.loc[idx['2019-1-1':None,'squat',:,:],:]           # This works

失败的示例:

dt_index = pd.date_range(start="1jan2019 00:15:33",periods=100,freq="h")
zf = pd.DataFrame(index=dt_index)
zf['foo']=10
zf['bar']="squat"
zf['zaa']=range(len(dt_index))
zf.index.name="date"
zf = zf.reset_index().set_index(["date", "bar", "zaa"])


zf.loc[idx['2019-1-1':'2019-1-3',"squat",:],:]     # This works ok
#zf.loc[idx['2019-1-1':,"squat",:],:]     # This fails  AttributeError: 'int' object has no attribute 'stop'
#zf.loc[idx['2019-1-1':None,'squat',:,:],:]           # AttributeError: 'int' object has no attribute 'stop'

1 个答案:

答案 0 :(得分:3)

这似乎是一个错误。根据{{​​3}},检查pandas软件包multi.py的第2614-2637行:

            try:
                if key.start is not None:
                    start = level_index.get_loc(key.start)
                else:
                    start = 0
                if key.stop is not None:
                    stop = level_index.get_loc(key.stop)
                else:
                    stop = len(level_index) - 1
                step = key.step
            except KeyError:

                # we have a partial slice (like looking up a partial date
                # string)
                start = stop = level_index.slice_indexer(key.start, key.stop,
                                                         key.step, kind='loc')
                step = start.step

            if isinstance(start, slice) or isinstance(stop, slice):
                # we have a slice for start and/or stop
                # a partial date slicer on a DatetimeIndex generates a slice
                # note that the stop ALREADY includes the stopped point (if
                # it was a string sliced)
                return convert_indexer(start.start, stop.stop, step)

停止 将始终为int,因为端点为None。但是qfxf 开始 不同。 qf的datetime_index的分辨率为1day,而qf.index.levels[0].get_loc('2019-01-17')将为'int'。但是xf的分辨率为0.001S,而xf.index.levels[0].get_loc('2019-01-17')将为slice,这将导致调用stop.stop,而 停止 int

作为解决方法,您可以使用很大的日期而不是None

xf.loc[idx['2019-5-1':'2222',"squat",:],:]