user2958395
user2958395

Reputation: 129

Select rows from pandas DataFrame of a certain date

I am fairly new to pandas and getting a problem with manipulating a DataFrame.

What I have is a DataFrame with repeating dates and I would like to only keep the dates which are hourly.

Here is an example of my current DataFrame:

Time                     ColA ColB
5/12/2011 10:00:00 PM     9     4 
5/12/2011 10:15:00 PM     5     3 
5/12/2011 10:30:00 PM     1     1 
5/12/2011 10:45:00 PM     2     3 
5/12/2011 11:00:00 PM     10    4 

Thus the result should be a DataFrame that contains the first and last row alone.

Time                     ColA ColB
5/12/2011 10:00:00 PM     9     4
5/12/2011 11:00:00 PM     10    4

Upvotes: 2

Views: 5103

Answers (2)

Liam Foley
Liam Foley

Reputation: 7822

Make sure your column is a datetime (use pd.to_datetime) and not a string.

df = df[df['Time'].apply(lambda x: x.minute) == 0]

Upvotes: 1

EdChum
EdChum

Reputation: 393893

So long as the column is a datetime already you can access the minute atribute and use this to filter:

In [26]:

df[df.Time.dt.minute == 0]
Out[26]:
                 Time  ColA  ColB
0 2011-05-12 22:00:00     9     4
4 2011-05-12 23:00:00    10     4

If necessary convert the string to a datetime using: df['Time'] = pd.to_datetime(df['Time')

Upvotes: 2

Related Questions