Re-fetch pandas numerical index series

Suppose I have a pandas.Series with an index with a numeric type value, for example.

pd.Series( [10,20], [1.1, 2.3] ) 

How do we rewrite the series above with an interval of 0.1? Does it look like .resample func only works with datetime interval?

+3
source share
3 answers

This is called the name of the interpolation. You can think of re-sampling as a special case of interpolation.

 In [24]: new_idx = s.index + pd.Index(np.arange(1.1, 2.3, .01)) In [25]: s.reindex(new_idx).interpolate().head() Out[25]: 1.10 10.000000 1.11 10.083333 1.12 10.166667 1.13 10.250000 1.14 10.333333 dtype: float64 In [26]: s.reindex(new_idx).interpolate().tail() Out[26]: 2.26 19.666667 2.27 19.750000 2.28 19.833333 2.29 19.916667 2.30 20.000000 dtype: float64 

We need new_idx be the union of the original index and the values ​​that we want to interpolate so that the original index is not discarded.

Take a look at the interpolation methods: http://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.interpolate.html

+3
source

Well, I don’t think you might have an integer index due to float comparison operations. p .1, however, you could do something like:

  • create a new df = pd.DataFrame(index=range(100, 201)) [the one that will now represent .1]
  • set the values ​​to 100 (initially 10) and 200 (initially 20) - 1.1 and 2.3
  • df.fillna(method='pad', inplace=True)

also it seems you don’t even need to use the index at all, you just need the gaps between the data ...

0
source

One option is to use cut to bin this data (much less elegant than reselecting, but here):

 In [11]: cat, retbins = pd.cut(s.index, np.arange(1, 3, 0.1), retbins=True) In [12]: s.index = retbins[cat.labels] In [13]: s Out[13]: 1.0 10 2.2 20 dtype: int64 

Say if you want to reprofile like = 'sum':

 In [14]: s = s.groupby(s.index).sum() In [15]: s = s.reindex(retbins) 

There are many NaNs now, now, as Tom says, you can interpolate:

 In [16]: s.interpolate() 
0
source

All Articles