peacock-data-public-datasets-idc-cronscript
/
venv
/lib
/python3.10
/site-packages
/pandas
/tests
/tslibs
/test_liboffsets.py
""" | |
Tests for helper functions in the cython tslibs.offsets | |
""" | |
from datetime import datetime | |
import pytest | |
from pandas._libs.tslibs.ccalendar import ( | |
get_firstbday, | |
get_lastbday, | |
) | |
import pandas._libs.tslibs.offsets as liboffsets | |
from pandas._libs.tslibs.offsets import roll_qtrday | |
from pandas import Timestamp | |
def day_opt(request): | |
return request.param | |
def test_get_last_bday(dt, exp_week_day, exp_last_day): | |
assert dt.weekday() == exp_week_day | |
assert get_lastbday(dt.year, dt.month) == exp_last_day | |
def test_get_first_bday(dt, exp_week_day, exp_first_day): | |
assert dt.weekday() == exp_week_day | |
assert get_firstbday(dt.year, dt.month) == exp_first_day | |
def test_shift_month_dt(months, day_opt, expected): | |
dt = datetime(2017, 11, 30) | |
assert liboffsets.shift_month(dt, months, day_opt=day_opt) == expected | |
def test_shift_month_ts(months, day_opt, expected): | |
ts = Timestamp("1929-05-05") | |
assert liboffsets.shift_month(ts, months, day_opt=day_opt) == expected | |
def test_shift_month_error(): | |
dt = datetime(2017, 11, 15) | |
day_opt = "this should raise" | |
with pytest.raises(ValueError, match=day_opt): | |
liboffsets.shift_month(dt, 3, day_opt=day_opt) | |
def test_roll_qtrday_year(other, expected, n): | |
month = 3 | |
day_opt = "start" # `other` will be compared to March 1. | |
assert roll_qtrday(other, n, month, day_opt, modby=12) == expected[n] | |
def test_roll_qtrday_year2(other, expected, n): | |
month = 6 | |
day_opt = "end" # `other` will be compared to June 30. | |
assert roll_qtrday(other, n, month, day_opt, modby=12) == expected[n] | |
def test_get_day_of_month_error(): | |
# get_day_of_month is not directly exposed. | |
# We test it via roll_qtrday. | |
dt = datetime(2017, 11, 15) | |
day_opt = "foo" | |
with pytest.raises(ValueError, match=day_opt): | |
# To hit the raising case we need month == dt.month and n > 0. | |
roll_qtrday(dt, n=3, month=11, day_opt=day_opt, modby=12) | |
def test_roll_qtr_day_not_mod_unequal(day_opt, month, n): | |
expected = {3: {-3: -2, 4: 4}, 5: {-3: -3, 4: 3}} | |
other = Timestamp(2072, 10, 1, 6, 17, 18) # Saturday. | |
assert roll_qtrday(other, n, month, day_opt, modby=3) == expected[month][n] | |
def test_roll_qtr_day_mod_equal(other, month, exp_dict, n, day_opt): | |
# All cases have (other.month % 3) == (month % 3). | |
expected = exp_dict.get(n, {}).get(day_opt, n) | |
assert roll_qtrday(other, n, month, day_opt, modby=3) == expected | |
def test_roll_convention(n, expected, compare): | |
assert liboffsets.roll_convention(29, n, compare) == expected[compare] | |