コード例 #1
0
ファイル: calendar.py プロジェクト: cehbrecht/xclim
def cftime_start_time(date, freq):
    """Get the cftime.datetime for the start 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 starting 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):
        month = freq.month
        return date - YearEnd(n=1, month=month) + pydt.timedelta(days=1)
    if isinstance(freq, QuarterEnd):
        month = freq.month
        return date - QuarterEnd(n=1, month=month) + pydt.timedelta(days=1)
    if isinstance(freq, MonthEnd):
        return date - MonthEnd(n=1) + pydt.timedelta(days=1)
    return date
コード例 #2
0
ファイル: test_cftime_offsets.py プロジェクト: visr/xarray
                         [(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


@pytest.mark.parametrize(
    'offset', [BaseCFTimeOffset(),
               MonthBegin(), YearBegin()], ids=_id_func)
def test_to_offset_offset_input(offset):
    assert to_offset(offset) == offset


@pytest.mark.parametrize(('freq', 'expected'), [('M', MonthEnd()),
                                                ('2M', MonthEnd(n=2)),
                                                ('MS', MonthBegin()),
                                                ('2MS', MonthBegin(n=2)),
                                                ('D', Day()), ('2D', Day(n=2)),
                                                ('H', Hour()),
                                                ('2H', Hour(n=2)),
                                                ('T', Minute()),
                                                ('2T', Minute(n=2)),
                                                ('min', Minute()),
                                                ('2min', Minute(n=2)),
                                                ('S', Second()),
                                                ('2S', Second(n=2))],
                         ids=_id_func)
def test_to_offset_sub_annual(freq, expected):
    assert to_offset(freq) == expected
コード例 #3
0
    assert str(offset) == expected
    assert repr(offset) == expected


@pytest.mark.parametrize(
    'offset',
    [BaseCFTimeOffset(), MonthBegin(), QuarterBegin(), YearBegin()],
    ids=_id_func
)
def test_to_offset_offset_input(offset):
    assert to_offset(offset) == offset


@pytest.mark.parametrize(
    ('freq', 'expected'),
    [('M', MonthEnd()),
     ('2M', MonthEnd(n=2)),
     ('MS', MonthBegin()),
     ('2MS', MonthBegin(n=2)),
     ('D', Day()),
     ('2D', Day(n=2)),
     ('H', Hour()),
     ('2H', Hour(n=2)),
     ('T', Minute()),
     ('2T', Minute(n=2)),
     ('min', Minute()),
     ('2min', Minute(n=2)),
     ('S', Second()),
     ('2S', Second(n=2))],
    ids=_id_func
)
コード例 #4
0
@pytest.mark.parametrize(
    "offset",
    [BaseCFTimeOffset(),
     MonthBegin(),
     QuarterBegin(),
     YearBegin()],
    ids=_id_func,
)
def test_to_offset_offset_input(offset):
    assert to_offset(offset) == offset


@pytest.mark.parametrize(
    ("freq", "expected"),
    [
        ("M", MonthEnd()),
        ("2M", MonthEnd(n=2)),
        ("MS", MonthBegin()),
        ("2MS", MonthBegin(n=2)),
        ("D", Day()),
        ("2D", Day(n=2)),
        ("H", Hour()),
        ("2H", Hour(n=2)),
        ("T", Minute()),
        ("2T", Minute(n=2)),
        ("min", Minute()),
        ("2min", Minute(n=2)),
        ("S", Second()),
        ("2S", Second(n=2)),
    ],
    ids=_id_func,
コード例 #5
0
    assert repr(offset) == expected


@pytest.mark.parametrize(
    "offset",
    [BaseCFTimeOffset(), MonthBegin(), QuarterBegin(), YearBegin()],
    ids=_id_func,
)
def test_to_offset_offset_input(offset):
    assert to_offset(offset) == offset


@pytest.mark.parametrize(
    ("freq", "expected"),
    [
        ("M", MonthEnd()),
        ("2M", MonthEnd(n=2)),
        ("MS", MonthBegin()),
        ("2MS", MonthBegin(n=2)),
        ("D", Day()),
        ("2D", Day(n=2)),
        ("H", Hour()),
        ("2H", Hour(n=2)),
        ("T", Minute()),
        ("2T", Minute(n=2)),
        ("min", Minute()),
        ("2min", Minute(n=2)),
        ("S", Second()),
        ("2S", Second(n=2)),
        ("L", Millisecond(n=1)),
        ("2L", Millisecond(n=2)),