Home > Net >  how to I use the .dt.hour accessor to get hours from a datetime object?
how to I use the .dt.hour accessor to get hours from a datetime object?

Time:11-12

I have a dataframe I'm trying to separate into hour and day, so I can use the "hour of day" as (1,2,3,...,22,23,24) as ID variables for a project.

I'm having trouble with casting .dt.hour to my date column, and it spits out:

AttributeError: Can only use .dt accessor with datetimelike values

Currently, my dateformat is: YYYY-MM-DD HH:MM:SS 00:00, and I'm assuming the error is in the 00:00

Here is a sample of the dataframe:

    date                        btc_open            btc_close
0   2021-01-01 00:00:00 00:00   28905.984003808422  29013.059128535537
1   2021-01-01 01:00:00 00:00   29016.129189426065  29432.828723553906
2   2021-01-01 02:00:00 00:00   29436.647295100185  29212.8610969002

For reproducible code (with error message), look below.

     data = pd.DataFrame({'date': ['2021-01-01 00:00:00 00:00','2021-01-01 01:00:00 00:00','2021-01-01 02:00:00 00:00'],
                          'btc_open': [28905.98, 29016.12, 29436.64],
                          'btc_close': [29013.05, 29432.82, 29212.86]})
     data['date'] = pd.to_datetime(data['date'], format = '%Y-%m-%d %H:%M:%S')

     df_subset_1 = data[['date','btc_open','btc_close']]

     # Converting datehour to date and hour columns
     df_subset_1['date'] = df_subset_1['date'].dt.date
     df_subset_1['hour'] = df_subset_1['date'].dt.hour

Does anyone know how to make this work?

CodePudding user response:

keep a column of pandas datetime dtype (see also Time series / date functionality), EX:

import pandas as pd

data = pd.DataFrame({'datetime': ['2021-01-01 00:00:00 00:00','2021-01-01 01:00:00 00:00','2021-01-01 02:00:00 00:00'],
                     'btc_open': [28905.98, 29016.12, 29436.64],
                     'btc_close': [29013.05, 29432.82, 29212.86]})

data['datetime'] = pd.to_datetime(data['datetime'])

df_subset_1 = data[['datetime','btc_open','btc_close']]

# extract date and hour from datetime column 
df_subset_1['date'] = df_subset_1['datetime'].dt.date
df_subset_1['hour'] = df_subset_1['datetime'].dt.hour

df_subset_1
                   datetime  btc_open  btc_close        date  hour
0 2021-01-01 00:00:00 00:00  28905.98   29013.05  2021-01-01     0
1 2021-01-01 01:00:00 00:00  29016.12   29432.82  2021-01-01     1
2 2021-01-01 02:00:00 00:00  29436.64   29212.86  2021-01-01     2
  • Related