2016-10-02 4 views
2

私はパンダのdateframeを持っているし、次のコードは、Pythonのパンダ - インデックス時間 『

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

動作しますが、私は作る必要削減しようとしていた『を使用して、時間』coloumnを」オブジェクトには属性がありません』コード

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

以下が、私は

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

誰もがこれを行う方法を知っているエラーメッセージを取得?

答えて

4

アプローチ1:

SeriesDateTimeIndexを変換しapplyを使用しています。

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

アプローチ2:行インデックスに沿って計算

使用axis=0

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

ソリューション
は、日時アクセサを使用dt

df['c'] = df.index.to_series().dt.hour.apply(circadian) 
関連する問題