import pytest
from pandas._libs.tslibs.frequencies import (
INVALID_FREQ_ERR_MSG, _period_code_map)
from pandas.errors import OutOfBoundsDatetime
from pandas import Period, offsets
class TestFreqConversion(object):
"""Test frequency conversion of date objects"""
@pytest.mark.parametrize('freq', ['A', 'Q', 'M', 'W', 'B', 'D'])
def test_asfreq_near_zero(self, freq):
# GH#19643, GH#19650
per = Period('0001-01-01', freq=freq)
tup1 = (per.year, per.hour, per.day)
prev = per - 1
assert prev.ordinal == per.ordinal - 1
tup2 = (prev.year, prev.month, prev.day)
assert tup2 < tup1
def test_asfreq_near_zero_weekly(self):
# GH#19834
per1 = Period('0001-01-01', 'D') + 6
per2 = Period('0001-01-01', 'D') - 6
week1 = per1.asfreq('W')
week2 = per2.asfreq('W')
assert week1 != week2
assert week1.asfreq('D', 'E') >= per1
assert week2.asfreq('D', 'S') <= per2
@pytest.mark.xfail(reason='GH#19643 period_helper asfreq functions fail '
'to check for overflows')
def test_to_timestamp_out_of_bounds(self):
# GH#19643, currently gives Timestamp('1754-08-30 22:43:41.128654848')
per = Period('0001-01-01', freq='B')
with pytest.raises(OutOfBoundsDatetime):
per.to_timestamp()
def test_asfreq_corner(self):
val = Period(freq='A', year=2007)
result1 = val.asfreq('5t')
result2 = val.asfreq('t')
expected = Period('2007-12-31 23:59', freq='t')
assert result1.ordinal == expected.ordinal
assert result1.freqstr == '5T'
assert result2.ordinal == expected.ordinal
assert result2.freqstr == 'T'
def test_conv_annual(self):
# frequency conversion tests: from Annual Frequency
ival_A = Period(freq='A', year=2007)
ival_AJAN = Period(freq="A-JAN", year=2007)
ival_AJUN = Period(freq="A-JUN", year=2007)
ival_ANOV = Period(freq="A-NOV", year=2007)
ival_A_to_Q_start = Period(freq='Q', year=2007, quarter=1)
ival_A_to_Q_end = Period(freq='Q', year=2007, quarter=4)
ival_A_to_M_start = Period(freq='M', year=2007, month=1)
ival_A_to_M_end = Period(freq='M', year=2007, month=12)
ival_A_to_W_start = Period(freq='W', year=2007, month=1, day=1)
ival_A_to_W_end = Period(freq='W', year=2007, month=12, day=31)
ival_A_to_B_start = Period(freq='B', year=2007, month=1, day=1)
ival_A_to_B_end = Period(freq='B', year=2007, month=12, day=31)
ival_A_to_D_start = Period(freq='D', year=2007, month=1, day=1)
ival_A_to_D_end = Period(freq='D', year=2007, month=12, day=31)
ival_A_to_H_start = Period(freq='H', year=2007, month=1, day=1, hour=0)
ival_A_to_H_end = Period(freq='H', year=2007, month=12, day=31,
hour=23)
ival_A_to_T_start = Period(freq='Min', year=2007, month=1, day=1,
hour=0, minute=0)
ival_A_to_T_end = Period(freq='Min', year=2007, month=12, day=31,
hour=23, minute=59)
ival_A_to_S_start = Period(freq='S', year=2007, month=1, day=1, hour=0,
minute=0, second=0)
ival_A_to_S_end = Period(freq='S', year=2007, month=12, day=31,
hour=23, minute=59, second=59)
ival_AJAN_to_D_end = Period(freq='D', year=2007, month=1, day=31)
ival_AJAN_to_D_start = Period(freq='D', year=2006, month=2, day=1)
ival_AJUN_to_D_end = Period(freq='D', year=2007, month=6, day=30)
ival_AJUN_to_D_start = Period(freq='D', year=2006, month=7, day=1)
ival_ANOV_to_D_end = Period(freq='D', year=2007, month=11, day=30)
ival_ANOV_to_D_start = Period(freq='D', year=2006, month=12, day=1)
assert ival_A.asfreq('Q', 'S') == ival_A_to_Q_start
assert ival_A.asfreq('Q', 'e') == ival_A_to_Q_end
assert ival_A.asfreq('M', 's') == ival_A_to_M_start
assert ival_A.asfreq('M', 'E') == ival_A_to_M_end
assert ival_A.asfreq('W', 'S') == ival_A_to_W_start
assert ival_A.asfreq('W', 'E') == ival_A_to_W_end
assert ival_A.asfreq('B', 'S') == ival_A_to_B_start
assert ival_A.asfreq('B', 'E') == ival_A_to_B_end
assert ival_A.asfreq('D', 'S') == ival_A_to_D_start
assert ival_A.asfreq('D', 'E') == ival_A_to_D_end
assert ival_A.asfreq('H', 'S') == ival_A_to_H_start
assert ival_A.asfreq('H', 'E') == ival_A_to_H_end
assert ival_A.asfreq('min', 'S') == ival_A_to_T_start
assert ival_A.asfreq('min', 'E') == ival_A_to_T_end
assert ival_A.asfreq('T', 'S') == ival_A_to_T_start
assert ival_A.asfreq('T', 'E') == ival_A_to_T_end
assert ival_A.asfreq('S', 'S') == ival_A_to_S_start
assert ival_A.asfreq('S', 'E') == ival_A_to_S_end
assert ival_AJAN.asfreq('D', 'S') == ival_AJAN_to_D_start
assert ival_AJAN.asfreq('D', 'E') == ival_AJAN_to_D_end
assert ival_AJUN.asfreq('D', 'S') == ival_AJUN_to_D_start
assert ival_AJUN.asfreq('D', 'E') == ival_AJUN_to_D_end
assert ival_ANOV.asfreq('D', 'S') == ival_ANOV_to_D_start
assert ival_ANOV.asfreq('D', 'E') == ival_ANOV_to_D_end
assert ival_A.asfreq('A') == ival_A
def test_conv_quarterly(self):
# frequency conversion tests: from Quarterly Frequency
ival_Q = Period(freq='Q', year=2007, quarter=1)
ival_Q_end_of_year = Period(freq='Q', year=2007, quarter=4)
ival_QEJAN = Period(freq="Q-JAN", year=2007, quarter=1)
ival_QEJUN = Period(freq="Q-JUN", year=2007, quarter=1)
ival_Q_to_A = Period(freq='A', year=2007)
ival_Q_to_M_start = Period(freq='M', year=2007, month=1)
ival_Q_to_M_end = Period(freq='M', year=2007, month=3)
ival_Q_to_W_start = Period(freq='W', year=2007, month=1, day=1)
ival_Q_to_W_end = Period(freq='W', year=2007, month=3, day=31)
ival_Q_to_B_start = Period(freq='B', year=2007, month=1, day=1)
ival_Q_to_B_end = Period(freq='B', year=2007, month=3, day=30)
ival_Q_to_D_start = Period(freq='D', year=2007, month=1, day=1)
ival_Q_to_D_end = Period(freq='D', year=2007, month=3, day=31)
ival_Q_to_H_start = Period(freq='H', year=2007, month=1, day=1, hour=0)
ival_Q_to_H_end = Period(freq='H', year=2007, month=3, day=31, hour=23)
ival_Q_to_T_start = Period(freq='Min', year=2007, month=1, day=1,
hour=0, minute=0)
ival_Q_to_T_end = Period(freq='Min', year=2007, month=3, day=31,
hour=23, minute=59)
ival_Q_to_S_start = Period(freq='S', year=2007, month=1, day=1, hour=0,
minute=0, second=0)
ival_Q_to_S_end = Period(freq='S', year=2007, month=3, day=31, hour=23,
minute=59, second=59)
ival_QEJAN_to_D_start = Period(freq='D', year=2006, month=2, day=1)
ival_QEJAN_to_D_end = Period(freq='D', year=2006, month=4, day=30)
ival_QEJUN_to_D_start = Period(freq='D', year=2006, month=7, day=1)
ival_QEJUN_to_D_end = Period(freq='D', year=2006, month=9, day=30)
assert ival_Q.asfreq('A') == ival_Q_to_A
assert ival_Q_end_of_year.asfreq('A') == ival_Q_to_A
assert ival_Q.asfreq('M', 'S') == ival_Q_to_M_start
assert ival_Q.asfreq('M', 'E') == ival_Q_to_M_end
assert ival_Q.asfreq('W', 'S') == ival_Q_to_W_start
assert ival_Q.asfreq('W', 'E') == ival_Q_to_W_end
assert ival_Q.asfreq('B', 'S') == ival_Q_to_B_start
assert ival_Q.asfreq('B', 'E') == ival_Q_to_B_end
assert ival_Q.asfreq('D', 'S') == ival_Q_to_D_start
assert ival_Q.asfreq('D', 'E') == ival_Q_to_D_end
assert ival_Q.asfreq('H', 'S') == ival_Q_to_H_start
assert ival_Q.asfreq('H', 'E') == ival_Q_to_H_end
assert ival_Q.asfreq('Min', 'S') == ival_Q_to_T_start
assert ival_Q.asfreq('Min', 'E') == ival_Q_to_T_end
assert ival_Q.asfreq('S', 'S') == ival_Q_to_S_start
assert ival_Q.asfreq('S', 'E') == ival_Q_to_S_end
assert ival_QEJAN.asfreq('D', 'S') == ival_QEJAN_to_D_start
assert ival_QEJAN.asfreq('D', 'E') == ival_QEJAN_to_D_end
assert ival_QEJUN.asfreq('D', 'S') == ival_QEJUN_to_D_start
assert ival_QEJUN.asfreq('D', 'E') == ival_QEJUN_to_D_end
assert ival_Q.asfreq('Q') == ival_Q
def test_conv_monthly(self):
# frequency conversion tests: from Monthly Frequency
ival_M = Period(freq='M', year=2007, month=1)
ival_M_end_of_year = Period(freq='M', year=2007, month=12)
ival_M_end_of_quarter = Period(freq='M', year=2007, month=3)
ival_M_to_A = Period(freq='A', year=2007)
ival_M_to_Q = Period(freq='Q', year=2007, quarter=1)
ival_M_to_W_start = Period(freq='W', year=2007, month=1, day=1)
ival_M_to_W_end = Period(freq='W', year=2007, month=1, day=31)
ival_M_to_B_start = Period(freq='B', year=2007, month=1, day=1)
ival_M_to_B_end = Period(freq='B', year=2007, month=1, day=31)
ival_M_to_D_start = Period(freq='D', year=2007, month=1, day=1)
ival_M_to_D_end = Period(freq='D', year=2007, month=1, day=31)
ival_M_to_H_start = Period(freq='H', year=2007, month=1, day=1, hour=0)
ival_M_to_H_end = Period(freq='H', year=2007, month=1, day=31, hour=23)
ival_M_to_T_start = Period(freq='Min', year=2007, month=1, day=1,
hour=0, minute=0)
ival_M_to_T_end = Period(freq='Min', year=2007, month=1, day=31,
hour=23, minute=59)
ival_M_to_S_start = Period(freq='S', year=2007, month=1, day=1, hour=0,
minute=0, second=0)
ival_M_to_S_end = Period(freq='S', year=2007, month=1, day=31, hour=23,
minute=59, second=59)
assert ival_M.asfreq('A') == ival_M_to_A
assert ival_M_end_of_year.asfreq('A') == ival_M_to_A
assert ival_M.asfreq('Q') == ival_M_to_Q
assert ival_M_end_of_quarter.asfreq('Q') == ival_M_to_Q
assert ival_M.asfreq('W', 'S') == ival_M_to_W_start
assert ival_M.asfreq('W', 'E') == ival_M_to_W_end
assert ival_M.asfreq('B', 'S') == ival_M_to_B_start
assert ival_M.asfreq('B', 'E') == ival_M_to_B_end
assert ival_M.asfreq('D', 'S') == ival_M_to_D_start
assert ival_M.asfreq('D', 'E') == ival_M_to_D_end
assert ival_M.asfreq('H', 'S') == ival_M_to_H_start
assert ival_M.asfreq('H', 'E') == ival_M_to_H_end
assert ival_M.asfreq('Min', 'S') == ival_M_to_T_start
assert ival_M.asfreq('Min', 'E') == ival_M_to_T_end
assert ival_M.asfreq('S', 'S') == ival_M_to_S_start
assert ival_M.asfreq('S', 'E') == ival_M_to_S_end
assert ival_M.asfreq('M') == ival_M
def test_conv_weekly(self):
# frequency conversion tests: from Weekly Frequency
ival_W = Period(freq='W', year=2007, month=1, day=1)
ival_WSUN = Period(freq='W', year=2007, month=1, day=7)
ival_WSAT = Period(freq='W-SAT', year=2007, month=1, day=6)
ival_WFRI = Period(freq='W-FRI', year=2007, month=1, day=5)
ival_WTHU = Period(freq='W-THU', year=2007, month=1, day=4)
ival_WWED = Period(freq='W-WED', year=2007, month=1, day=3)
ival_WTUE = Period(freq='W-TUE', year=2007, month=1, day=2)
ival_WMON = Period(freq='W-MON', year=2007, month=1, day=1)
ival_WSUN_to_D_start = Period(freq='D', year=2007, month=1, day=1)
ival_WSUN_to_D_end = Period(freq='D', year=2007, month=1, day=7)
ival_WSAT_to_D_start = Period(freq='D', year=2006, month=12, day=31)
ival_WSAT_to_D_end = Period(freq='D', year=2007, month=1, day=6)
ival_WFRI_to_D_start = Period(freq='D', year=2006, month=12, day=30)
ival_WFRI_to_D_end = Period(freq='D', year=2007, month=1, day=5)
ival_WTHU_to_D_start = Period(freq='D', year=2006, month=12, day=29)
ival_WTHU_to_D_end = Period(freq='D', year=2007, month=1, day=4)
ival_WWED_to_D_start = Period(freq='D', year=2006, month=12, day=28)
ival_WWED_to_D_end = Period(freq='D', year=2007, month=1, day=3)
ival_WTUE_to_D_start = Period(freq='D', year=2006, month=12, day=27)
ival_WTUE_to_D_end = Period(freq='D', year=2007, month=1, day=2)
ival_WMON_to_D_start = Period(freq='D', year=2006, month=12, day=26)
ival_WMON_to_D_end = Period(freq='D', year=2007, month=1, day=1)
ival_W_end_of_year = Period(freq='W', year=2007, month=12, day=31)
ival_W_end_of_quarter = Period(freq='W', year=2007, month=3, day=31)
ival_W_end_of_month = Period(freq='W', year=2007, month=1, day=31)
ival_W_to_A = Period(freq='A', year=2007)
ival_W_to_Q = Period(freq='Q', year=2007, quarter=1)
ival_W_to_M = Period(freq='M', year=2007, month=1)
if Period(freq='D', year=2007, month=12, day=31).weekday == 6:
ival_W_to_A_end_of_year = Period(freq='A', year=2007)
else:
ival_W_to_A_end_of_year = Period(freq='A', year=2008)
if Period(freq='D', year=2007, month=3, day=31).weekday == 6:
ival_W_to_Q_end_of_quarter = Period(freq='Q', year=2007, quarter=1)
else:
ival_W_to_Q_end_of_quarter = Period(freq='Q', year=2007, quarter=2)
if Period(freq='D', year=2007, month=1, day=31).weekday == 6:
ival_W_to_M_end_of_month = Period(freq='M', year=2007, month=1)
else:
ival_W_to_M_end_of_month = Period(freq='M', year=2007, month=2)
ival_W_to_B_start = Period(freq='B', year=2007, month=1, day=1)
ival_W_to_B_end = Period(freq='B', year=2007, month=1, day=5)
ival_W_to_D_start = Period(freq='D', year=2007, month=1, day=1)
ival_W_to_D_end = Period(freq='D', year=2007, month=1, day=7)
ival_W_to_H_start = Period(freq='H', year=2007, month=1, day=1, hour=0)
ival_W_to_H_end = Period(freq='H', year=2007, month=1, day=7, hour=23)
ival_W_to_T_start = Period(freq='Min', year=2007, month=1, day=1,
hour=0, minute=0)
ival_W_to_T_end = Period(freq='Min', year=2007, month=1, day=7,
hour=23, minute=59)
ival_W_to_S_start = Period(freq='S', year=2007, month=1, day=1, hour=0,
minute=0, second=0)
ival_W_to_S_end = Period(freq='S', year=2007, month=1, day=7, hour=23,
minute=59, second=59)
assert ival_W.asfreq('A') == ival_W_to_A
assert ival_W_end_of_year.asfreq('A') == ival_W_to_A_end_of_year
assert ival_W.asfreq('Q') == ival_W_to_Q
assert ival_W_end_of_quarter.asfreq('Q') == ival_W_to_Q_end_of_quarter
assert ival_W.asfreq('M') == ival_W_to_M
assert ival_W_end_of_month.asfreq('M') == ival_W_to_M_end_of_month
assert ival_W.asfreq('B', 'S') == ival_W_to_B_start
assert ival_W.asfreq('B', 'E') == ival_W_to_B_end
assert ival_W.asfreq('D', 'S') == ival_W_to_D_start
assert ival_W.asfreq('D', 'E') == ival_W_to_D_end
assert ival_WSUN.asfreq('D', 'S') == ival_WSUN_to_D_start
assert ival_WSUN.asfreq('D', 'E') == ival_WSUN_to_D_end
assert ival_WSAT.asfreq('D', 'S') == ival_WSAT_to_D_start
assert ival_WSAT.asfreq('D', 'E') == ival_WSAT_to_D_end
assert ival_WFRI.asfreq('D', 'S') == ival_WFRI_to_D_start
assert ival_WFRI.asfreq('D', 'E') == ival_WFRI_to_D_end
assert ival_WTHU.asfreq('D', 'S') == ival_WTHU_to_D_start
assert ival_WTHU.asfreq('D', 'E') == ival_WTHU_to_D_end
assert ival_WWED.asfreq('D', 'S') == ival_WWED_to_D_start
assert ival_WWED.asfreq('D', 'E') == ival_WWED_to_D_end
assert ival_WTUE.asfreq('D', 'S') == ival_WTUE_to_D_start
assert ival_WTUE.asfreq('D', 'E') == ival_WTUE_to_D_end
assert ival_WMON.asfreq('D', 'S') == ival_WMON_to_D_start
assert ival_WMON.asfreq('D', 'E') == ival_WMON_to_D_end
assert ival_W.asfreq('H', 'S') == ival_W_to_H_start
assert ival_W.asfreq('H', 'E') == ival_W_to_H_end
assert ival_W.asfreq('Min', 'S') == ival_W_to_T_start
assert ival_W.asfreq('Min', 'E') == ival_W_to_T_end
assert ival_W.asfreq('S', 'S') == ival_W_to_S_start
assert ival_W.asfreq('S', 'E') == ival_W_to_S_end
assert ival_W.asfreq('W') == ival_W
msg = INVALID_FREQ_ERR_MSG
with pytest.raises(ValueError, match=msg):
ival_W.asfreq('WK')
def test_conv_weekly_legacy(self):
# frequency conversion tests: from Weekly Frequency
msg = INVALID_FREQ_ERR_MSG
with pytest.raises(ValueError, match=msg):
Period(freq='WK', year=2007, month=1, day=1)
with pytest.raises(ValueError, match=msg):
Period(freq='WK-SAT', year=2007, month=1, day=6)
with pytest.raises(ValueError, match=msg):
Period(freq='WK-FRI', year=2007, month=1, day=5)
with pytest.raises(ValueError, match=msg):
Period(freq='WK-THU', year=2007, month=1, day=4)
with pytest.raises(ValueError, match=msg):
Period(freq='WK-WED', year=2007, month=1, day=3)
with pytest.raises(ValueError, match=msg):
Loading ...