Skip to content

BUG: repr of Periods in a Series is broken #10974

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 3, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pandas/core/format.py
Original file line number Diff line number Diff line change
Expand Up @@ -2106,7 +2106,7 @@ def _format_strings(self):
class PeriodArrayFormatter(IntArrayFormatter):

def _format_strings(self):
values = np.array(self.values.to_native_types(), dtype=object)
values = PeriodIndex(self.values).to_native_types()
formatter = self.formatter or (lambda x: '%s' % x)
fmt_values = [formatter(x) for x in values]
return fmt_values
Expand Down
131 changes: 131 additions & 0 deletions pandas/tseries/tests/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,51 @@ def test_representation(self):
result = getattr(idx, func)()
self.assertEqual(result, expected)

def test_representation_to_series(self):
idx1 = DatetimeIndex([], freq='D')
idx2 = DatetimeIndex(['2011-01-01'], freq='D')
idx3 = DatetimeIndex(['2011-01-01', '2011-01-02'], freq='D')
idx4 = DatetimeIndex(['2011-01-01', '2011-01-02', '2011-01-03'], freq='D')
idx5 = DatetimeIndex(['2011-01-01 09:00', '2011-01-01 10:00', '2011-01-01 11:00'],
freq='H', tz='Asia/Tokyo')
idx6 = DatetimeIndex(['2011-01-01 09:00', '2011-01-01 10:00', pd.NaT],
tz='US/Eastern')
idx7 = DatetimeIndex(['2011-01-01 09:00', '2011-01-02 10:15'])

exp1 = """Series([], dtype: datetime64[ns])"""

exp2 = """0 2011-01-01
dtype: datetime64[ns]"""

exp3 = """0 2011-01-01
1 2011-01-02
dtype: datetime64[ns]"""

exp4 = """0 2011-01-01
1 2011-01-02
2 2011-01-03
dtype: datetime64[ns]"""

exp5 = """0 2011-01-01 09:00:00+09:00
1 2011-01-01 10:00:00+09:00
2 2011-01-01 11:00:00+09:00
dtype: object"""

exp6 = """0 2011-01-01 09:00:00-05:00
1 2011-01-01 10:00:00-05:00
2 NaN
dtype: object"""

exp7 = """0 2011-01-01 09:00:00
1 2011-01-02 10:15:00
dtype: datetime64[ns]"""

with pd.option_context('display.width', 300):
for idx, expected in zip([idx1, idx2, idx3, idx4, idx5, idx6, idx7],
[exp1, exp2, exp3, exp4, exp5, exp6, exp7]):
result = repr(Series(idx))
self.assertEqual(result, expected)

def test_summary(self):
# GH9116
idx1 = DatetimeIndex([], freq='D')
Expand Down Expand Up @@ -536,6 +581,38 @@ def test_representation(self):
result = getattr(idx, func)()
self.assertEqual(result, expected)

def test_representation_to_series(self):
idx1 = TimedeltaIndex([], freq='D')
idx2 = TimedeltaIndex(['1 days'], freq='D')
idx3 = TimedeltaIndex(['1 days', '2 days'], freq='D')
idx4 = TimedeltaIndex(['1 days', '2 days', '3 days'], freq='D')
idx5 = TimedeltaIndex(['1 days 00:00:01', '2 days', '3 days'])

exp1 = """Series([], dtype: timedelta64[ns])"""

exp2 = """0 1 days
dtype: timedelta64[ns]"""

exp3 = """0 1 days
1 2 days
dtype: timedelta64[ns]"""

exp4 = """0 1 days
1 2 days
2 3 days
dtype: timedelta64[ns]"""

exp5 = """0 1 days 00:00:01
1 2 days 00:00:00
2 3 days 00:00:00
dtype: timedelta64[ns]"""

with pd.option_context('display.width',300):
for idx, expected in zip([idx1, idx2, idx3, idx4, idx5],
[exp1, exp2, exp3, exp4, exp5]):
result = repr(pd.Series(idx))
self.assertEqual(result, expected)

def test_summary(self):
# GH9116
idx1 = TimedeltaIndex([], freq='D')
Expand Down Expand Up @@ -1145,6 +1222,60 @@ def test_representation(self):
result = getattr(idx, func)()
self.assertEqual(result, expected)

def test_representation_to_series(self):
# GH 10971
idx1 = PeriodIndex([], freq='D')
idx2 = PeriodIndex(['2011-01-01'], freq='D')
idx3 = PeriodIndex(['2011-01-01', '2011-01-02'], freq='D')
idx4 = PeriodIndex(['2011-01-01', '2011-01-02', '2011-01-03'], freq='D')
idx5 = PeriodIndex(['2011', '2012', '2013'], freq='A')
idx6 = PeriodIndex(['2011-01-01 09:00', '2012-02-01 10:00', 'NaT'], freq='H')

idx7 = pd.period_range('2013Q1', periods=1, freq="Q")
idx8 = pd.period_range('2013Q1', periods=2, freq="Q")
idx9 = pd.period_range('2013Q1', periods=3, freq="Q")

exp1 = """Series([], dtype: object)"""

exp2 = """0 2011-01-01
dtype: object"""

exp3 = """0 2011-01-01
1 2011-01-02
dtype: object"""

exp4 = """0 2011-01-01
1 2011-01-02
2 2011-01-03
dtype: object"""

exp5 = """0 2011
1 2012
2 2013
dtype: object"""

exp6 = """0 2011-01-01 09:00
1 2012-02-01 10:00
2 NaT
dtype: object"""

exp7 = """0 2013Q1
dtype: object"""

exp8 = """0 2013Q1
1 2013Q2
dtype: object"""

exp9 = """0 2013Q1
1 2013Q2
2 2013Q3
dtype: object"""

for idx, expected in zip([idx1, idx2, idx3, idx4, idx5, idx6, idx7, idx8, idx9],
[exp1, exp2, exp3, exp4, exp5, exp6, exp7, exp8, exp9]):
result = repr(pd.Series(idx))
self.assertEqual(result, expected)

def test_summary(self):
# GH9116
idx1 = PeriodIndex([], freq='D')
Expand Down