Python Pandas:直接从日期时间列返回小时
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25129144/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Pandas: Return Hour from Datetime Column Directly
提问by Daniel Black
Assume I have a DataFrame salesof timestamp values:
假设我有一个sales时间戳值的 DataFrame :
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
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:
我想创建一个新列time_hour。我可以通过编写一个简短的函数并使用apply()它来迭代地应用它来创建它:
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 liketo achieve is some shorter transformation like this (which I know is erroneous but gets at the spirit):
什么我想实现的是这样一些较短的转变(我知道是错误的,但在精神得到):
sales['time_hour'] = sales['timestamp'].hour
Obviously the column is of type Seriesand as such doesn't have those attributes, but it seems there's a simpler way to make use of matrix operations.
显然,列是类型的Series,因此没有这些属性,但似乎有一种更简单的方法来使用矩阵运算。
Is there a more-direct approach?
有没有更直接的方法?
采纳答案by Sudipta Basak
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)
Edit: 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:
编辑:如果您有几列 datetime 对象,则过程相同。如果您的数据框中有一列 ['date'],并假设 'date' 具有日期时间值,您可以从 'date' 访问小时:
hours = sales['date'].hour
Edit2:
If you want to adjust a column in your data frame you have to include dt:
Edit2:如果要调整数据框中的列,则必须包括dt:
sales['datehour'] = sales['date'].dt.hour
回答by Bob Hannon
You can use a lambda expression, e.g:
您可以使用lambda 表达式,例如:
sales['time_hour'] = sales.timestamp.apply(lambda x: x.hour)
回答by iff_or
For posterity: as of 0.15.0, there is a handy .dt accessoryou can use to pull such values from a datetime/period series (in the above case, just sales.timestamp.dt.hour!
对于后代:从0.15.0 开始,有一个方便的.dt 访问器可用于从日期时间/周期系列中提取此类值(在上述情况下,只需sales.timestamp.dt.hour!
回答by TCO
You can try this:
你可以试试这个:
sales['time_hour'] = pd.to_datetime(sales['timestamp']).dt.hour
回答by DINA TAKLIT
Here is a simple solution:
这是一个简单的解决方案:
import pandas as pd
# convert the timestamp column to datetime
df['timestamp'] = pd.to_datetime(df['timestamp'])
# extract hour from the timestamp column to create an time_hour column
df['time_hour'] = df['timestamp'].dt.hour

