Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

datetime - extract hour from timestamp with python

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

with this structure :

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

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

Kind regards

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...