def test_to_offset_multiple(self): freqstr = '2h30min' freqstr2 = '2h 30min' result = frequencies.to_offset(freqstr) assert (result == frequencies.to_offset(freqstr2)) expected = offsets.Minute(150) assert (result == expected) freqstr = '2h30min15s' result = frequencies.to_offset(freqstr) expected = offsets.Second(150 * 60 + 15) assert (result == expected) freqstr = '2h 60min' result = frequencies.to_offset(freqstr) expected = offsets.Hour(3) assert (result == expected) freqstr = '15l500u' result = frequencies.to_offset(freqstr) expected = offsets.Micro(15500) assert (result == expected) freqstr = '10s75L' result = frequencies.to_offset(freqstr) expected = offsets.Milli(10075) assert (result == expected) freqstr = '2800N' result = frequencies.to_offset(freqstr) expected = offsets.Nano(2800) assert (result == expected) freqstr = '2SM' result = frequencies.to_offset(freqstr) expected = offsets.SemiMonthEnd(2) assert (result == expected) freqstr = '2SM-16' result = frequencies.to_offset(freqstr) expected = offsets.SemiMonthEnd(2, day_of_month=16) assert (result == expected) freqstr = '2SMS-14' result = frequencies.to_offset(freqstr) expected = offsets.SemiMonthBegin(2, day_of_month=14) assert (result == expected) freqstr = '2SMS-15' result = frequencies.to_offset(freqstr) expected = offsets.SemiMonthBegin(2) assert (result == expected) # malformed with tm.assertRaisesRegexp(ValueError, 'Invalid frequency: 2h20m'): frequencies.to_offset('2h20m')
def test_anchored_shortcuts(): result = frequencies.to_offset('W') expected = frequencies.to_offset('W-SUN') assert (result == expected) result1 = frequencies.to_offset('Q') result2 = frequencies.to_offset('Q-DEC') expected = offsets.QuarterEnd(startingMonth=12) assert (result1 == expected) assert (result2 == expected) result1 = frequencies.to_offset('Q-MAY') expected = offsets.QuarterEnd(startingMonth=5) assert (result1 == expected) result1 = frequencies.to_offset('SM') result2 = frequencies.to_offset('SM-15') expected = offsets.SemiMonthEnd(day_of_month=15) assert (result1 == expected) assert (result2 == expected) result = frequencies.to_offset('SM-1') expected = offsets.SemiMonthEnd(day_of_month=1) assert (result == expected) result = frequencies.to_offset('SM-27') expected = offsets.SemiMonthEnd(day_of_month=27) assert (result == expected) result = frequencies.to_offset('SMS-2') expected = offsets.SemiMonthBegin(day_of_month=2) assert (result == expected) result = frequencies.to_offset('SMS-27') expected = offsets.SemiMonthBegin(day_of_month=27) assert (result == expected) # ensure invalid cases fail as expected invalid_anchors = [ 'SM-0', 'SM-28', 'SM-29', 'SM-FOO', 'BSM', 'SM--1' 'SMS-1', 'SMS-28', 'SMS-30', 'SMS-BAR', 'BSMS', 'SMS--2' ] for invalid_anchor in invalid_anchors: try: frequencies.to_offset(invalid_anchor) except ValueError: pass else: raise AssertionError(invalid_anchor)
def test_to_offset_invalid(self): # GH 13930 with tm.assert_raises_regex(ValueError, 'Invalid frequency: U1'): frequencies.to_offset('U1') with tm.assert_raises_regex(ValueError, 'Invalid frequency: -U'): frequencies.to_offset('-U') with tm.assert_raises_regex(ValueError, 'Invalid frequency: 3U1'): frequencies.to_offset('3U1') with tm.assert_raises_regex(ValueError, 'Invalid frequency: -2-3U'): frequencies.to_offset('-2-3U') with tm.assert_raises_regex(ValueError, 'Invalid frequency: -2D:3H'): frequencies.to_offset('-2D:3H') with tm.assert_raises_regex(ValueError, 'Invalid frequency: 1.5.0S'): frequencies.to_offset('1.5.0S') # split offsets with spaces are valid assert frequencies.to_offset('2D 3H') == offsets.Hour(51) assert frequencies.to_offset('2 D3 H') == offsets.Hour(51) assert frequencies.to_offset('2 D 3 H') == offsets.Hour(51) assert frequencies.to_offset(' 2 D 3 H ') == offsets.Hour(51) assert frequencies.to_offset(' H ') == offsets.Hour() assert frequencies.to_offset(' 3 H ') == offsets.Hour(3) # special cases assert frequencies.to_offset('2SMS-15') == offsets.SemiMonthBegin(2) with tm.assert_raises_regex(ValueError, 'Invalid frequency: 2SMS-15-15'): frequencies.to_offset('2SMS-15-15') with tm.assert_raises_regex(ValueError, 'Invalid frequency: 2SMS-15D'): frequencies.to_offset('2SMS-15D')
def test_to_offset_invalid(self): # GH 13930 with tm.assertRaisesRegexp(ValueError, 'Invalid frequency: U1'): frequencies.to_offset('U1') with tm.assertRaisesRegexp(ValueError, 'Invalid frequency: -U'): frequencies.to_offset('-U') with tm.assertRaisesRegexp(ValueError, 'Invalid frequency: 3U1'): frequencies.to_offset('3U1') with tm.assertRaisesRegexp(ValueError, 'Invalid frequency: -2-3U'): frequencies.to_offset('-2-3U') with tm.assertRaisesRegexp(ValueError, 'Invalid frequency: -2D:3H'): frequencies.to_offset('-2D:3H') # ToDo: Must be fixed in #8419 with tm.assertRaisesRegexp(ValueError, 'Invalid frequency: .5S'): frequencies.to_offset('.5S') # split offsets with spaces are valid assert frequencies.to_offset('2D 3H') == offsets.Hour(51) assert frequencies.to_offset('2 D3 H') == offsets.Hour(51) assert frequencies.to_offset('2 D 3 H') == offsets.Hour(51) assert frequencies.to_offset(' 2 D 3 H ') == offsets.Hour(51) assert frequencies.to_offset(' H ') == offsets.Hour() assert frequencies.to_offset(' 3 H ') == offsets.Hour(3) # special cases assert frequencies.to_offset('2SMS-15') == offsets.SemiMonthBegin(2) with tm.assertRaisesRegexp(ValueError, 'Invalid frequency: 2SMS-15-15'): frequencies.to_offset('2SMS-15-15') with tm.assertRaisesRegexp(ValueError, 'Invalid frequency: 2SMS-15D'): frequencies.to_offset('2SMS-15D')
def test_anchored_shortcuts(self): result = frequencies.to_offset('W') expected = frequencies.to_offset('W-SUN') assert (result == expected) result1 = frequencies.to_offset('Q') result2 = frequencies.to_offset('Q-DEC') expected = offsets.QuarterEnd(startingMonth=12) assert (result1 == expected) assert (result2 == expected) result1 = frequencies.to_offset('Q-MAY') expected = offsets.QuarterEnd(startingMonth=5) assert (result1 == expected) result1 = frequencies.to_offset('SM') result2 = frequencies.to_offset('SM-15') expected = offsets.SemiMonthEnd(day_of_month=15) assert (result1 == expected) assert (result2 == expected) result = frequencies.to_offset('SM-1') expected = offsets.SemiMonthEnd(day_of_month=1) assert (result == expected) result = frequencies.to_offset('SM-27') expected = offsets.SemiMonthEnd(day_of_month=27) assert (result == expected) result = frequencies.to_offset('SMS-2') expected = offsets.SemiMonthBegin(day_of_month=2) assert (result == expected) result = frequencies.to_offset('SMS-27') expected = offsets.SemiMonthBegin(day_of_month=27) assert (result == expected) # ensure invalid cases fail as expected invalid_anchors = ['SM-0', 'SM-28', 'SM-29', 'SM-FOO', 'BSM', 'SM--1', 'SMS-1', 'SMS-28', 'SMS-30', 'SMS-BAR', 'SMS-BYR' 'BSMS', 'SMS--2'] for invalid_anchor in invalid_anchors: with tm.assert_raises_regex(ValueError, 'Invalid frequency: '): frequencies.to_offset(invalid_anchor)
def test_to_offset_multiple(): freqstr = '2h30min' freqstr2 = '2h 30min' result = frequencies.to_offset(freqstr) assert (result == frequencies.to_offset(freqstr2)) expected = offsets.Minute(150) assert (result == expected) freqstr = '2h30min15s' result = frequencies.to_offset(freqstr) expected = offsets.Second(150 * 60 + 15) assert (result == expected) freqstr = '2h 60min' result = frequencies.to_offset(freqstr) expected = offsets.Hour(3) assert (result == expected) freqstr = '15l500u' result = frequencies.to_offset(freqstr) expected = offsets.Micro(15500) assert (result == expected) freqstr = '10s75L' result = frequencies.to_offset(freqstr) expected = offsets.Milli(10075) assert (result == expected) freqstr = '2800N' result = frequencies.to_offset(freqstr) expected = offsets.Nano(2800) assert (result == expected) freqstr = '2SM' result = frequencies.to_offset(freqstr) expected = offsets.SemiMonthEnd(2) assert (result == expected) freqstr = '2SM-16' result = frequencies.to_offset(freqstr) expected = offsets.SemiMonthEnd(2, day_of_month=16) assert (result == expected) freqstr = '2SMS-14' result = frequencies.to_offset(freqstr) expected = offsets.SemiMonthBegin(2, day_of_month=14) assert (result == expected) freqstr = '2SMS-15' result = frequencies.to_offset(freqstr) expected = offsets.SemiMonthBegin(2) assert (result == expected) # malformed try: frequencies.to_offset('2h20m') except ValueError: pass else: assert (False)
((5, "T"), offsets.Minute(5)), ("2h30min", offsets.Minute(150)), ("2h 30min", offsets.Minute(150)), ("2h30min15s", offsets.Second(150 * 60 + 15)), ("2h 60min", offsets.Hour(3)), ("2h 20.5min", offsets.Second(8430)), ("1.5min", offsets.Second(90)), ("0.5S", offsets.Milli(500)), ("15l500u", offsets.Micro(15500)), ("10s75L", offsets.Milli(10075)), ("1s0.25ms", offsets.Micro(1000250)), ("1s0.25L", offsets.Micro(1000250)), ("2800N", offsets.Nano(2800)), ("2SM", offsets.SemiMonthEnd(2)), ("2SM-16", offsets.SemiMonthEnd(2, day_of_month=16)), ("2SMS-14", offsets.SemiMonthBegin(2, day_of_month=14)), ("2SMS-15", offsets.SemiMonthBegin(2)), ], ) def test_to_offset(freq_input, expected): result = frequencies.to_offset(freq_input) assert result == expected @pytest.mark.parametrize( "freqstr,expected", [("-1S", -1), ("-2SM", -2), ("-1SMS", -1), ("-5min10s", -310)] ) def test_to_offset_negative(freqstr, expected): result = frequencies.to_offset(freqstr) assert result.n == expected