inneb
inneb

Reputation: 1110

Delete certain rows in pandas dataframe with specific datetime value

I have a dataframe where I am just interested in the values which occur for the full hour. That is how my data looks like:

0     2016-01-01 00:00:00  11.263
1     2016-01-01 00:15:00  11.244
2     2016-01-01 00:30:00  11.109
3     2016-01-01 00:45:00  10.975
4     2016-01-01 01:00:00  10.849

The dataframe has about 50 000 rows. What I want it to look like:

0     2016-01-01 00:00:00  11.263
4     2016-01-01 01:00:00  10.849

I just want to see the values at full hours (1 am 2 am). Is there any possibility to do that? I have been trying out some stuff but it was not really working out. I have to admit that I am quite new to python.

Thanks for any help! :)

Upvotes: 1

Views: 417

Answers (1)

jezrael
jezrael

Reputation: 862641

You can use boolean indexing with comparing column date with truncated values by floor:

df['date'] = pd.to_datetime(df['date'])

mask = df['date'] == df['date'].dt.floor('H')
df = df[mask]
print (df)
                 date     val
0 2016-01-01 00:00:00  11.263
4 2016-01-01 01:00:00  10.849

Upvotes: 1

Related Questions