Home > Enterprise >  How to extract the data on hourly basis using pandas?
How to extract the data on hourly basis using pandas?

Time:02-22

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 ?

CodePudding user response:

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')

CodePudding user response:

Try something like this:

df['Date'] = pd.to_datetime(df['Date']) # You may not need to do this.

df = df[df['Date'].dt.minute == 0]
  • Related