2016-10-02 5 views
2

가 나는 팬더 dateframe가 다음과 같은 코드가파이썬 팬더 - 색인 시간 '

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 :

DateTimeIndexSeries에 변환 및 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)