'Can I use itertools.count to add values in a column, resetting at a certain point?

I'm trying to create a list of timestamps from a column in a dataframe, that resets after a certain time to zero. So, if the limit was 4, I want the count to add up the values of the column up to position 4, and then reset to zero, and continue adding the values of the column, from position 5, and so forth until it reaches the length of the column. I used itertools.islice earlier in the script to create a counter, so I was wondering if I could use a combination of this and itertools.count to do something similar? So far, this is my code:

cycle_time = list(itertools.islice(itertools.count(0,raw_data['Total Time (s)'][lens]),range(0, block_cycles),lens))

Where raw_data['Total Time (s)'] contains the values I wish to add up, block_cycles is the number I want to add up to in the dataframe column before resetting, and lens is the length of the column in the dataframe. Ideally, the output from my list would look like this:

print(cycle_time)
0
0.24
0.36
0.57
0
0.13
0.32
0.57

Which is calculated from this input:

print(raw_data['Total Time (s)'])
0
0.24
0.36
0.57
0.7
0.89
1.14

Which I would then append to a new column in a dataframe, interim_data_output['Cycle time (s)'] which details the time elapsed at that point in the 'cycle'. block_cycles is the number of iterations in each large 'cycle' This is what I would do with the list:

interim_data_output['Cycle time (s)'] = cycle_time

I'm a bit lost here, is this even possible using these methods? I'd like to use itertools for performance reasons. Any help would be greatly appreciated!



Solution 1:[1]

Given the discussion in the comments, here is an example:

df = pd.DataFrame({'Total Time (s)':[0, 0.24, 0.36, 0.57, 0.7, 0.89, 1.14]})
   Total Time (s)
0            0.00
1            0.24
2            0.36
3            0.57
4            0.70
5            0.89
6            1.14

You can do:

block_cycles = 4

# Calculate cycle times.
cycle_times = df['Total Time (s)'].diff().fillna(0).groupby(df.index // block_cycles).cumsum()

# Insert the desired zeros after all cycles.
for idx in range(block_cycles, cycle_times.index.max(), block_cycles):
    cycle_times.loc[idx-0.5] = 0
    
cycle_times = cycle_times.sort_index().reset_index(drop=True)
print(cycle_times)

Which gives:

0    0.00
1    0.24
2    0.36
3    0.57
4    0.00
5    0.13
6    0.32
7    0.57
Name: Total Time (s), dtype: float64

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1