pandas :直接从“日期时间"列返回时间 [英] Pandas: Return Hour from Datetime Column Directly
问题描述
假设我有一个时间戳值的DataFrame sales
:
Assume I have a DataFrame sales
of timestamp values:
timestamp sales_office
2014-01-01 09:01:00 Cincinnati
2014-01-01 09:11:00 San Francisco
2014-01-01 15:22:00 Chicago
2014-01-01 19:01:00 Chicago
我想创建一个新列time_hour
.我可以这样编写简短函数并使用apply()
迭代地应用它来创建它:
I would like to create a new column time_hour
. I can create it by writing a short function as so and using apply()
to apply it iteratively:
def hr_func(ts):
return ts.hour
sales['time_hour'] = sales['timestamp'].apply(hr_func)
然后我会看到以下结果:
I would then see this result:
timestamp sales_office time_hour
2014-01-01 09:01:00 Cincinnati 9
2014-01-01 09:11:00 San Francisco 9
2014-01-01 15:22:00 Chicago 15
2014-01-01 19:01:00 Chicago 19
我想 要实现的是这样的一些较短的转换(我知道这是错误的,但是很精打细算):
What I'd like to achieve is some shorter transformation like this (which I know is erroneous but gets at the spirit):
sales['time_hour'] = sales['timestamp'].hour
很显然,该列的类型为Series
,因此没有这些属性,但似乎有一种使用矩阵运算的简单方法.
Obviously the column is of type Series
and as such doesn't have those attributes, but it seems there's a simpler way to make use of matrix operations.
有更直接的方法吗?
推荐答案
假设时间戳是数据帧的索引,则可以执行以下操作:
Assuming timestamp is the index of the data frame, you can just do the following:
hours = sales.index.hour
如果要将其添加到销售数据框中,只需执行以下操作:
If you want to add that to your sales data frame, just do:
import pandas as pd
pd.concat([sales, pd.DataFrame(hours, index=sales.index)], axis = 1)
如果您有几列日期时间对象,则过程相同.如果数据框中有['date']列,并假设'date'具有datetime值,则可以按以下方式访问"date"中的小时:
If you have several columns of datetime objects, it's the same process. If you have a column ['date'] in your data frame, and assuming that 'date' has datetime values, you can access the hour from the 'date' as:
hours = sales['date'].hour
Edit2:
如果要调整数据框中的列,则必须包含dt
:
If you want to adjust a column in your data frame you have to include dt
:
sales['datehour'] = sales['date'].dt.hour
这篇关于 pandas :直接从“日期时间"列返回时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!