简体   繁体   English

Python Pandas - Index'对象没有属性'小时'

[英]Python Pandas - Index' object has no attribute 'hour'

I have a pandas dateframe and the following code works 我有一个pandas日期框架,以下代码可以正常工作

df['hour'] = df.index.hour
df['c'] = df['hour'].apply(circadian)

but i was trying to reduce the need to make a 'hour' coloumn, using the following code 但我试图使用以下代码减少制作'小时'coloumn的需要

df['c'] = df.apply(lambda x: circadian(x.index.hour), axis=1)

but I get the error message 但我收到错误信息

AttributeError: ("'Index' object has no attribute 'hour'", u'occurred at index 2015-09-25 01:00:00')

anyone know how to do this? 有人知道怎么做吗?

Approach 1: 方法1:

Convert the DateTimeIndex to Series and use apply . DateTimeIndex转换为Series并使用apply

df['c'] = df.index.to_series().apply(lambda x: circadian(x.hour))

Approach 2: 方法2:

Use axis=0 which computes along the row-index. 使用axis=0 ,它沿行索引计算。

df['c'] = df.apply(lambda x: circadian(x.index.hour), axis=0)

solution
use the datetime accessor dt 使用datetime访问器dt

df['c'] = df.index.to_series().dt.hour.apply(circadian)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python pandas的“索引”对象没有属性“ str” - Python pandas 'Index' object has no attribute 'str' Python Pandas Group By Error 'Index' 对象没有属性 'labels' - Python Pandas Group By Error 'Index' object has no attribute 'labels' python pandas function object 没有属性 min - python pandas function object has no attribute min Pandas Styler 对象没有属性隐藏索引错误 - Pandas Styler object has no attribute hide index error Python Pandas在多个索引上按小时搜索 - Python Pandas search by hour on multiple index Python / Pandas —将日期和小时列转换为小时索引 - Python/Pandas — convert day and hour columns into index of hour Python Pandas:AttributeError:'str'对象没有属性'loc' - Python Pandas: AttributeError: 'str' object has no attribute 'loc' 如何克服“DataFrame”对象在 Python 的 Pandas 中没有属性“excelwriter” - How to overcome 'DataFrame' object has no attribute 'excelwriter' in pandas for Python Python Pandas GeopyAttributeError:“NoneType”对象没有“纬度”属性 - Python Pandas GeopyAttributeError: 'NoneType' object has no attribute 'latitude' python pandas- AttributeError:'Series'对象没有属性'columns'? - python pandas- AttributeError: 'Series' object has no attribute 'columns'?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM