Skip to content
Browse files

BUG: fix searchsorted off by one error, close #1068

  • Loading branch information...
1 parent 219bdcb commit e1552a607be435b71e16da02940118b30a25b939 @wesm wesm committed Apr 16, 2012
Showing with 12 additions and 13 deletions.
  1. +0 −4 pandas/core/groupby.py
  2. +6 −7 pandas/core/index.py
  3. +6 −2 pandas/tests/test_timeseries.py
View
4 pandas/core/groupby.py
@@ -1323,10 +1323,6 @@ def _get_index():
index = MultiIndex.from_tuples(keys, names=self.grouper.names)
else:
index = Index(keys, name=self.grouper.names[0])
- # if len(keys) == self.grouper.ngroups:
- # index = self.grouper.result_index
- # else:
- # index = Index(keys, name=self.grouper.names[0])
return index
if isinstance(values[0], Series):
View
13 pandas/core/index.py
@@ -1711,23 +1711,22 @@ def _partial_date_slice(self, reso, parsed):
if reso == 'year':
t1 = to_timestamp(datetime(parsed.year, 1, 1))
t2 = to_timestamp(datetime(parsed.year, 12, 31))
- i1, i2 = np.searchsorted(self.asi8, [t1.value, t2.value])
- return slice(i1, i2+1)
elif reso == 'month':
d = lib.monthrange(parsed.year, parsed.month)[1]
t1 = to_timestamp(datetime(parsed.year, parsed.month, 1))
t2 = to_timestamp(datetime(parsed.year, parsed.month, d))
- i1, i2 = np.searchsorted(self.asi8, [t1.value, t2.value])
- return slice(i1, i2+1)
elif reso == 'quarter':
qe = (((parsed.month - 1) + 2) % 12) + 1 # two months ahead
d = lib.monthrange(parsed.year, qe)[1] # at end of month
t1 = to_timestamp(datetime(parsed.year, parsed.month, 1))
t2 = to_timestamp(datetime(parsed.year, qe, d))
- i1, i2 = np.searchsorted(self.asi8, [t1.value, t2.value])
- return slice(i1, i2+1)
+ else:
+ raise KeyError
- raise KeyError
+ stamps = self.asi8
+ left = stamps.searchsorted(t1.value, side='left')
+ right = stamps.searchsorted(t2.value, side='right')
+ return slice(left, right)
def get_value(self, series, key):
"""
View
8 pandas/tests/test_timeseries.py
@@ -548,10 +548,14 @@ def test_slice_year(self):
dti = DatetimeIndex(freq='B', start=datetime(2005,1,1), periods=500)
s = Series(np.arange(len(dti)), index=dti)
- self.assertEquals(len(s['2005']), 261)
+ result = s['2005']
+ expected = s[s.index.year == 2005]
+ assert_series_equal(result, expected)
df = DataFrame(np.random.rand(len(dti), 5), index=dti)
- self.assertEquals(len(df.ix['2005']), 261)
+ result = df.ix['2005']
+ expected = df[df.index.year == 2005]
+ assert_frame_equal(result, expected)
def test_slice_quarter(self):
dti = DatetimeIndex(freq='D', start=datetime(2000,6,1), periods=500)

0 comments on commit e1552a6

Please sign in to comment.
Something went wrong with that request. Please try again.