Skip to content

Commit 2db285f

Browse files
committed
Merge pull request #7161 from sinhrks/pslice2
BUG: PeriodIndex slices with out of bounds results incorrect
2 parents 9fff73f + 21d25a9 commit 2db285f

File tree

3 files changed

+24
-0
lines changed

3 files changed

+24
-0
lines changed

doc/source/release.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,7 @@ Bug Fixes
527527
(:issue:`7105`)
528528
- Bug in ``DatetimeIndex`` specifying ``freq`` raises ``ValueError`` when passed value is too short (:issue:`7098`)
529529
- Fixed a bug with the `info` repr not honoring the `display.max_info_columns` setting (:issue:`6939`)
530+
- Bug ``PeriodIndex`` string slicing with out of bounds values (:issue:`5407`)
530531

531532
pandas 0.13.1
532533
-------------

pandas/tseries/period.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -730,6 +730,12 @@ def __iter__(self):
730730
for val in self.values:
731731
yield Period(ordinal=val, freq=self.freq)
732732

733+
def searchsorted(self, key, side='left'):
734+
if isinstance(key, compat.string_types):
735+
key = Period(key, freq=self.freq).ordinal
736+
737+
return self.values.searchsorted(key, side=side)
738+
733739
@property
734740
def is_all_dates(self):
735741
return True

pandas/tseries/tests/test_period.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1959,6 +1959,23 @@ def test_range_slice_seconds(self):
19591959
for d in ['2013/01/01', '2013/01', '2013']:
19601960
assert_series_equal(s[d:], s)
19611961

1962+
def test_range_slice_outofbounds(self):
1963+
# GH 5407
1964+
didx = DatetimeIndex(start='2013/10/01', freq='D', periods=10)
1965+
pidx = PeriodIndex(start='2013/10/01', freq='D', periods=10)
1966+
1967+
for idx in [didx, pidx]:
1968+
df = DataFrame(dict(units=[100 + i for i in range(10)]), index=idx)
1969+
empty = DataFrame(index=DatetimeIndex([], freq='D'), columns=['units'])
1970+
1971+
tm.assert_frame_equal(df['2013/09/01':'2013/09/30'], empty)
1972+
tm.assert_frame_equal(df['2013/09/30':'2013/10/02'], df.iloc[:2])
1973+
tm.assert_frame_equal(df['2013/10/01':'2013/10/02'], df.iloc[:2])
1974+
tm.assert_frame_equal(df['2013/10/02':'2013/09/30'], empty)
1975+
tm.assert_frame_equal(df['2013/10/15':'2013/10/17'], empty)
1976+
tm.assert_frame_equal(df['2013/06':'2013/09'], empty)
1977+
tm.assert_frame_equal(df['2013/11':'2013/12'], empty)
1978+
19621979
def test_pindex_qaccess(self):
19631980
pi = PeriodIndex(['2Q05', '3Q05', '4Q05', '1Q06', '2Q06'], freq='Q')
19641981
s = Series(np.random.rand(len(pi)), index=pi).cumsum()

0 commit comments

Comments
 (0)