Vincent L
Vincent L

Reputation: 739

Pandas - Sum of first X hours of datetime index

I have a dataframe with a datetime index and 100 columns.

I want to have a new dataframe with the same datetime index and columns, but the values would contain the sum of the first 10 hours of each day.

So if I had an original dataframe like this:

                      A   B    C
---------------------------------
2018-01-01 00:00:00   2   5   -10
2018-01-01 01:00:00   6   5   7
2018-01-01 02:00:00   7   5   9
2018-01-01 03:00:00   9   5   6
2018-01-01 04:00:00   10  5   2
2018-01-01 05:00:00   7   5   -1
2018-01-01 06:00:00   1   5   -1
2018-01-01 07:00:00   -4  5   10
2018-01-01 08:00:00   9   5   10
2018-01-01 09:00:00   21  5   -10
2018-01-01 10:00:00   2   5   -1
2018-01-01 11:00:00   8   5   -1
2018-01-01 12:00:00   8   5   10
2018-01-01 13:00:00   8   5   9
2018-01-01 14:00:00   7   5   -10
2018-01-01 15:00:00   7   5   5
2018-01-01 16:00:00   7   5   -10
2018-01-01 17:00:00   4   5   7
2018-01-01 18:00:00   5   5   8
2018-01-01 19:00:00   2   5   8
2018-01-01 20:00:00   2   5   4
2018-01-01 21:00:00   8   5   3
2018-01-01 22:00:00   1   5   3
2018-01-01 23:00:00   1   5   1
2018-01-02 00:00:00   2   5   2
2018-01-02 01:00:00   3   5   8
2018-01-02 02:00:00   4   5   6
2018-01-02 03:00:00   5   5   6
2018-01-02 04:00:00   1   5   7
2018-01-02 05:00:00   7   5   7
2018-01-02 06:00:00   5   5   1
2018-01-02 07:00:00   2   5   2
2018-01-02 08:00:00   4   5   3
2018-01-02 09:00:00   6   5   4
2018-01-02 10:00:00   9   5   4
2018-01-02 11:00:00   11  5   5
2018-01-02 12:00:00   2   5   8
2018-01-02 13:00:00   2   5   0
2018-01-02 14:00:00   4   5   5
2018-01-02 15:00:00   5   5   4
2018-01-02 16:00:00   7   5   4
2018-01-02 17:00:00   -1  5   7
2018-01-02 18:00:00   1   5   7
2018-01-02 19:00:00   1   5   7
2018-01-02 20:00:00   5   5   7
2018-01-02 21:00:00   2   5   7
2018-01-02 22:00:00   2   5   7
2018-01-02 23:00:00   8   5   7

So for all rows with date 2018-01-01:

The value for column A would be 68 (2+6+7+9+10+7+1-4+9+21)
The value for column B would be 50 (5+5+5+5+5+5+5+5+5+5)
The value for column C would be 22 (-10+7+9+6+2-1-1+10+10-10)

So for all rows with date 2018-01-02:

The value for column A would be 39 (2+3+4+5+1+7+5+2+4+6)
The value for column B would be 50 (5+5+5+5+5+5+5+5+5+5)
The value for column C would be 46 (2+8+6+6+7+7+1+2+3+4)

The outcome would be:

                      A   B   C
