使用 python 从时间戳中提取小时
extract hour from timestamp with python
我有一个数据框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
有没有python函数可以从TIMESTAMP
中提取小时?
亲切的问候
我觉得你需要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
鉴于您的数据:
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
您有时间戳作为索引。从时间戳中提取小时数作为数据帧的索引:
hours = df_energy2.index.hour
编辑:是的,jezrael,你是对的。说出他所说的话:pandas dataframe 为此有一个 属性 即 dt
:
<dataframe>.<ts_column>.dt.hour
您上下文中的示例 - 带有日期的列是 TIMESTAMP
df.TIMESTAMP.dt.hour
类似的问题 - Pandas, dataframe with a datetime64 column, querying by hour
我有一个数据框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
有没有python函数可以从TIMESTAMP
中提取小时?
亲切的问候
我觉得你需要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
鉴于您的数据:
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
您有时间戳作为索引。从时间戳中提取小时数作为数据帧的索引:
hours = df_energy2.index.hour
编辑:是的,jezrael,你是对的。说出他所说的话:pandas dataframe 为此有一个 属性 即 dt
:
<dataframe>.<ts_column>.dt.hour
您上下文中的示例 - 带有日期的列是 TIMESTAMP
df.TIMESTAMP.dt.hour
类似的问题 - Pandas, dataframe with a datetime64 column, querying by hour