Is there a better way to fill missing index of a series with chaining without using pipe?

Viewed 72

Let's say I have a dataframe, and after some operations I arrive at an intermediate series:

>>> df.groupby(...).some_operation()
1    1
3    2
5    1
7    4
8    5
dtype: int64

Now, I want to convert the index into a RangeIndex with start=1, stop=9, step=1, like this:

1    1.0
2    NaN
3    2.0
4    NaN
5    1.0
6    NaN
7    4.0
8    5.0
dtype: float64

One way it can be done is:

>>> s = df.groupby(...).some_operation()
>>> s.reindex(range(s.index.min(), s.index.max()+1))

But I don't want to store the intermediate series. Another way would be:

>>> ( df.groupby(...).some_operation()
        .pipe(lambda x: x.reindex(range(x.index.min(), x.index.max()+1))
    )

This works, but wondering if there is something better, something like interpolate for index or asfreq, but for RangeIndex. reindex could have been an option, but it doesn't support functions, even if it did, it would be clunky. It seems like there must be a method, because it may be a common thing to do, either I am unaware of such method, or can't think of it, if it exists.

It can be assumed that df.index is completely different (let's say datetime index) and unlikely to be helpful. One can reindex the series by a range object with arbitrarily large number and drop the ending nans, but that would be quite inefficient.

For example:

A = np.array([ 3, 15, 12, 14, 15,  1, 14, 18, 11, 16, 10, 12, 14, 15, 10, 13, 12,
       11,  6, 13])
B = np.array([1.1 , 1.09, 0.8 , 0.71, 0.37, 0.93, 0.9 , 0.54, 1.29, 0.33, 1.29,
       0.39, 0.69, 0.89, 0.89, 0.46, 1.12, 0.29, 0.61, 0.81])
df = pd.DataFrame({'A': A, 'B': B})

# This gives:
>>> df.groupby(df['B'].ge(1).cumsum()).size().value_counts(sort=False)
1    1
2    1
4    1
6    1
7    1
dtype: int64

How do I make it like below, in a better way than above mentioned methods:

1    1.0
2    1.0
3    NaN
4    1.0
5    NaN
6    1.0
7    1.0
dtype: float64
0 Answers
Related