4

I have a dataframe of many days that look like this....consecutive rows of 30 min intervals:

                      a   b
2006-05-08 09:30:00  10  13
2006-05-08 10:00:00  11  12
                          .
                          .
                          .
2006-05-08 15:30:00  15  14
2006-05-08 16:00:00  16  15

However, I only care about certain specific times, so I want EVERY DAY of the df to look like:

2006-05-08 09:30:00  10  13
2006-05-08 11:30:00  14  15
2006-05-08 13:00:00  18  15
2006-05-08 16:00:00  16  15

Meaning, I just want to keep the rows at times (16, 13, 11:30, 9:30), for all the different days in the dataframe.

Thanks

Update:

I made a bit of progress, using

hour = df.index.hour
selector = ((hour == 16) | (hour == 13) | (hour == 11) | (hour == 9))
df = df[selector]

However, I need to account for the minutes too, so I tried:

minute = df.index.minute
selector = ((hour == 16) & (minute == 0) | (hour == 3) & (minute == 0) | (hour == 9) & (minute == 30) | (hour == 12) & (minute == 0))

But I get error:

ValueError: operands could not be broadcast together with shapes (96310,) (16500,) 
hernanavella
  • 5,462
  • 8
  • 47
  • 84

1 Answers1

1
import numpy as np
import pandas as pd
N = 100
df = pd.DataFrame(range(N), index=pd.date_range('2000-1-1', freq='30T', 
                                                periods=N))
mask = np.in1d((df.index.hour)*100+(df.index.minute), [930, 1130, 1300, 1600])
print(df.loc[mask])

yields

                      0
2000-01-01 09:30:00  19
2000-01-01 11:30:00  23
2000-01-01 13:00:00  26
2000-01-01 16:00:00  32
2000-01-02 09:30:00  67
2000-01-02 11:30:00  71
2000-01-02 13:00:00  74
2000-01-02 16:00:00  80
unutbu
  • 842,883
  • 184
  • 1,785
  • 1,677
  • No, I don't. I tried your code on my `df` and it works just fine... :/ I would put an extra set of parentheses around each `((hour == X) & (minutes = Y))` pair, but I don't think that actually changes the result. – unutbu Nov 11 '14 at 01:20
  • Do the shapes `(96310,)` and `(16500,)` mean anything to you? That might be a clue... One is probably the length of `df`, but what is the other? If you print out the shape of `selector` or bits and pieces of `selector` and find the misshapen result, you'll find the bug. – unutbu Nov 11 '14 at 01:24