2012-12-06 4 views
3

날짜 표시 문자열을 사용하여 timeseries를 인덱싱 할 수있을 것으로 예상되는 날짜/시간 인덱스가있는 시계가 s 인 경우를 가정합니다. 이것이 어떻게 작동해야하는지 오해하고 있습니까?시간 문자열을 날짜 문자열로 인덱싱

import pandas as pd 
url = 'http://ichart.finance.yahoo.com/table.csvs=SPY&d=12&e=4&f=2012&g=d&a=01&b=01&c=2001&ignore=.csv' 
df = pd.read_csv(url, index_col='Date', parse_dates=True) 
s = df['Close'] 
s['2012-12-04'] 

결과 :

TimeSeriesError       Traceback (most recent call last) 
<ipython-input-244-e2ccd4ecce94> in <module>() 
     2 df = pd.read_csv(url, index_col='Date', parse_dates=True) 
     3 s = df['Close'] 
----> 4 s['2012-12-04'] 

    G:\Python27-32\lib\site-packages\pandas\core\series.pyc in __getitem__(self, key) 
    468  def __getitem__(self, key): 
    469   try: 
--> 470    return self.index.get_value(self, key) 
    471   except InvalidIndexError: 
    472    pass 

    G:\Python27-32\lib\site-packages\pandas\tseries\index.pyc in get_value(self, series, key) 

    1030 
    1031    try: 
-> 1032     loc = self._get_string_slice(key) 
    1033     return series[loc] 
    1034    except (TypeError, ValueError, KeyError): 

G:\Python27-32\lib\site-packages\pandas\tseries\index.pyc in _get_string_slice(self, key) 
    1077   asdt, parsed, reso = parse_time_string(key, freq) 
    1078   key = asdt 
-> 1079   loc = self._partial_date_slice(reso, parsed) 
    1080   return loc 
    1081 

G:\Python27-32\lib\site-packages\pandas\tseries\index.pyc in _partial_date_slice(self, reso, parsed) 
    992  def _partial_date_slice(self, reso, parsed): 
    993   if not self.is_monotonic: 
--> 994    raise TimeSeriesError('Partial indexing only valid for ordered ' 
    995         'time series.') 
    996 

TimeSeriesError: Partial indexing only valid for ordered time series. 

더 구체적으로 (그리고 아마도 현학적 ..), 여기에 두 시계열의 차이 내용은 다음과 같습니다

import pandas as pd 
url = 'http://ichart.finance.yahoo.com/table.csv?  s=SPY&d=12&e=4&f=2012&g=d&a=01&b=01&c=2001&ignore=.csv' 
s = pd.read_csv(url, index_col='Date', parse_dates=True)['Close'] 
rng = date_range(start='2011-01-01', end='2011-12-31') 
ts = Series(randn(len(rng)), index=rng) 
print ts.__class__ 
print ts.index[0].__class__ 
print s1.__class__ 
print s1.index[0].__class__ 
print ts[ts.index[0]] 
print s[s.index[0]] 
print ts['2011-01-01'] 
try: 
    print s['2012-12-05'] 
except: 
    print "doesn't work" 

결과 :

<class 'pandas.core.series.TimeSeries'> 
<class 'pandas.lib.Timestamp'> 
<class 'pandas.core.series.TimeSeries'> 
<class 'pandas.lib.Timestamp'> 
-0.608673793503 
141.5 
-0.608673793503 
doesn't work 
+0

해당 URL에 404 오류가 표시됩니다. 테이블의 일부를 복사하거나'df.to_dict()'의 출력을 복사하는 것이 더 쉽습니다. –

답변

2

다음으로 색인 생성을 시도하십시오. Timestamp 객체 :

>>> import pandas as pd 
>>> from pandas.lib import Timestamp 
>>> url = 'http://ichart.finance.yahoo.com/table.csv?s=SPY&d=12&e=4&f=2012&g=d&a=01&b=01&c=2001&ignore=.csv' 
>>> df = pd.read_csv(url, index_col='Date', parse_dates=True) 
>>> s = df['Close'] 
>>> s[Timestamp('2012-12-04')] 
141.25 
+0

그래, 고마워, 그게 효과가 있다는 것을 알 수있다. 제 질문은 s [ '2012-12-04']가하지 않는 이유입니다. 그것은 버그입니까, 아니면 Datetime 인덱스가 포함 된 Timeseries에서 어떻게 작동해야합니까? – user1878647

+0

