2013-05-28 7 views
5

사용 팬더 나는 (월, 일 포함) 날짜 객체를 사용하여 시계열 및 기간에 대한 값을 얻을, 예를 들어 인덱스 할 수 있습니다만들기 팬더의 시계열

from pandas import * 
ts = TimeSeries([41,45,48],[Period('2012'),Period('2013'),Period('2014')]) 
print ts[datetime(2013,05,17)] 

이 있습니까 한 달에 1 년이지만 1 년이없는 기간을 정의하는 방법은 무엇입니까?

ts = TimeSeries(range(1,13),[Period(month=n,freq='M') for n in range(1,13)]) 
print ts[datetime(2013,05,17)] 

기간 객체가이 (이것은 오류가 발생합니다 지원하지 않는 것 : 나는 예는, 월/일에 의해 색인을 수 있도록하고 싶습니다 것으로, 매달 주파수 연평균 프로필이). 일년에 timeseries를 만드는 것보다 datetime 오브젝트를 수정하는 것이 더 나은 방법일까요?

http://pandas.pydata.org/pandas-docs/dev/timeseries.html#period

편집 1 : 나는 매일 시간 단계에서 계산하는 모델을 가지고 : 나는이 일을하려는 이유

조금 명확히하기. 현재 날짜를 나타내는 datetime 객체 인 모델에 변수가 있습니다. 나는 몇몇 날짜들에 대해 현재 날짜를 검사 할 필요가 있는데, 그 중 일부는 전체 날짜 (년/월/일)를 가졌지 만 나머지는 한 달 밖에 걸리지 않는다. timeseries/profiles은 런타임에 사용자가 제공하기 때문에 인덱싱만큼 매끄러운 것을 기대하고있었습니다. TimeSeries 객체의 __getitem__ 메서드를 재정의하는 작업을 수행 했으므로 (수 년 뒤의 문제를 해결할 수 있도록) 그러나 약간의 미친 해킹처럼 보입니다.

from pandas import * 

class TimeSeriesProfile(TimeSeries): 
    year = 2004 

    def __new__(self, *args, **kwargs): 
     inst = TimeSeries.__new__(self, *args, **kwargs) 
     inst.index = period_range(str(self.year)+str(inst.index[0])[4:], periods=len(inst.index), freq=inst.index.freq) 
     return inst.view(TimeSeriesProfile) 

    def __getitem__(self, key): 
     without_year = datetime(self.year, key.month, key.day, key.hour, key.minute, key.second) 
     return TimeSeries.__getitem__(self, without_year) 

ts = TimeSeriesProfile(range(0, 366), period_range('1996-01-01', periods=366, freq='D')) 

print ts[datetime(2008, 02, 29)] 

답변

1

period_range을 시도해보십시오

In [65]: TimeSeries(range(1, 13), period_range('2013-01', periods=12, freq='M')) 
Out[65]: 
2013-01  1 
2013-02  2 
2013-03  3 
2013-04  4 
2013-05  5 
2013-06  6 
2013-07  7 
2013-08  8 
2013-09  9 
2013-10 10 
2013-11 11 
2013-12 12 
Freq: M, dtype: int64 
+5

'period_range' 유용 보이지만 일년없이 시계열을 만드는 방법에 대한 내 기본 질문에 대답하지 않습니다. – Snorfalorpagus