Skip to content

FMT: trim redundant freqstr from PeriodIndex __repr__ #41653

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 2 commits into from
May 25, 2021
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
4 changes: 2 additions & 2 deletions pandas/core/arrays/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -1119,14 +1119,14 @@ def to_period(self, freq=None) -> PeriodArray:
... "2000-08-31 00:00:00"]))
>>> df.index.to_period("M")
PeriodIndex(['2000-03', '2000-05', '2000-08'],
dtype='period[M]', freq='M')
dtype='period[M]')

Infer the daily frequency

>>> idx = pd.date_range("2017-01-01", periods=2)
>>> idx.to_period()
PeriodIndex(['2017-01-01', '2017-01-02'],
dtype='period[D]', freq='D')
dtype='period[D]')
"""
from pandas.core.arrays import PeriodArray

Expand Down
6 changes: 3 additions & 3 deletions pandas/core/arrays/period.py
Original file line number Diff line number Diff line change
Expand Up @@ -564,15 +564,15 @@ def asfreq(self, freq=None, how: str = "E") -> PeriodArray:
>>> pidx = pd.period_range('2010-01-01', '2015-01-01', freq='A')
>>> pidx
PeriodIndex(['2010', '2011', '2012', '2013', '2014', '2015'],
dtype='period[A-DEC]', freq='A-DEC')
dtype='period[A-DEC]')

>>> pidx.asfreq('M')
PeriodIndex(['2010-12', '2011-12', '2012-12', '2013-12', '2014-12',
'2015-12'], dtype='period[M]', freq='M')
'2015-12'], dtype='period[M]')

>>> pidx.asfreq('M', how='S')
PeriodIndex(['2010-01', '2011-01', '2012-01', '2013-01', '2014-01',
'2015-01'], dtype='period[M]', freq='M')
'2015-01'], dtype='period[M]')
"""
how = libperiod.validate_end_alias(how)

Expand Down
8 changes: 4 additions & 4 deletions pandas/core/indexes/period.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,11 +153,11 @@ class PeriodIndex(DatetimeIndexOpsMixin):
--------
>>> idx = pd.PeriodIndex(year=[2000, 2002], quarter=[1, 3])
>>> idx
PeriodIndex(['2000Q1', '2002Q3'], dtype='period[Q-DEC]', freq='Q-DEC')
PeriodIndex(['2000Q1', '2002Q3'], dtype='period[Q-DEC]')
"""

_typ = "periodindex"
_attributes = ["name", "freq"]
_attributes = ["name"]

# define my properties & methods for delegation
_is_numeric_dtype = False
Expand Down Expand Up @@ -636,7 +636,7 @@ def period_range(
PeriodIndex(['2017-01', '2017-02', '2017-03', '2017-04', '2017-05', '2017-06',
'2017-07', '2017-08', '2017-09', '2017-10', '2017-11', '2017-12',
'2018-01'],
dtype='period[M]', freq='M')
dtype='period[M]')

If ``start`` or ``end`` are ``Period`` objects, they will be used as anchor
endpoints for a ``PeriodIndex`` with frequency matching that of the
Expand All @@ -645,7 +645,7 @@ def period_range(
>>> pd.period_range(start=pd.Period('2017Q1', freq='Q'),
... end=pd.Period('2017Q2', freq='Q'), freq='M')
PeriodIndex(['2017-03', '2017-04', '2017-05', '2017-06'],
dtype='period[M]', freq='M')
dtype='period[M]')
"""
if com.count_not_none(start, end, periods) != 2:
raise ValueError(
Expand Down
4 changes: 3 additions & 1 deletion pandas/tests/indexes/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ def test_str(self, simple_index):
if hasattr(idx, "tz"):
if idx.tz is not None:
assert idx.tz in str(idx)
if hasattr(idx, "freq"):
if isinstance(idx, pd.PeriodIndex):
assert f"dtype='period[{idx.freqstr}]'" in str(idx)
else:
assert f"freq='{idx.freqstr}'" in str(idx)

def test_view(self, simple_index):
Expand Down
29 changes: 10 additions & 19 deletions pandas/tests/indexes/period/test_formats.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,40 +62,31 @@ def test_representation(self, method):
idx9 = pd.period_range("2013Q1", periods=3, freq="Q")
idx10 = PeriodIndex(["2011-01-01", "2011-02-01"], freq="3D")

exp1 = "PeriodIndex([], dtype='period[D]', freq='D')"
exp1 = "PeriodIndex([], dtype='period[D]')"

exp2 = "PeriodIndex(['2011-01-01'], dtype='period[D]', freq='D')"
exp2 = "PeriodIndex(['2011-01-01'], dtype='period[D]')"

exp3 = "PeriodIndex(['2011-01-01', '2011-01-02'], dtype='period[D]', freq='D')"
exp3 = "PeriodIndex(['2011-01-01', '2011-01-02'], dtype='period[D]')"

exp4 = (
"PeriodIndex(['2011-01-01', '2011-01-02', '2011-01-03'], "
"dtype='period[D]', freq='D')"
"dtype='period[D]')"
)

exp5 = (
"PeriodIndex(['2011', '2012', '2013'], dtype='period[A-DEC]', "
"freq='A-DEC')"
)
exp5 = "PeriodIndex(['2011', '2012', '2013'], dtype='period[A-DEC]')"

exp6 = (
"PeriodIndex(['2011-01-01 09:00', '2012-02-01 10:00', 'NaT'], "
"dtype='period[H]', freq='H')"
"dtype='period[H]')"
)

exp7 = "PeriodIndex(['2013Q1'], dtype='period[Q-DEC]', freq='Q-DEC')"
exp7 = "PeriodIndex(['2013Q1'], dtype='period[Q-DEC]')"

exp8 = "PeriodIndex(['2013Q1', '2013Q2'], dtype='period[Q-DEC]', freq='Q-DEC')"
exp8 = "PeriodIndex(['2013Q1', '2013Q2'], dtype='period[Q-DEC]')"

exp9 = (
"PeriodIndex(['2013Q1', '2013Q2', '2013Q3'], "
"dtype='period[Q-DEC]', freq='Q-DEC')"
)
exp9 = "PeriodIndex(['2013Q1', '2013Q2', '2013Q3'], dtype='period[Q-DEC]')"

exp10 = (
"PeriodIndex(['2011-01-01', '2011-02-01'], "
"dtype='period[3D]', freq='3D')"
)
exp10 = "PeriodIndex(['2011-01-01', '2011-02-01'], dtype='period[3D]')"

for idx, expected in zip(
[idx1, idx2, idx3, idx4, idx5, idx6, idx7, idx8, idx9, idx10],
Expand Down