|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +""" |
| 3 | +Tests for helper functions in the cython tslibs.offsets |
| 4 | +""" |
| 5 | +from datetime import datetime |
| 6 | + |
| 7 | +import pytest |
| 8 | + |
| 9 | +import pandas as pd |
| 10 | + |
| 11 | +import pandas._libs.tslibs.offsets as liboffsets |
| 12 | + |
| 13 | + |
| 14 | +def test_shift_month(): |
| 15 | + dt = datetime(2017, 11, 15) |
| 16 | + |
| 17 | + assert liboffsets.shift_month(dt, 0, day_opt=None) == dt |
| 18 | + assert liboffsets.shift_month(dt, 0, day_opt=15) == dt |
| 19 | + |
| 20 | + assert liboffsets.shift_month(dt, 1, |
| 21 | + day_opt='start') == datetime(2017, 12, 1) |
| 22 | + |
| 23 | + assert liboffsets.shift_month(dt, -145, |
| 24 | + day_opt='end') == datetime(2005, 10, 31) |
| 25 | + |
| 26 | + with pytest.raises(ValueError): |
| 27 | + liboffsets.shift_month(dt, 3, day_opt='this should raise') |
| 28 | + |
| 29 | + |
| 30 | +def test_get_day_of_month(): |
| 31 | + # get_day_of_month is not directly exposed; we test it via roll_yearday |
| 32 | + dt = datetime(2017, 11, 15) |
| 33 | + |
| 34 | + with pytest.raises(ValueError): |
| 35 | + # To hit the raising case we need month == dt.month and n > 0 |
| 36 | + liboffsets.roll_yearday(dt, n=3, month=11, day_opt='foo') |
| 37 | + |
| 38 | + |
| 39 | +def test_roll_yearday(): |
| 40 | + # Copied from doctest examples |
| 41 | + month = 3 |
| 42 | + day_opt = 'start' # `other` will be compared to March 1 |
| 43 | + other = datetime(2017, 2, 10) # before March 1 |
| 44 | + assert liboffsets.roll_yearday(other, 2, month, day_opt) == 1 |
| 45 | + assert liboffsets.roll_yearday(other, -7, month, day_opt) == -7 |
| 46 | + assert liboffsets.roll_yearday(other, 0, month, day_opt) == 0 |
| 47 | + |
| 48 | + other = pd.Timestamp('2014-03-15', tz='US/Eastern') # after March 1 |
| 49 | + assert liboffsets.roll_yearday(other, 2, month, day_opt) == 2 |
| 50 | + assert liboffsets.roll_yearday(other, -7, month, day_opt) == -6 |
| 51 | + assert liboffsets.roll_yearday(other, 0, month, day_opt) == 1 |
| 52 | + |
| 53 | + month = 6 |
| 54 | + day_opt = 'end' # `other` will be compared to June 30 |
| 55 | + other = datetime(1999, 6, 29) # before June 30 |
| 56 | + assert liboffsets.roll_yearday(other, 5, month, day_opt) == 4 |
| 57 | + assert liboffsets.roll_yearday(other, -7, month, day_opt) == -7 |
| 58 | + assert liboffsets.roll_yearday(other, 0, month, day_opt) == 0 |
| 59 | + |
| 60 | + other = pd.Timestamp(2072, 8, 24, 6, 17, 18) # after June 30 |
| 61 | + assert liboffsets.roll_yearday(other, 5, month, day_opt) == 5 |
| 62 | + assert liboffsets.roll_yearday(other, -7, month, day_opt) == -6 |
| 63 | + assert liboffsets.roll_yearday(other, 0, month, day_opt) == 1 |
0 commit comments