按日期字符串索引时间序列 [英] Indexing timeseries by date string

查看:95
本文介绍了按日期字符串索引时间序列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出一个具有日期时间索引的时间序列s,我希望能够通过日期字符串为该时间序列建立索引.我是否误解了它应该如何工作?

Given a timeseries, s, with a datetime index I expected to be able to index the timeseries by the date string. Am I misunderstanding how this should work?

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. 

更具体地说(也许是书呆子..),这里的两个时间序列有什么区别:

To be more specific (and perhaps pedantic..), what's the difference between the 2 Timeseries here:

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

推荐答案

尝试使用Timestamp对象建立索引:

Try indexing with a Timestamp object:

>>> 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

这篇关于按日期字符串索引时间序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