Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.5k views
in Technique[技术] by (71.8m points)

numpy - ValueError: index must be monotonic increasing or decreasing

ser3 = Series(['USA','Mexico','Canada'],index = ['0','5','10'])

here ranger = range(15)

I get an error while using Forward fill in iPython

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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

As David said it was due to index being a string. But why were you getting the "Index not monotonic Error" and the answer to that is - For reindexing methods to work, your index must be in sorted/monotonic/increasing order. And when your index was a string, it wasn't sorted, correct sorting should have been:

ser3 = Series(['USA','Mexico','Canada'],index = ['0','10','5']) ranger = range(15)

Note: ranger being an integer sequence while index is string sequence, the method isn't going to do much but reindex will work

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

Hope this helps and makes reindex clearer !!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...