Pandas, dataframe with datetime64 column, query by hours

I have a pandas dataframe dfthat has one column consisting of datetime64e.g.

<class 'pandas.core.frame.DataFrame'>
Int64Index: 1471 entries, 0 to 2940
Data columns (total 2 columns):
date    1471  non-null values
id      1471  non-null values
dtypes: datetime64[ns](1), int64(1)

I would like to subcategorize to dfuse the hour of the day as a criterion (regardless of other information in date). For example, in pseudo code

df_sub = df[ (HOUR(df.date) > 8) & (HOUR(df.date) < 20) ]

for some function HOUR.

I think the problem can be solved by first converting from datetime64to datetime. Could this be handled more efficiently?

+2
source share
1 answer

Found a simple solution.

df['hour'] = df.date.apply(lambda x : x.hour)

df_sub = df[(df.hour > 8) & (df.hour) <20]

EDIT:

dt, . :

df_sub = df[ (df.date.dt.hour > 8) 
              &  (df.date.dt.hour < 20) ]
+3

Source: https://habr.com/ru/post/1653920/


All Articles