使用python从时间戳中提取小时 [英] extract hour from timestamp with python

查看:405
本文介绍了使用python从时间戳中提取小时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据框df_energy2

I have a dataframe df_energy2

df_energy2.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 29974 entries, 0 to 29973
Data columns (total 4 columns):
TIMESTAMP        29974 non-null datetime64[ns]
P_ACT_KW         29974 non-null int64
PERIODE_TARIF    29974 non-null object
P_SOUSCR         29974 non-null int64
dtypes: datetime64[ns](1), int64(2), object(1)
memory usage: 936.8+ KB

具有这种结构:

df_energy2.head()

TIMESTAMP P_ACT_KW PERIODE_TARIF P_SOUSCR
2016-01-01 00:00:00 116 HC 250 
2016-01-01 00:10:00 121 HC 250

是否有任何可以从TIMESTAMP中提取小时的python功能?

Is there any python fucntion which can extract hour from TIMESTAMP?

亲切的问候

推荐答案

我认为您需要

I think you need dt.hour:

print (df.TIMESTAMP.dt.hour)
0    0
1    0
Name: TIMESTAMP, dtype: int64

df['hours'] = df.TIMESTAMP.dt.hour
print (df)
            TIMESTAMP  P_ACT_KW PERIODE_TARIF  P_SOUSCR  hours
0 2016-01-01 00:00:00       116            HC       250      0
1 2016-01-01 00:10:00       121            HC       250      0

这篇关于使用python从时间戳中提取小时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