user6357081
user6357081

Reputation: 3

Slicing a dataframe based on range of time

I am new to Pandas. I have a set of Excel data read into a dataframe as follows:

TimeReceived    A   B
08:00:01.010    70  40
08:00:01.050    80  50
08:01:01.100    50  20
08:01:01.150    40  30

I want to compute the average for columns A & B based on time intervals of 100 ms. The output in this case would be :

TimeReceived    A   B
 08:00:01.000   75  45
 08:00:01.100   45  25

I have set the 'TimeReceived' as a Date-Time index:

df = df.set_index (['TimeReceived']) 

I can select rows based on predefined time ranges but I cannot do computations on time intervals as shown above.

Upvotes: 0

Views: 91

Answers (1)

EdChum
EdChum

Reputation: 393973

If you have aDatetimeIndex you can then use resample to up or down sample your data to a new frequency. This will introduce NaN rows where there are gaps but you can drop these using dropna:

df.resample('100ms').mean().dropna()

Upvotes: 1

Related Questions