'How to extract the data on hourly basis using pandas?

I have the dataframe in which data is minute basis like shown below:

           Date               Open    High    Low     Close 
0   01.01.2015 00:00:00.000 1.20976 1.20976 1.20976 1.20976 0.0
1   01.01.2015 00:01:00.000 1.20976 1.20976 1.20976 1.20976 0.0
2   01.01.2015 00:02:00.000 1.20976 1.20976 1.20976 1.20976 0.0

As you can see there is 12:01, 12:02.... I do not need the data like this , I expect only hourly basis like 12:00, 13:00, 14:00 etc. How can I do this ?



Solution 1:[1]

You can use DatetimeIndex.floor, assuming Date column has already datetime64 dtype:

>>> df[df['Date'] == df['Date'].dt.floor('H')]
        Date     Open     High      Low        Close
0 2015-01-01  1.20976  1.20976  1.20976  1.20976 0.0

How can I convert this string '20030505 01:00:00.000'

df['Date'] = pd.to_datetime(df['Date'], format='%Y%d%m %H:%M:%S.%f')

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1