Skip to content

Commit 582d154

Browse files
committed
fixing #5372
1 parent 78a5112 commit 582d154

File tree

3 files changed

+20
-3
lines changed

3 files changed

+20
-3
lines changed

doc/source/release.rst

+2-1
Original file line numberDiff line numberDiff line change
@@ -760,7 +760,8 @@ Bug Fixes
760760
(thanks for catching this @yarikoptic!)
761761
- Fixed html tests on win32. (:issue:`4580`)
762762
- Make sure that ``head/tail`` are ``iloc`` based, (:issue:`5370`)
763-
763+
- Fixed bug for ``PeriodIndex`` string representation if there are 1 or 2
764+
elements. (:issue:`5372`)
764765

765766
pandas 0.12.0
766767
-------------

pandas/tseries/period.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -1082,7 +1082,11 @@ def __repr__(self):
10821082
output = com.pprint_thing(self.__class__) + '\n'
10831083
output += 'freq: %s\n' % self.freq
10841084
n = len(self)
1085-
if n:
1085+
if n == 1:
1086+
output += '[%s]\n' % (self[0])
1087+
elif n == 2:
1088+
output += '[%s, %s]\n' % (self[0], self[-1])
1089+
elif n:
10861090
output += '[%s, ..., %s]\n' % (self[0], self[-1])
10871091
output += 'length: %d' % n
10881092
return output

pandas/tseries/tests/test_period.py

+13-1
Original file line numberDiff line numberDiff line change
@@ -1672,7 +1672,19 @@ def test_asfreq(self):
16721672
def test_ts_repr(self):
16731673
index = PeriodIndex(freq='A', start='1/1/2001', end='12/31/2010')
16741674
ts = Series(np.random.randn(len(index)), index=index)
1675-
repr(ts)
1675+
repr(ts) # ??
1676+
1677+
val = period_range('2013Q1', periods=1, freq="Q")
1678+
expected = "<class 'pandas.tseries.period.PeriodIndex'>\nfreq: Q-DEC\n[2013Q1]\nlength: 1"
1679+
assert_equal(repr(val), expected)
1680+
1681+
val = period_range('2013Q1', periods=2, freq="Q")
1682+
expected = "<class 'pandas.tseries.period.PeriodIndex'>\nfreq: Q-DEC\n[2013Q1, 2013Q2]\nlength: 2"
1683+
assert_equal(repr(val), expected)
1684+
1685+
val = period_range('2013Q1', periods=3, freq="Q")
1686+
expected = "<class 'pandas.tseries.period.PeriodIndex'>\nfreq: Q-DEC\n[2013Q1, ..., 2013Q3]\nlength: 3"
1687+
assert_equal(repr(val), expected)
16761688

16771689
def test_period_index_unicode(self):
16781690
pi = PeriodIndex(freq='A', start='1/1/2001', end='12/1/2009')

0 commit comments

Comments
 (0)