Skip to content

Commit a12ffa0

Browse files
jbrockmendelJulianWgs
authored andcommitted
FMT: trim redundant freqstr from PeriodIndex __repr__ (pandas-dev#41653)
1 parent 1b390c6 commit a12ffa0

File tree

5 files changed

+22
-29
lines changed

5 files changed

+22
-29
lines changed

pandas/core/arrays/datetimes.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1119,14 +1119,14 @@ def to_period(self, freq=None) -> PeriodArray:
11191119
... "2000-08-31 00:00:00"]))
11201120
>>> df.index.to_period("M")
11211121
PeriodIndex(['2000-03', '2000-05', '2000-08'],
1122-
dtype='period[M]', freq='M')
1122+
dtype='period[M]')
11231123
11241124
Infer the daily frequency
11251125
11261126
>>> idx = pd.date_range("2017-01-01", periods=2)
11271127
>>> idx.to_period()
11281128
PeriodIndex(['2017-01-01', '2017-01-02'],
1129-
dtype='period[D]', freq='D')
1129+
dtype='period[D]')
11301130
"""
11311131
from pandas.core.arrays import PeriodArray
11321132

pandas/core/arrays/period.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -564,15 +564,15 @@ def asfreq(self, freq=None, how: str = "E") -> PeriodArray:
564564
>>> pidx = pd.period_range('2010-01-01', '2015-01-01', freq='A')
565565
>>> pidx
566566
PeriodIndex(['2010', '2011', '2012', '2013', '2014', '2015'],
567-
dtype='period[A-DEC]', freq='A-DEC')
567+
dtype='period[A-DEC]')
568568
569569
>>> pidx.asfreq('M')
570570
PeriodIndex(['2010-12', '2011-12', '2012-12', '2013-12', '2014-12',
571-
'2015-12'], dtype='period[M]', freq='M')
571+
'2015-12'], dtype='period[M]')
572572
573573
>>> pidx.asfreq('M', how='S')
574574
PeriodIndex(['2010-01', '2011-01', '2012-01', '2013-01', '2014-01',
575-
'2015-01'], dtype='period[M]', freq='M')
575+
'2015-01'], dtype='period[M]')
576576
"""
577577
how = libperiod.validate_end_alias(how)
578578

pandas/core/indexes/period.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -153,11 +153,11 @@ class PeriodIndex(DatetimeIndexOpsMixin):
153153
--------
154154
>>> idx = pd.PeriodIndex(year=[2000, 2002], quarter=[1, 3])
155155
>>> idx
156-
PeriodIndex(['2000Q1', '2002Q3'], dtype='period[Q-DEC]', freq='Q-DEC')
156+
PeriodIndex(['2000Q1', '2002Q3'], dtype='period[Q-DEC]')
157157
"""
158158

159159
_typ = "periodindex"
160-
_attributes = ["name", "freq"]
160+
_attributes = ["name"]
161161

162162
# define my properties & methods for delegation
163163
_is_numeric_dtype = False
@@ -636,7 +636,7 @@ def period_range(
636636
PeriodIndex(['2017-01', '2017-02', '2017-03', '2017-04', '2017-05', '2017-06',
637637
'2017-07', '2017-08', '2017-09', '2017-10', '2017-11', '2017-12',
638638
'2018-01'],
639-
dtype='period[M]', freq='M')
639+
dtype='period[M]')
640640
641641
If ``start`` or ``end`` are ``Period`` objects, they will be used as anchor
642642
endpoints for a ``PeriodIndex`` with frequency matching that of the
@@ -645,7 +645,7 @@ def period_range(
645645
>>> pd.period_range(start=pd.Period('2017Q1', freq='Q'),
646646
... end=pd.Period('2017Q2', freq='Q'), freq='M')
647647
PeriodIndex(['2017-03', '2017-04', '2017-05', '2017-06'],
648-
dtype='period[M]', freq='M')
648+
dtype='period[M]')
649649
"""
650650
if com.count_not_none(start, end, periods) != 2:
651651
raise ValueError(

pandas/tests/indexes/datetimelike.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,9 @@ def test_str(self, simple_index):
4444
if hasattr(idx, "tz"):
4545
if idx.tz is not None:
4646
assert idx.tz in str(idx)
47-
if hasattr(idx, "freq"):
47+
if isinstance(idx, pd.PeriodIndex):
48+
assert f"dtype='period[{idx.freqstr}]'" in str(idx)
49+
else:
4850
assert f"freq='{idx.freqstr}'" in str(idx)
4951

5052
def test_view(self, simple_index):

pandas/tests/indexes/period/test_formats.py

+10-19
Original file line numberDiff line numberDiff line change
@@ -62,40 +62,31 @@ def test_representation(self, method):
6262
idx9 = pd.period_range("2013Q1", periods=3, freq="Q")
6363
idx10 = PeriodIndex(["2011-01-01", "2011-02-01"], freq="3D")
6464

65-
exp1 = "PeriodIndex([], dtype='period[D]', freq='D')"
65+
exp1 = "PeriodIndex([], dtype='period[D]')"
6666

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

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

7171
exp4 = (
7272
"PeriodIndex(['2011-01-01', '2011-01-02', '2011-01-03'], "
73-
"dtype='period[D]', freq='D')"
73+
"dtype='period[D]')"
7474
)
7575

76-
exp5 = (
77-
"PeriodIndex(['2011', '2012', '2013'], dtype='period[A-DEC]', "
78-
"freq='A-DEC')"
79-
)
76+
exp5 = "PeriodIndex(['2011', '2012', '2013'], dtype='period[A-DEC]')"
8077

8178
exp6 = (
8279
"PeriodIndex(['2011-01-01 09:00', '2012-02-01 10:00', 'NaT'], "
83-
"dtype='period[H]', freq='H')"
80+
"dtype='period[H]')"
8481
)
8582

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

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

90-
exp9 = (
91-
"PeriodIndex(['2013Q1', '2013Q2', '2013Q3'], "
92-
"dtype='period[Q-DEC]', freq='Q-DEC')"
93-
)
87+
exp9 = "PeriodIndex(['2013Q1', '2013Q2', '2013Q3'], dtype='period[Q-DEC]')"
9488

95-
exp10 = (
96-
"PeriodIndex(['2011-01-01', '2011-02-01'], "
97-
"dtype='period[3D]', freq='3D')"
98-
)
89+
exp10 = "PeriodIndex(['2011-01-01', '2011-02-01'], dtype='period[3D]')"
9990

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

0 commit comments

Comments
 (0)