Skip to content

Commit c47f350

Browse files
committed
BUG: repr of Periods in a Series is broken
1 parent 7ca2190 commit c47f350

File tree

2 files changed

+132
-1
lines changed

2 files changed

+132
-1
lines changed

pandas/core/format.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2106,7 +2106,7 @@ def _format_strings(self):
21062106
class PeriodArrayFormatter(IntArrayFormatter):
21072107

21082108
def _format_strings(self):
2109-
values = np.array(self.values.to_native_types(), dtype=object)
2109+
values = PeriodIndex(self.values).to_native_types()
21102110
formatter = self.formatter or (lambda x: '%s' % x)
21112111
fmt_values = [formatter(x) for x in values]
21122112
return fmt_values

pandas/tseries/tests/test_base.py

+131
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,51 @@ def test_representation(self):
139139
result = getattr(idx, func)()
140140
self.assertEqual(result, expected)
141141

142+
def test_representation_to_series(self):
143+
idx1 = DatetimeIndex([], freq='D')
144+
idx2 = DatetimeIndex(['2011-01-01'], freq='D')
145+
idx3 = DatetimeIndex(['2011-01-01', '2011-01-02'], freq='D')
146+
idx4 = DatetimeIndex(['2011-01-01', '2011-01-02', '2011-01-03'], freq='D')
147+
idx5 = DatetimeIndex(['2011-01-01 09:00', '2011-01-01 10:00', '2011-01-01 11:00'],
148+
freq='H', tz='Asia/Tokyo')
149+
idx6 = DatetimeIndex(['2011-01-01 09:00', '2011-01-01 10:00', pd.NaT],
150+
tz='US/Eastern')
151+
idx7 = DatetimeIndex(['2011-01-01 09:00', '2011-01-02 10:15'])
152+
153+
exp1 = """Series([], dtype: datetime64[ns])"""
154+
155+
exp2 = """0 2011-01-01
156+
dtype: datetime64[ns]"""
157+
158+
exp3 = """0 2011-01-01
159+
1 2011-01-02
160+
dtype: datetime64[ns]"""
161+
162+
exp4 = """0 2011-01-01
163+
1 2011-01-02
164+
2 2011-01-03
165+
dtype: datetime64[ns]"""
166+
167+
exp5 = """0 2011-01-01 09:00:00+09:00
168+
1 2011-01-01 10:00:00+09:00
169+
2 2011-01-01 11:00:00+09:00
170+
dtype: object"""
171+
172+
exp6 = """0 2011-01-01 09:00:00-05:00
173+
1 2011-01-01 10:00:00-05:00
174+
2 NaN
175+
dtype: object"""
176+
177+
exp7 = """0 2011-01-01 09:00:00
178+
1 2011-01-02 10:15:00
179+
dtype: datetime64[ns]"""
180+
181+
with pd.option_context('display.width', 300):
182+
for idx, expected in zip([idx1, idx2, idx3, idx4, idx5, idx6, idx7],
183+
[exp1, exp2, exp3, exp4, exp5, exp6, exp7]):
184+
result = repr(Series(idx))
185+
self.assertEqual(result, expected)
186+
142187
def test_summary(self):
143188
# GH9116
144189
idx1 = DatetimeIndex([], freq='D')
@@ -536,6 +581,38 @@ def test_representation(self):
536581
result = getattr(idx, func)()
537582
self.assertEqual(result, expected)
538583

584+
def test_representation_to_series(self):
585+
idx1 = TimedeltaIndex([], freq='D')
586+
idx2 = TimedeltaIndex(['1 days'], freq='D')
587+
idx3 = TimedeltaIndex(['1 days', '2 days'], freq='D')
588+
idx4 = TimedeltaIndex(['1 days', '2 days', '3 days'], freq='D')
589+
idx5 = TimedeltaIndex(['1 days 00:00:01', '2 days', '3 days'])
590+
591+
exp1 = """Series([], dtype: timedelta64[ns])"""
592+
593+
exp2 = """0 1 days
594+
dtype: timedelta64[ns]"""
595+
596+
exp3 = """0 1 days
597+
1 2 days
598+
dtype: timedelta64[ns]"""
599+
600+
exp4 = """0 1 days
601+
1 2 days
602+
2 3 days
603+
dtype: timedelta64[ns]"""
604+
605+
exp5 = """0 1 days 00:00:01
606+
1 2 days 00:00:00
607+
2 3 days 00:00:00
608+
dtype: timedelta64[ns]"""
609+
610+
with pd.option_context('display.width',300):
611+
for idx, expected in zip([idx1, idx2, idx3, idx4, idx5],
612+
[exp1, exp2, exp3, exp4, exp5]):
613+
result = repr(pd.Series(idx))
614+
self.assertEqual(result, expected)
615+
539616
def test_summary(self):
540617
# GH9116
541618
idx1 = TimedeltaIndex([], freq='D')
@@ -1145,6 +1222,60 @@ def test_representation(self):
11451222
result = getattr(idx, func)()
11461223
self.assertEqual(result, expected)
11471224

1225+
def test_representation_to_series(self):
1226+
# GH 10971
1227+
idx1 = PeriodIndex([], freq='D')
1228+
idx2 = PeriodIndex(['2011-01-01'], freq='D')
1229+
idx3 = PeriodIndex(['2011-01-01', '2011-01-02'], freq='D')
1230+
idx4 = PeriodIndex(['2011-01-01', '2011-01-02', '2011-01-03'], freq='D')
1231+
idx5 = PeriodIndex(['2011', '2012', '2013'], freq='A')
1232+
idx6 = PeriodIndex(['2011-01-01 09:00', '2012-02-01 10:00', 'NaT'], freq='H')
1233+
1234+
idx7 = pd.period_range('2013Q1', periods=1, freq="Q")
1235+
idx8 = pd.period_range('2013Q1', periods=2, freq="Q")
1236+
idx9 = pd.period_range('2013Q1', periods=3, freq="Q")
1237+
1238+
exp1 = """Series([], dtype: object)"""
1239+
1240+
exp2 = """0 2011-01-01
1241+
dtype: object"""
1242+
1243+
exp3 = """0 2011-01-01
1244+
1 2011-01-02
1245+
dtype: object"""
1246+
1247+
exp4 = """0 2011-01-01
1248+
1 2011-01-02
1249+
2 2011-01-03
1250+
dtype: object"""
1251+
1252+
exp5 = """0 2011
1253+
1 2012
1254+
2 2013
1255+
dtype: object"""
1256+
1257+
exp6 = """0 2011-01-01 09:00
1258+
1 2012-02-01 10:00
1259+
2 NaT
1260+
dtype: object"""
1261+
1262+
exp7 = """0 2013Q1
1263+
dtype: object"""
1264+
1265+
exp8 = """0 2013Q1
1266+
1 2013Q2
1267+
dtype: object"""
1268+
1269+
exp9 = """0 2013Q1
1270+
1 2013Q2
1271+
2 2013Q3
1272+
dtype: object"""
1273+
1274+
for idx, expected in zip([idx1, idx2, idx3, idx4, idx5, idx6, idx7, idx8, idx9],
1275+
[exp1, exp2, exp3, exp4, exp5, exp6, exp7, exp8, exp9]):
1276+
result = repr(pd.Series(idx))
1277+
self.assertEqual(result, expected)
1278+
11481279
def test_summary(self):
11491280
# GH9116
11501281
idx1 = PeriodIndex([], freq='D')

0 commit comments

Comments
 (0)