pythonpandasappendseriespad

Appending pandas series to the left of index zero


I'm trying to select portions of a pandas data series yf according to left limit a0 and right limit b0 .

If the left limit is negative, I want to pad the difference with zeros so the resulting series would have the desired length, like this:

if a0<0: ycr = pd.Series([0]*(abs(a0))).append(yf[:b0])

but this is returning:

Series([], Name: 1, dtype: float64)

and no more information is given.


Solution

  • I created the source Series as:

    lst = np.arange(10,20)
    yf = pd.Series(lst + 5, index = lst)
    

    so that it contains:

    10    15
    11    16
    12    17
    13    18
    14    19
    15    20
    16    21
    17    22
    18    23
    19    24
    dtype: int32
    

    (the left column is the index, and the right - actual values).

    Then, to create an output Series composed of 3 zeroes and then 5 initial elements of yf I ran:

    a0 = -3; b0 = 5
    ycr = pd.Series([0]*(abs(a0))).append(yf[:b0])
    

    and got:

    0      0
    1      0
    2      0
    10    15
    11    16
    12    17
    13    18
    14    19
    dtype: int64
    

    Then I performed another test, on the source Series created with the default index (consecutive integers from 0):

    yf = pd.Series(lst + 5)
    

    This time the result is:

    0     0
    1     0
    2     0
    0    15
    1    16
    2    17
    3    18
    4    19
    dtype: int64
    

    (the only difference is in the index column, as I expected).

    So, as you can see, your code works as expected. Probably there is something wrong with your source data.