给定一个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.
更具体地说(也许是迂腐的..),这里的 2 个 Timeseries 有什么区别:
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