4

I have a pandas dataframe 'df' with a column 'DateTimes' of type datetime.time.

The entries of that column are hours of a single day:

00:00:00
.
.
.
23:59:00

Seconds are skipped, it counts by minutes.

How can I choose rows by hour, for example the rows between 00:00:00 and 00:01:00?


If I try this:

df.between_time('00:00:00', '00:00:10')

I get an error that index must be a DateTimeIndex.

I set the index as such with:

df=df.set_index(keys='DateTime')

but I get the same error.

I can't seem to get 'loc' to work either. Any suggestions?

Mike
  • 444
  • 1
  • 8
  • 19

2 Answers2

6

Here a working example of what you are trying to do:

times = pd.date_range('3/6/2012 00:00', periods=100, freq='S', tz='UTC')
df = pd.DataFrame(np.random.randint(10, size=(100,1)), index=times)
df.between_time('00:00:00', '00:00:30')

Note the index has to be of type DatetimeIndex.

I understand you have a column with your dates/times. The problem probably is that your column is not of this type, so you have to convert it first, before setting it as index:

# Method A
df.set_index(pd.to_datetime(df['column_name'], drop=True)

# Method B
df.index = pd.to_datetime(df['column_name'])
df = df.drop('col', axis=1)

(The drop is only necessary if you want to remove the original column after setting it as index)

nkaenzig
  • 684
  • 7
  • 14
  • Thanks, that worked perfectly, because I had already converted to datetime.time I had an issue converting back to datetime, but I resolved it. – Mike Feb 20 '19 at 17:42
0

Check out these links:
convert column to date type: Convert DataFrame column type from string to datetime
filter dataframe on dates: Filtering Pandas DataFrames on dates
Hope this helps

  • Thanks, my data is already converted to datetime, specifically datetime.time. I've read through the second link, but what they do with 'loc' doesn't seem to work in my case, and the discussion mostly revolves around dates, not times, I've had difficulty connecting from what they are saying to my context. – Mike Feb 20 '19 at 17:21