Repository: pandas-dev/pandas

Issue:
BUG: partial string indexing with PeriodIndex
partial indexing works with time series, but not period index

```
In [27]: df2 = DataFrame(np.arange(20).reshape(10,2),index=pd.date_range('20130101 09:00',freq='T',periods=10))

In [28]: df = DataFrame(np.arange(20).reshape(10,2),index=pd.period_range('20130101 09:00',freq='T',periods=10))

In [29]: df2['20130101']
Out[29]: 
                      0   1
2013-01-01 09:00:00   0   1
2013-01-01 09:01:00   2   3
2013-01-01 09:02:00   4   5
2013-01-01 09:03:00   6   7
2013-01-01 09:04:00   8   9
2013-01-01 09:05:00  10  11
2013-01-01 09:06:00  12  13
2013-01-01 09:07:00  14  15
2013-01-01 09:08:00  16  17
2013-01-01 09:09:00  18  19

[10 rows x 2 columns]

In [30]: df['20130101']
KeyError: u'no item named 20130101'

In [31]: df2['20130101 09']
Out[31]: 
                      0   1
2013-01-01 09:00:00   0   1
2013-01-01 09:01:00   2   3
2013-01-01 09:02:00   4   5
2013-01-01 09:03:00   6   7
2013-01-01 09:04:00   8   9
2013-01-01 09:05:00  10  11
2013-01-01 09:06:00  12  13
2013-01-01 09:07:00  14  15
2013-01-01 09:08:00  16  17
2013-01-01 09:09:00  18  19

[10 rows x 2 columns]

In [32]: df['20130101 09']
KeyError: u'no item named 20130101 09'
```

Works fine for exact start points

```
In [34]: df2['20130101 09:05':]
Out[34]: 
                      0   1
2013-01-01 09:05:00  10  11
2013-01-01 09:06:00  12  13
2013-01-01 09:07:00  14  15
2013-01-01 09:08:00  16  17
2013-01-01 09:09:00  18  19

[5 rows x 2 columns]

In [35]: df['20130101 09:05':]
Out[35]: 
                   0   1
2013-01-01 09:05  10  11
2013-01-01 09:06  12  13
2013-01-01 09:07  14  15
2013-01-01 09:08  16  17
2013-01-01 09:09  18  19

[5 rows x 2 columns]
```



Discussion:

Resolving patch:
diff --git a/pandas/tseries/period.py b/pandas/tseries/period.py
--- a/pandas/tseries/period.py
+++ b/pandas/tseries/period.py
@@ -908,13 +908,13 @@ def get_value(self, series, key):
                     pos = np.searchsorted(self.values, [ord1, ord2])
                     key = slice(pos[0], pos[1] + 1)
                     return series[key]
-                else:
+                elif grp == freqn:
                     key = Period(asdt, freq=self.freq).ordinal
                     return _maybe_box(self, self._engine.get_value(s, key), series, key)
+                else:
+                    raise KeyError(key)
             except TypeError:
                 pass
-            except KeyError:
-                pass
 
             key = Period(key, self.freq).ordinal
             return _maybe_box(self, self._engine.get_value(s, key), series, key)
@@ -978,8 +978,10 @@ def _get_string_slice(self, key):
             raise ValueError('Partial indexing only valid for '
                              'ordered time series')
 
-        asdt, parsed, reso = parse_time_string(key, self.freq)
-        key = asdt
+        key, parsed, reso = parse_time_string(key, self.freq)
+
+        grp = _freq_mod._infer_period_group(reso)
+        freqn = _freq_mod._period_group(self.freq)
 
         if reso == 'year':
             t1 = Period(year=parsed.year, freq='A')
@@ -988,6 +990,19 @@ def _get_string_slice(self, key):
         elif reso == 'quarter':
             q = (parsed.month - 1) // 3 + 1
             t1 = Period(year=parsed.year, quarter=q, freq='Q-DEC')
+        elif reso == 'day' and grp < freqn:
+            t1 = Period(year=parsed.year, month=parsed.month, day=parsed.day,
+                        freq='D')
+        elif reso == 'hour' and grp < freqn:
+            t1 = Period(year=parsed.year, month=parsed.month, day=parsed.day,
+                        hour=parsed.hour, freq='H')
+        elif reso == 'minute' and grp < freqn:
+            t1 = Period(year=parsed.year, month=parsed.month, day=parsed.day,
+                        hour=parsed.hour, minute=parsed.minute, freq='T')
+        elif reso == 'second' and grp < freqn:
+            t1 = Period(year=parsed.year, month=parsed.month, day=parsed.day,
+                        hour=parsed.hour, minute=parsed.minute, second=parsed.second,
+                        freq='S')
         else:
             raise KeyError(key)
 