@ user1878647 timestamp 인덱스는 datetime이 아니지만 datetime을 사용하여 조회 할 수도 있습니다. 할 수없는 일은 알 수없는 날짜 형식의 문자열을 사용하여 조회하는 것입니다 (한 가지로 해석해야하는 날짜가 명확하지 않을 수 있습니다). 나는 그것이 벌레가 아니라고 말할 것이다. –

+0

@hayden 위의 예제에서 ts와 s는 모두 Timestamp 인덱스를 갖지만 다르게 동작합니다. – user1878647

1

시계열이 주문하지 않고 부분적인 타임 스탬프를 제공하는 경우 (예를 들어, datetime보다는 date 임) 선택해야하는 datetime이 명확하지 않습니다.

날짜별로 단 하나의 datetime 개체가 있다고 가정 할 수는 없지만 여기에는 몇 가지 옵션이 있지만 여기에는 사용자 동기를 추측하는 대신 오류가 발생하는 것이 안전합니다. (우리는 .ix['2011-01']과 비슷한 시리즈 /리스트를 반환 할 수 있지만 다른 경우에는 숫자를 반환하는 것이 혼동 스러울 수 있습니다. "가장 가까운 일치"를 반환하려고 할 수는 있지만 ... 역시 의미가 없습니다.)

주문형 일 경우 더 쉽습니다. 선택한 날짜가있는 첫 번째 날짜 시간을 선택합니다.

당신은이 간단한 예제에서이 동작에서 볼 수있다 :이 버그라고 생각하지 않습니다

import pandas as pd 
from numpy.random import randn 
from random import shuffle 
rng = pd.date_range(start='2011-01-01', end='2011-12-31') 
rng2 = list(rng) 
shuffle(rng2) # not in order 
rng3 = list(rng) 
del rng3[20] # in order, but no freq 

ts = pd.Series(randn(len(rng)), index=rng) 
ts2 = pd.Series(randn(len(rng)), index=rng2) 
ts3 = pd.Series(randn(len(rng)-1), index=rng3) 

ts.index 
<class 'pandas.tseries.index.DatetimeIndex'> 
[2011-01-01 00:00:00, ..., 2011-12-31 00:00:00] 
Length: 365, Freq: D, Timezone: None 

ts['2011-01-01'] 
# -1.1454418070543406 

ts2.index 
<class 'pandas.tseries.index.DatetimeIndex'> 
[2011-04-16 00:00:00, ..., 2011-03-10 00:00:00] 
Length: 365, Freq: None, Timezone: None 

ts2['2011-01-01'] 
#...error which you describe 
TimeSeriesError: Partial indexing only valid for ordered time series 

ts3.index 
<class 'pandas.tseries.index.DatetimeIndex'> 
[2011-01-01 00:00:00, ..., 2011-12-31 00:00:00] 
Length: 364, Freq: None, Timezone: None 

ts3['2011-01-01'] 
1.7631554507355987 


rng4 = pd.date_range(start='2011-01-01', end='2011-01-31', freq='H') 
ts4 = pd.Series(randn(len(rng4)), index=rng4) 

ts4['2011-01-01'] == ts4[0] 
# True # it picks the first element with that date 

는, 그럼에도 불구하고 나는 an issue on github로 배치했다.

+0

감사합니다. 나는 yr 시점을 본다. 방금 그 시간을 무시 했어. – user1878647

0

팬더 튜토리얼은 도움이되었지만 원래의 질문에는 직접적인 대답이 필요하다고 생각합니다.

import pandas as pd 
import datetime as dt 

def dt_parser(date): 
return dt.datetime.strptime(date, '%Y-%m-%d') + dt.timedelta(hours=16) 

url = 'http://ichart.finance.yahoo.com/table.csvs=SPY&d=12&e=4&f=2012&g=d&a=01&b=01&c=2001&ignore=.csv' 
df = pd.read_csv(url, index_col=0, parse_dates=True, date_parser=dt_parser) 
df.sort_index(inplace=True) 
s = df['Close'] 
s['2012-12-04']  # now should work 

은 "트릭은"내 자신의 date_parser을 포함했다 : 내가 필요했다 유일한 것은 것을 발견 등 슬라이스 할 수있는 DataFrame, 야후 차트 정보를 변환 같은 문제 다 퉜다. read_csv에서이 작업을 수행하는 것이 더 좋은 방법이라고 생각합니다. 그러나 적어도 인덱싱 된 DataFrame을 생성하고 슬라이스 할 수있었습니다.

관련 문제