Search code examples
pandasdatetime-seriesreindex

How can i reindex a monthly pandas series to mins, having the values corresponding to the month in every day of the resulting days of thats months?


i have a pandas series of this form:

1950-01-31      2.0
1950-02-28      2.0
1950-03-31      1.0
1950-04-30      2.0
1950-05-31      0.0
1950-06-30      0.0

i would like to reindex passin from monthly to mins likes this:

1950-01-31 00:00:00     2.0
1950-01-31 00:01:00     2.0
1950-01-31 00:02:00     2.0
1950-02-28 00:03:00     2.0
.
.
.


1950-06-30 00:00.00     0.0
1950-06-30 00:01.00     0.0
1950-06-30 00:02.00     0.0
1950-06-30 00:03.00     0.0
.
.

i mean for example that the values corresponding to 1950-01-31 that is in this case is 2.0, it expands to every min of that month and successively

hi, thanks, i have a trouble, i need strictly that the value be in the month that belongs, for example if i use s.resample('T').ffill() i have this in my resultant serie:

2018-03-31 23:24:00    2.0
2018-03-31 23:25:00    2.0
2018-03-31 23:26:00    2.0
2018-03-31 23:27:00    2.0
                      ... 
2018-04-01 23:31:00    2.0
2018-04-01 23:32:00    2.0
2018-04-01 23:33:00    2.0
2018-04-01 23:34:00    2.0

every min of 2018-04 of that month must be 0.0 instead i have 2.0 using s.resample('T').ffill(). resuming in need a serie pasing the monthly values to mins in the respective months since the firts min to last min of that month. (Sorry for my english)


Solution

  • IIUC:

    Setup

    s = pd.Series([2, 2, 1, 2, 0, 0], pd.date_range('1950-01-31', periods=6, freq='M'))
    

    Broadcasting and reindex

    Looks as though you only capture the first 4 minutes. So I created an array of timedeltas that range from 0 to 3 minutes.

    d = np.arange(4).astype('timedelta64[m]')
    
    s.reindex((s.index.values[:, None] + d).ravel(), method='ffill')
    
    1950-01-31 00:00:00    2
    1950-01-31 00:01:00    2
    1950-01-31 00:02:00    2
    1950-01-31 00:03:00    2
    1950-02-28 00:00:00    2
    1950-02-28 00:01:00    2
    1950-02-28 00:02:00    2
    1950-02-28 00:03:00    2
    1950-03-31 00:00:00    1
    1950-03-31 00:01:00    1
    1950-03-31 00:02:00    1
    1950-03-31 00:03:00    1
    1950-04-30 00:00:00    2
    1950-04-30 00:01:00    2
    1950-04-30 00:02:00    2
    1950-04-30 00:03:00    2
    1950-05-31 00:00:00    0
    1950-05-31 00:01:00    0
    1950-05-31 00:02:00    0
    1950-05-31 00:03:00    0
    1950-06-30 00:00:00    0
    1950-06-30 00:01:00    0
    1950-06-30 00:02:00    0
    1950-06-30 00:03:00    0
    dtype: int64
    

    To capture every minute, do as @Wen suggested

    s.resample('T').ffill()