I have some dataframe with timestamps as a column, I want to filter rows between 8:00:00 to 17:00:00 with np.where. I keep getting error messages on data/object types. Any help would be appreciated
example:
timestamp volume
2013-03-01 07:59:00 5
2013-03-01 08:00:00 6
2013-03-01 08:01:00 7
2013-03-01 08:02:00 8
Basically I want to end with:
2013-03-01 08:00:00 6
2013-03-01 08:01:00 7
2013-03-01 08:02:00 8
By using methods along the line of
np.where(df['timestamp'] > dt.time('8:00:00')
Try this:
In [226]: df
Out[226]:
timestamp volume
0 2013-03-01 07:59:00 5
1 2013-03-01 08:00:00 6
2 2013-03-01 08:01:00 7
3 2013-03-01 08:02:00 8
In [227]: df.dtypes
Out[227]:
timestamp object
volume int64
dtype: object
In [228]: df['timestamp'] = pd.to_datetime(df['timestamp'], errors='coerce')
In [229]: df.dtypes
Out[229]:
timestamp datetime64[ns] # <---- it's `datetime64[ns]` now
volume int64
dtype: object
In [230]: df.set_index('timestamp').between_time('08:00','17:00').reset_index()
Out[230]:
timestamp volume
0 2013-03-01 08:00:00 6
1 2013-03-01 08:01:00 7
2 2013-03-01 08:02:00 8
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With