Test patch:
diff --git a/pandas/tseries/tests/test_period.py b/pandas/tseries/tests/test_period.py
--- a/pandas/tseries/tests/test_period.py
+++ b/pandas/tseries/tests/test_period.py
@@ -1851,6 +1851,97 @@ def test_pindex_slice_index(self):
         exp = s[12:24]
         assert_series_equal(res, exp)
 
+    def test_getitem_day(self):
+        # GH 6716
+        # Confirm DatetimeIndex and PeriodIndex works identically
+        didx = DatetimeIndex(start='2013/01/01', freq='D', periods=400)
+        pidx = PeriodIndex(start='2013/01/01', freq='D', periods=400)
+
+        for idx in [didx, pidx]:
+            # getitem against index should raise ValueError
+            values = ['2014', '2013/02', '2013/01/02',
+                      '2013/02/01 9H', '2013/02/01 09:00']
+            for v in values:
+                with tm.assertRaises(ValueError):
+                    idx[v]
+
+            s = Series(np.random.rand(len(idx)), index=idx)
+            assert_series_equal(s['2013/01'], s[0:31])
+            assert_series_equal(s['2013/02'], s[31:59])
+            assert_series_equal(s['2014'], s[365:])
+
+            invalid = ['2013/02/01 9H', '2013/02/01 09:00']
+            for v in invalid:
+                with tm.assertRaises(KeyError):
+                    s[v]
+
+    def test_range_slice_day(self):
+        # GH 6716
+        didx = DatetimeIndex(start='2013/01/01', freq='D', periods=400)
+        pidx = PeriodIndex(start='2013/01/01', freq='D', periods=400)
+
+        for idx in [didx, pidx]:
+            # slices against index should raise IndexError
+            values = ['2014', '2013/02', '2013/01/02',
+                      '2013/02/01 9H', '2013/02/01 09:00']
+            for v in values:
+                with tm.assertRaises(IndexError):
+                    idx[v:]
+
+            s = Series(np.random.rand(len(idx)), index=idx)
+
+            assert_series_equal(s['2013/01/02':], s[1:])
+            assert_series_equal(s['2013/01/02':'2013/01/05'], s[1:5])
+            assert_series_equal(s['2013/02':], s[31:])
+            assert_series_equal(s['2014':], s[365:])
+
+            invalid = ['2013/02/01 9H', '2013/02/01 09:00']
+            for v in invalid:
+                with tm.assertRaises(IndexError):
+                    idx[v:]
+
+    def test_getitem_seconds(self):
+        # GH 6716
+        didx = DatetimeIndex(start='2013/01/01 09:00:00', freq='S', periods=4000)
+        pidx = PeriodIndex(start='2013/01/01 09:00:00', freq='S', periods=4000)
+
+        for idx in [didx, pidx]:
+            # getitem against index should raise ValueError
+            values = ['2014', '2013/02', '2013/01/02',
+                      '2013/02/01 9H', '2013/02/01 09:00']
+            for v in values:
+                with tm.assertRaises(ValueError):
+                    idx[v]
+
+            s = Series(np.random.rand(len(idx)), index=idx)
+
+            assert_series_equal(s['2013/01/01 10:00'], s[3600:3660])
+            assert_series_equal(s['2013/01/01 9H'], s[:3600])
+            for d in ['2013/01/01', '2013/01', '2013']:
+                assert_series_equal(s[d], s)
+
+    def test_range_slice_seconds(self):
+        # GH 6716
+        didx = DatetimeIndex(start='2013/01/01 09:00:00', freq='S', periods=4000)
+        pidx = PeriodIndex(start='2013/01/01 09:00:00', freq='S', periods=4000)
+
+        for idx in [didx, pidx]:
+            # slices against index should raise IndexError
+            values = ['2014', '2013/02', '2013/01/02',
+                      '2013/02/01 9H', '2013/02/01 09:00']
+            for v in values:
+                with tm.assertRaises(IndexError):
+                    idx[v:]
+
+            s = Series(np.random.rand(len(idx)), index=idx)
+
+            assert_series_equal(s['2013/01/01 09:05':'2013/01/01 09:10'], s[300:660])
+            assert_series_equal(s['2013/01/01 10:00':'2013/01/01 10:05'], s[3600:3960])
+            assert_series_equal(s['2013/01/01 10H':], s[3600:])
+            assert_series_equal(s[:'2013/01/01 09:30'], s[:1860])
+            for d in ['2013/01/01', '2013/01', '2013']:
+                assert_series_equal(s[d:], s)
+
     def test_pindex_qaccess(self):
         pi = PeriodIndex(['2Q05', '3Q05', '4Q05', '1Q06', '2Q06'], freq='Q')
         s = Series(np.random.rand(len(pi)), index=pi).cumsum()