def cftime_end_time(date, freq): """Get the cftime.datetime for the end of a period. As we are not supplying actual period objects, assumptions regarding the period are made based on the given freq. IMPORTANT NOTE: this function cannot be used on greater-than-day freq that start at the beginning of a month, e.g. 'MS', 'QS', 'AS' -- this mirrors pandas behavior. Parameters ---------- date : cftime.datetime The original datetime object as a proxy representation for period. freq : str String specifying the frequency/offset such as 'MS', '2D', 'H', or '3T' Returns ------- cftime.datetime The ending datetime of the period inferred from date and freq. """ freq = to_offset(freq) if isinstance(freq, (YearBegin, QuarterBegin, MonthBegin)): raise ValueError("Invalid frequency: " + freq.rule_code()) if isinstance(freq, YearEnd): mod_freq = YearBegin(n=freq.n, month=freq.month) elif isinstance(freq, QuarterEnd): mod_freq = QuarterBegin(n=freq.n, month=freq.month) elif isinstance(freq, MonthEnd): mod_freq = MonthBegin(n=freq.n) else: mod_freq = freq return cftime_start_time(date + mod_freq, freq) - pydt.timedelta(microseconds=1)
( YearBegin, 13, ValueError, ), (YearEnd, 13, ValueError), (YearBegin, 1.5, TypeError), (YearEnd, 1.5, TypeError)], ids=_id_func) def test_year_offset_constructor_invalid_month(offset, invalid_month, exception): with pytest.raises(exception): offset(month=invalid_month) @pytest.mark.parametrize(('offset', 'expected'), [(BaseCFTimeOffset(), None), (MonthBegin(), 'MS'), (YearBegin(), 'AS-JAN')], ids=_id_func) def test_rule_code(offset, expected): assert offset.rule_code() == expected @pytest.mark.parametrize(('offset', 'expected'), [(BaseCFTimeOffset(), '<BaseCFTimeOffset: n=1>'), (YearBegin(), '<YearBegin: n=1, month=1>')], ids=_id_func) def test_str_and_repr(offset, expected): assert str(offset) == expected assert repr(offset) == expected
(QuarterBegin, 1.5, TypeError), (QuarterEnd, 1.5, TypeError), (QuarterBegin, 13, ValueError), (QuarterEnd, 13, ValueError)], ids=_id_func ) def test_year_offset_constructor_invalid_month( offset, invalid_month, exception): with pytest.raises(exception): offset(month=invalid_month) @pytest.mark.parametrize( ('offset', 'expected'), [(BaseCFTimeOffset(), None), (MonthBegin(), 'MS'), (YearBegin(), 'AS-JAN'), (QuarterBegin(), 'QS-MAR')], ids=_id_func ) def test_rule_code(offset, expected): assert offset.rule_code() == expected @pytest.mark.parametrize( ('offset', 'expected'), [(BaseCFTimeOffset(), '<BaseCFTimeOffset: n=1>'), (YearBegin(), '<YearBegin: n=1, month=1>'), (QuarterBegin(), '<QuarterBegin: n=1, month=3>')], ids=_id_func )
(QuarterBegin, 13, ValueError), (QuarterEnd, 13, ValueError), ], ids=_id_func, ) def test_year_offset_constructor_invalid_month(offset, invalid_month, exception): with pytest.raises(exception): offset(month=invalid_month) @pytest.mark.parametrize( ("offset", "expected"), [ (BaseCFTimeOffset(), None), (MonthBegin(), "MS"), (YearBegin(), "AS-JAN"), (QuarterBegin(), "QS-MAR"), ], ids=_id_func, ) def test_rule_code(offset, expected): assert offset.rule_code() == expected @pytest.mark.parametrize( ("offset", "expected"), [ (BaseCFTimeOffset(), "<BaseCFTimeOffset: n=1>"), (YearBegin(), "<YearBegin: n=1, month=1>"), (QuarterBegin(), "<QuarterBegin: n=1, month=3>"),
(QuarterEnd, 1.5, TypeError), (QuarterBegin, 13, ValueError), (QuarterEnd, 13, ValueError), ], ids=_id_func, ) def test_year_offset_constructor_invalid_month(offset, invalid_month, exception): with pytest.raises(exception): offset(month=invalid_month) @pytest.mark.parametrize( ("offset", "expected"), [ (BaseCFTimeOffset(), None), (MonthBegin(), "MS"), (YearBegin(), "AS-JAN"), (QuarterBegin(), "QS-MAR"), ], ids=_id_func, ) def test_rule_code(offset, expected): assert offset.rule_code() == expected @pytest.mark.parametrize( ("offset", "expected"), [ (BaseCFTimeOffset(), "<BaseCFTimeOffset: n=1>"), (YearBegin(), "<YearBegin: n=1, month=1>"), (QuarterBegin(), "<QuarterBegin: n=1, month=3>"),