Reputation: 60
I've found many answers that address slicing a single time window, but none that address removing multiple time windows based on some criteria.
I have a large DataFrame with weather data in it (with a Datetime index):
Station_ID air_temp_set_1 relative_humidity_set_1
2015-01-01 00:10:00+00:00 UT23 -7.44 47.43
2015-01-01 00:20:00+00:00 UT23 -7.38 47.33
2015-01-01 00:30:00+00:00 UT23 -7.34 46.96
....
and I want to create other DataFrames that have, for example, all the information between the hours of 6 a.m. and 12 p.m. for every day, but no entries for times outside of that range. Or, I want to create a new one that only has data for June of every year (ideally without iterating over the whole thing). I've had success slicing in this way based on other variables, for example
cold = data.loc[data['air_temp_set_1'] < 0]
but this method doesn't seem to work with the index.
Upvotes: 0
Views: 885
Reputation: 730
Pandas offers an indexer for time-slicing. It works like:
data.loc[data.index.indexer_between_time(start_time, end_time)]
for slicing by a particular month see the comment by ALollz that provides something like this formula:
data.loc[data.index.month == 6]
To include multiple slicing rules, surround your expressions in parentheses and use the boolean &
operator:
data.loc[(data.index.month == 6) & (data.index.month == 4)]
Here's a link to the documentation for the DatetimeIndex.indexer_between_time: https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DatetimeIndex.indexer_between_time.html
Upvotes: 2