Skip to content

Commit 30fba73

Browse files
committed
BUG: PeriodIndex.get_loc KeyError now reports Period instead of ordinal #3179
1 parent 91ad389 commit 30fba73

File tree

3 files changed

+15
-2
lines changed

3 files changed

+15
-2
lines changed

RELEASE.rst

+2
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,7 @@ pandas 0.11.0
229229
- fixed handling of rolling_corr with center=True which could produce corr>1 (GH3155_)
230230
- Fixed issues where indices can be passed as 'index/column' in addition to 0/1 for the axis parameter
231231
- PeriodIndex.tolist now boxes to Period (GH3178_)
232+
- PeriodIndex.get_loc KeyError now reports Period instead of ordinal (GH3179_)
232233

233234
.. _GH622: https://github.com/pydata/pandas/issues/622
234235
.. _GH797: https://github.com/pydata/pandas/issues/797
@@ -303,6 +304,7 @@ pandas 0.11.0
303304
.. _GH3094: https://github.com/pydata/pandas/issues/3094
304305
.. _GH3130: https://github.com/pydata/pandas/issues/3130
305306
.. _GH3178: https://github.com/pydata/pandas/issues/3178
307+
.. _GH3179: https://github.com/pydata/pandas/issues/3179
306308

307309
pandas 0.10.1
308310
=============

pandas/tseries/period.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -887,8 +887,11 @@ def get_loc(self, key):
887887
except TypeError:
888888
pass
889889

890-
key = Period(key, self.freq).ordinal
891-
return self._engine.get_loc(key)
890+
key = Period(key, self.freq)
891+
try:
892+
return self._engine.get_loc(key.ordinal)
893+
except KeyError as inst:
894+
raise KeyError(repr(key))
892895

893896
def slice_locs(self, start=None, end=None):
894897
"""

pandas/tseries/tests/test_period.py

+8
Original file line numberDiff line numberDiff line change
@@ -1926,6 +1926,14 @@ def test_to_datetime_1703(self):
19261926
result = index.to_datetime()
19271927
self.assertEquals(result[0], Timestamp('1/1/2012'))
19281928

1929+
def test_get_loc_msg(self):
1930+
idx = period_range('2000-1-1', freq='A', periods=10)
1931+
bad_period = Period('2012', 'A')
1932+
try:
1933+
idx.get_loc(bad_period)
1934+
except KeyError as inst:
1935+
self.assert_(inst.message == repr(bad_period))
1936+
19291937
def test_append_concat(self):
19301938
# #1815
19311939
d1 = date_range('12/31/1990', '12/31/1999', freq='A-DEC')

0 commit comments

Comments
 (0)