ser3 = Series(['USA','Mexico','Canada'],index = ['0','5','10'])
这里ranger = range(15)
我在iPython中使用Forward fill时遇到错误
ser3.reindex(ranger,method = 'ffill')
/Users/varun/anaconda/lib/python2.7/site-packages/pandas/core/index.pyc in _searchsorted_monotonic(self, label, side)
2395 return len(self) - pos
2396
-> 2397 raise ValueError('index must be monotonic increasing or decreasing')
2398
2399 def get_slice_bound(self, label, side, kind):
ValueError: index must be monotonic increasing or decreasing
正如大卫所说,这是由于索引是一个字符串。但是,为什么你得到“索引不单调错误”,答案是 - 为了重新索引方法,你的索引必须是有序/单调/递增的顺序。当你的索引是一个字符串时,它没有排序,正确的排序应该是:
ser3 = Series(['USA','Mexico','Canada'],index = ['0','10','5'])ranger = range(15)
In [100]: ser3.reindex(ranger,method = 'ffill')
Out[100]:
0 NaN
1 NaN
2 NaN
3 NaN
4 NaN
5 NaN
6 NaN
7 NaN
8 NaN
9 NaN
10 NaN
11 NaN
12 NaN
13 NaN
14 NaN
dtype: object
希望这有助于使reindex更清晰!
原始索引是字符串而不是数字。如果将原始索引更改为数字(例如,index = [0,5,10]),则它可以正常工作。
In [1]: from pandas import Series
...: ser3 = Series(['USA','Mexico','Canada'],index = [0,5,10])
...: ranger = range(15)
...: ser3.reindex(ranger,method = 'ffill')
...:
Out[1]:
0 USA
1 USA
2 USA
3 USA
4 USA
5 Mexico
6 Mexico
7 Mexico
8 Mexico
9 Mexico
10 Canada
11 Canada
12 Canada
13 Canada
14 Canada
dtype: object
也许你可以试着把ffill
放出reindex
l
ser3.reindex(ranger).ffill()