---------------------------------
2018-01-01 00:00:00   68  50  22
2018-01-01 01:00:00   68  50  22
2018-01-01 02:00:00   68  50  22
2018-01-01 03:00:00   68  50  22
2018-01-01 04:00:00   68  50  22
2018-01-01 05:00:00   68  50  22
2018-01-01 06:00:00   68  50  22
2018-01-01 07:00:00   68  50  22
2018-01-01 08:00:00   68  50  22
2018-01-01 09:00:00   68  50  22
2018-01-01 10:00:00   68  50  22
2018-01-01 11:00:00   68  50  22
2018-01-01 12:00:00   68  50  22
2018-01-01 13:00:00   68  50  22
2018-01-01 14:00:00   68  50  22
2018-01-01 15:00:00   68  50  22
2018-01-01 16:00:00   68  50  22
2018-01-01 17:00:00   68  50  22
2018-01-01 18:00:00   68  50  22
2018-01-01 19:00:00   68  50  22
2018-01-01 20:00:00   68  50  22
2018-01-01 21:00:00   68  50  22
2018-01-01 22:00:00   68  50  22
2018-01-01 23:00:00   68  50  22
2018-01-02 00:00:00   39  50  46
2018-01-02 01:00:00   39  50  46
2018-01-02 02:00:00   39  50  46
2018-01-02 03:00:00   39  50  46
2018-01-02 04:00:00   39  50  46
2018-01-02 05:00:00   39  50  46
2018-01-02 06:00:00   39  50  46
2018-01-02 07:00:00   39  50  46
2018-01-02 08:00:00   39  50  46
2018-01-02 09:00:00   39  50  46
2018-01-02 10:00:00   39  50  46
2018-01-02 11:00:00   39  50  46
2018-01-02 12:00:00   39  50  46
2018-01-02 13:00:00   39  50  46
2018-01-02 14:00:00   39  50  46
2018-01-02 15:00:00   39  50  46
2018-01-02 16:00:00   39  50  46
2018-01-02 17:00:00   39  50  46
2018-01-02 18:00:00   39  50  46
2018-01-02 19:00:00   39  50  46
2018-01-02 20:00:00   39  50  46
2018-01-02 21:00:00   39  50  46
2018-01-02 22:00:00   39  50  46
2018-01-02 23:00:00   39  50  46

I figured I'd group by date first and perform a sum and then merge the results based on the date. Is there a better/faster way to do this?

Thanks.

EDIT: I worked on this answer in the mean time:

    df= df.between_time('0:00','9:00').groupby(pd.Grouper(freq='D')).sum()
    df= df.resample('1H').ffill() 

Upvotes: 2

Views: 1172

Answers (1)

Space Impact
Space Impact

Reputation: 13255

You need groupby df.index.date and use transfrom with lambda function to find sum of first 10 values as:

df.loc[:,['A','B','C']] = df.groupby(df.index.date).transform(lambda x: x[:10].sum()) 

Or if the sequence is the same for both grouped values and real columns

df.loc[:,:] = df.groupby(df.index.date).transform(lambda x: x[:10].sum())

print(df)
                      A   B   C
2018-01-01 00:00:00  68  50  22
2018-01-01 01:00:00  68  50  22
2018-01-01 02:00:00  68  50  22
2018-01-01 03:00:00  68  50  22
2018-01-01 04:00:00  68  50  22
2018-01-01 05:00:00  68  50  22
2018-01-01 06:00:00  68  50  22
2018-01-01 07:00:00  68  50  22
2018-01-01 08:00:00  68  50  22
2018-01-01 09:00:00  68  50  22
2018-01-01 10:00:00  68  50  22
2018-01-01 11:00:00  68  50  22
2018-01-01 12:00:00  68  50  22
2018-01-01 13:00:00  68  50  22
2018-01-01 14:00:00  68  50  22
2018-01-01 15:00:00  68  50  22
2018-01-01 16:00:00  68  50  22
2018-01-01 17:00:00  68  50  22
2018-01-01 18:00:00  68  50  22
2018-01-01 19:00:00  68  50  22
2018-01-01 20:00:00  68  50  22
2018-01-01 21:00:00  68  50  22
2018-01-01 22:00:00  68  50  22
2018-01-01 23:00:00  68  50  22
2018-01-02 00:00:00  39  50  46
2018-01-02 01:00:00  39  50  46
2018-01-02 02:00:00  39  50  46
2018-01-02 03:00:00  39  50  46
2018-01-02 04:00:00  39  50  46
2018-01-02 05:00:00  39  50  46
2018-01-02 06:00:00  39  50  46
2018-01-02 07:00:00  39  50  46
2018-01-02 08:00:00  39  50  46
2018-01-02 09:00:00  39  50  46
2018-01-02 10:00:00  39  50  46
2018-01-02 11:00:00  39  50  46
2018-01-02 12:00:00  39  50  46
2018-01-02 13:00:00  39  50  46
2018-01-02 14:00:00  39  50  46
2018-01-02 15:00:00  39  50  46
2018-01-02 16:00:00  39  50  46
2018-01-02 17:00:00  39  50  46
2018-01-02 18:00:00  39  50  46
2018-01-02 19:00:00  39  50  46
2018-01-02 20:00:00  39  50  46
2018-01-02 21:00:00  39  50  46
2018-01-02 22:00:00  39  50  46
2018-01-02 23:00:00  39  50  46

Upvotes: 3

Related Questions