Skip to content

Commit 369da47

Browse files
committed
unit tests for liboffsets; fix an unraises ValueError
1 parent 99e9ec0 commit 369da47

File tree

2 files changed

+65
-2
lines changed

2 files changed

+65
-2
lines changed

pandas/_libs/tslibs/offsets.pyx

+2-2
Original file line numberDiff line numberDiff line change
@@ -430,7 +430,7 @@ cpdef datetime shift_month(datetime stamp, int months, object day_opt=None):
430430
return stamp.replace(year=year, month=month, day=day)
431431

432432

433-
cdef int get_day_of_month(datetime other, day_opt):
433+
cdef int get_day_of_month(datetime other, day_opt) except? -1:
434434
"""
435435
Find the day in `other`'s month that satisfies a DateOffset's onOffset
436436
policy, as described by the `day_opt` argument.
@@ -463,7 +463,7 @@ cdef int get_day_of_month(datetime other, day_opt):
463463
raise ValueError(day_opt)
464464

465465

466-
cpdef int roll_yearday(other, n, month, day_opt='start'):
466+
cpdef int roll_yearday(other, n, month, day_opt='start') except? -1:
467467
"""
468468
Possibly increment or decrement the number of periods to shift
469469
based on rollforward/rollbackward conventions.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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

Comments
 (0)