diff --git a/doc/source/release.rst b/doc/source/release.rst index 01468e35a037c..6fde66821f650 100644 --- a/doc/source/release.rst +++ b/doc/source/release.rst @@ -527,6 +527,7 @@ Bug Fixes (:issue:`7105`) - Bug in ``DatetimeIndex`` specifying ``freq`` raises ``ValueError`` when passed value is too short (:issue:`7098`) - Fixed a bug with the `info` repr not honoring the `display.max_info_columns` setting (:issue:`6939`) +- Bug ``PeriodIndex`` string slicing with out of bounds values (:issue:`5407`) pandas 0.13.1 ------------- diff --git a/pandas/tseries/period.py b/pandas/tseries/period.py index b70a7dafa28bd..1d528c9545863 100644 --- a/pandas/tseries/period.py +++ b/pandas/tseries/period.py @@ -730,6 +730,12 @@ def __iter__(self): for val in self.values: yield Period(ordinal=val, freq=self.freq) + def searchsorted(self, key, side='left'): + if isinstance(key, compat.string_types): + key = Period(key, freq=self.freq).ordinal + + return self.values.searchsorted(key, side=side) + @property def is_all_dates(self): return True diff --git a/pandas/tseries/tests/test_period.py b/pandas/tseries/tests/test_period.py index 15cbbd69bb52c..b599665b936f3 100644 --- a/pandas/tseries/tests/test_period.py +++ b/pandas/tseries/tests/test_period.py @@ -1959,6 +1959,23 @@ def test_range_slice_seconds(self): for d in ['2013/01/01', '2013/01', '2013']: assert_series_equal(s[d:], s) + def test_range_slice_outofbounds(self): + # GH 5407 + didx = DatetimeIndex(start='2013/10/01', freq='D', periods=10) + pidx = PeriodIndex(start='2013/10/01', freq='D', periods=10) + + for idx in [didx, pidx]: + df = DataFrame(dict(units=[100 + i for i in range(10)]), index=idx) + empty = DataFrame(index=DatetimeIndex([], freq='D'), columns=['units']) + + tm.assert_frame_equal(df['2013/09/01':'2013/09/30'], empty) + tm.assert_frame_equal(df['2013/09/30':'2013/10/02'], df.iloc[:2]) + tm.assert_frame_equal(df['2013/10/01':'2013/10/02'], df.iloc[:2]) + tm.assert_frame_equal(df['2013/10/02':'2013/09/30'], empty) + tm.assert_frame_equal(df['2013/10/15':'2013/10/17'], empty) + tm.assert_frame_equal(df['2013/06':'2013/09'], empty) + tm.assert_frame_equal(df['2013/11':'2013/12'], empty) + def test_pindex_qaccess(self): pi = PeriodIndex(['2Q05', '3Q05', '4Q05', '1Q06', '2Q06'], freq='Q') s = Series(np.random.rand(len(pi)), index=pi).cumsum()