Skip to content

Commit 6a53880

Browse files
committed
ENH: to_period converts BQS,QS,BQ to Q and similarly for annual
1 parent 6876725 commit 6a53880

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

pandas/tseries/index.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
from pandas.core.common import isnull
99
from pandas.core.index import Index, Int64Index
10-
from pandas.tseries.frequencies import infer_freq, to_offset
10+
from pandas.tseries.frequencies import infer_freq, to_offset, get_period_alias
1111
from pandas.tseries.offsets import DateOffset, generate_range, Tick
1212
from pandas.tseries.tools import parse_time_string, normalize_date
1313
from pandas.util.decorators import cache_readonly
@@ -683,7 +683,7 @@ def to_period(self, freq=None):
683683
raise ValueError(msg)
684684

685685
if freq is None:
686-
freq = self.freqstr
686+
freq = get_period_alias(self.freqstr)
687687

688688
return PeriodIndex(self.values, freq=freq)
689689

pandas/tseries/tests/test_period.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1568,6 +1568,27 @@ def test_to_period_quarterly(self):
15681568
result = stamps.to_period(freq)
15691569
self.assert_(rng.equals(result))
15701570

1571+
def test_to_period_quarterlyish(self):
1572+
offsets = ['BQ', 'QS', 'BQS']
1573+
for off in offsets:
1574+
rng = date_range('01-Jan-2012', periods=8, freq=off)
1575+
prng = rng.to_period()
1576+
self.assert_(prng.freq == 'Q-DEC')
1577+
1578+
def test_to_period_annualish(self):
1579+
offsets = ['BA', 'AS', 'BAS']
1580+
for off in offsets:
1581+
rng = date_range('01-Jan-2012', periods=8, freq=off)
1582+
prng = rng.to_period()
1583+
self.assert_(prng.freq == 'A-DEC')
1584+
1585+
def test_to_period_monthish(self):
1586+
offsets = ['MS', 'EOM', 'BM']
1587+
for off in offsets:
1588+
rng = date_range('01-Jan-2012', periods=8, freq=off)
1589+
prng = rng.to_period()
1590+
self.assert_(prng.freq == 'M')
1591+
15711592
def test_no_multiples(self):
15721593
self.assertRaises(ValueError, period_range, '1989Q3', periods=10,
15731594
freq='2Q')

0 commit comments

Comments
 (0)