Esempio n. 1
0
def index(date_type):
    dates = [
        date_type(1, 1, 1),
        date_type(1, 2, 1),
        date_type(2, 1, 1),
        date_type(2, 2, 1)
    ]
    return CFTimeIndex(dates)
Esempio n. 2
0
def test_cftimeindex_shift(index, freq):
    date_type = index.date_type
    expected_dates = [date_type(1, 1, 3), date_type(1, 2, 3),
                      date_type(2, 1, 3), date_type(2, 2, 3)]
    expected = CFTimeIndex(expected_dates)
    result = index.shift(2, freq)
    assert result.equals(expected)
    assert isinstance(result, CFTimeIndex)
Esempio n. 3
0
def index_with_name(date_type):
    dates = [
        date_type(1, 1, 1),
        date_type(1, 2, 1),
        date_type(2, 1, 1),
        date_type(2, 2, 1)
    ]
    return CFTimeIndex(dates, name='foo')
Esempio n. 4
0
def monotonic_decreasing_index(date_type):
    dates = [
        date_type(2, 2, 1),
        date_type(2, 1, 1),
        date_type(1, 2, 1),
        date_type(1, 1, 1)
    ]
    return CFTimeIndex(dates)
Esempio n. 5
0
def test_cftimeindex_radd(index):
    date_type = index.date_type
    expected_dates = [date_type(1, 1, 2), date_type(1, 2, 2),
                      date_type(2, 1, 2), date_type(2, 2, 2)]
    expected = CFTimeIndex(expected_dates)
    result = timedelta(days=1) + index
    assert result.equals(expected)
    assert isinstance(result, CFTimeIndex)
Esempio n. 6
0
def test_time_bnds(freq, datetime_index, cftime_index):
    da_datetime = da(datetime_index).resample(time=freq)
    da_cftime = da(cftime_index).resample(time=freq)

    cftime_bounds = time_bnds(da_cftime, freq=freq)
    cftime_starts, cftime_ends = zip(*cftime_bounds)
    cftime_starts = CFTimeIndex(cftime_starts).to_datetimeindex()
    cftime_ends = CFTimeIndex(cftime_ends).to_datetimeindex()

    # cftime resolution goes down to microsecond only, code below corrects
    # that to allow for comparison with pandas datetime
    cftime_ends += np.timedelta64(999, "ns")
    datetime_starts = da_datetime._full_index.to_period(freq).start_time
    datetime_ends = da_datetime._full_index.to_period(freq).end_time

    assert_array_equal(cftime_starts, datetime_starts)
    assert_array_equal(cftime_ends, datetime_ends)
Esempio n. 7
0
def test_safe_cast_to_index_cftimeindex():
    date_types = _all_cftime_date_types()
    for date_type in date_types.values():
        dates = [date_type(1, 1, day) for day in range(1, 20)]
        expected = CFTimeIndex(dates)
        actual = utils.safe_cast_to_index(np.array(dates))
        assert_array_equal(expected, actual)
        assert expected.dtype == actual.dtype
        assert isinstance(actual, type(expected))
Esempio n. 8
0
def test_cftimeindex_sub_timedelta_array(index, other):
    date_type = index.date_type
    expected_dates = [
        date_type(1, 1, 2),
        date_type(1, 2, 2),
        date_type(2, 1, 2),
        date_type(2, 2, 2),
    ]
    expected = CFTimeIndex(expected_dates)
    result = index + timedelta(days=2)
    result = result - other
    assert result.equals(expected)
    assert isinstance(result, CFTimeIndex)
Esempio n. 9
0
def test_safe_cast_to_index_cftimeindex(enable_cftimeindex):
    date_types = _all_cftime_date_types()
    for date_type in date_types.values():
        dates = [date_type(1, 1, day) for day in range(1, 20)]

        if enable_cftimeindex and has_cftime:
            expected = CFTimeIndex(dates)
        else:
            expected = pd.Index(dates)

        with set_options(enable_cftimeindex=enable_cftimeindex):
            actual = utils.safe_cast_to_index(np.array(dates))
        assert_array_equal(expected, actual)
        assert expected.dtype == actual.dtype
        assert isinstance(actual, type(expected))
Esempio n. 10
0
def cfindex_end_time(cfindex, freq):
    """
    Get the end of a period for a pseudo-period index.

    As we are using datetime indices to stand in for period indices, 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
    ----------
    cfindex : CFTimeIndex
        CFTimeIndex as a proxy representation for CFPeriodIndex
    freq : str
        String specifying the frequency/offset such as 'MS', '2D', 'H', or '3T'

    Returns
    -------
    CFTimeIndex
        The ending datetimes of periods inferred from dates and freq
    """
    return CFTimeIndex([cftime_end_time(date, freq) for date in cfindex])
Esempio n. 11
0
def test_empty_cftimeindex():
    index = CFTimeIndex([])
    assert index.date_type is None
Esempio n. 12
0
def test_constructor_with_name(index_with_name, name, expected_name):
    result = CFTimeIndex(index_with_name, name=name).name
    assert result == expected_name
Esempio n. 13
0
def length_one_index(date_type):
    dates = [date_type(1, 1, 1)]
    return CFTimeIndex(dates)
Esempio n. 14
0
    "periods,expected",
    [
        (
            2,
            """\
CFTimeIndex([2000-01-01 00:00:00, 2000-01-02 00:00:00],
            dtype='object', length=2, calendar='gregorian', freq=None)""",
        ),
        (
            4,
            """\
CFTimeIndex([2000-01-01 00:00:00, 2000-01-02 00:00:00, 2000-01-03 00:00:00,
             2000-01-04 00:00:00],
            dtype='object', length=4, calendar='gregorian', freq='D')""",
        ),
        (
            101,
            """\
CFTimeIndex([2000-01-01 00:00:00, 2000-01-02 00:00:00, 2000-01-03 00:00:00,
             2000-01-04 00:00:00, 2000-01-05 00:00:00, 2000-01-06 00:00:00,
             2000-01-07 00:00:00, 2000-01-08 00:00:00, 2000-01-09 00:00:00,
             2000-01-10 00:00:00,
             ...
             2000-04-01 00:00:00, 2000-04-02 00:00:00, 2000-04-03 00:00:00,
             2000-04-04 00:00:00, 2000-04-05 00:00:00, 2000-04-06 00:00:00,
             2000-04-07 00:00:00, 2000-04-08 00:00:00, 2000-04-09 00:00:00,
             2000-04-10 00:00:00],
            dtype='object', length=101, calendar='gregorian', freq='D')""",
        ),
    ],
)