Pandas重新索引和填充缺失值:"索引必须单调递增"

3

在回答 这个stackoverflow问题 时,我发现在重新索引数据框时使用fill方法会出现一些有趣的行为。

pandas中的 旧bug报告 表示df.reindex(newIndex,method='ffill')应该等同于df.reindex(newIndex).ffill(),但我目睹的情况并非如此。

下面是一个代码片段,用于说明这种行为

df = pd.DataFrame({'values': 2}, index=pd.DatetimeIndex(['2016-06-02', '2016-05-04', '2016-06-03']))
newIndex = pd.DatetimeIndex(['2016-05-04', '2016-06-01', '2016-06-02', '2016-06-03', '2016-06-05'])
print(df.reindex(newIndex).ffill())
print(df.reindex(newIndex, method='ffill'))

第一个打印语句按预期工作。第二个引发错误。
ValueError: index must be monotonic increasing or decreasing

这里发生了什么?

编辑:请注意,示例df有意具有非单调索引。问题涉及在df.reindex(newIndex, method='ffil')中的操作顺序。我的期望是,它应该按照错误报告所说的方式工作- 首先重新索引新的索引,然后填充。

正如您所看到的,newIndex.is_monotonicTrue,并且在单独调用填充时可以工作,但在作为reindex参数调用时失败。

2个回答

3
似乎这也需要在列上完成。
In[76]: frame = DataFrame(np.arange(9).reshape((3, 3)), index=['a', 'c', 'd'],columns=['Ohio', 'Texas', 'California'])

In[77]: frame.reindex(index=['a','b','c','d'],method='ffill',columns=states)
---> ValueError: index must be monotonic increasing or decreasing

In[78]: frame.reindex(index=['a','b','c','d'],method='ffill',columns=states.sort())

Out[78]:
  Ohio  Texas  California
a     0      1           2
b     0      1           2
c     3      4           5
d     6      7           8


这个问题是由于未排序的列所致。因此,我认为这个答案更准确。 - Junyong Yao

3

reindex的某些元素需要对输入的索引进行排序。我推断当传递method参数时,它未能预先对输入的索引进行排序,因此失败了。我得出这个结论是基于以下事实:这样可以正常工作:

print df.sort_index().reindex(newIndex.sort_values(), method='ffill')

这确实是问题的堆栈跟踪:1940 indexer = self.get_indexer(target) 1941 nonexact =(indexer == -1)-> 1942 indexer [nonexact] = self._searchsorted_monotonic(target [nonexact],side)1943如果侧面== 'left':1944#searchsorted返回“索引到排序数组的指数,使得,表明索引必须按顺序排序才能正常工作,这是有道理的,因为如果索引未排序,则无法进行“ffill”。 - EdChum
@piRSquared 昨晚我写这个问题时已经很晚了。请注意,all(newIndex.sort_values()==newIndex)True。让你的片段工作的一部分是 df.sort_index() 调用。我的示例 df 故意具有非单调索引。我期望 reindex(newIndex, method='ffill') 的执行顺序是先重新索引,然后再填充,而不是反过来。 - michael_j_ward
@EdChum,我同意在非单调索引上使用ffill没有意义。但是我的newIndex是单调的。我对df.reindex(newIndex, method='ffill')的期望是首先使用newIndex重新索引,然后再填充。但显然这不是正在发生的事情。 - michael_j_ward

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接