Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mathause committed Jan 12, 2024
1 parent 769a872 commit 0ce4291
Showing 1 changed file with 67 additions and 0 deletions.
67 changes: 67 additions & 0 deletions xarray/tests/test_cftimeindex.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,28 @@ def test_cftimeindex_field_accessors(index, field, expected):
assert_array_equal(result, expected)


@requires_cftime
@pytest.mark.parametrize(
("field"),
[
"year",
"month",
"day",
"hour",
"minute",
"second",
"microsecond",
"dayofyear",
"dayofweek",
"days_in_month",
],
)
def test_empty_cftimeindex_field_accessors(field):
index = CFTimeIndex([])
result = getattr(index, field)
assert_array_equal(result, np.array([], dtype=np.float64))


@requires_cftime
def test_cftimeindex_dayofyear_accessor(index):
result = index.dayofyear
Expand Down Expand Up @@ -959,6 +981,31 @@ def test_cftimeindex_calendar_property(calendar, expected):
assert index.calendar == expected


@requires_cftime
def test_empty_cftimeindex_calendar_property():
index = CFTimeIndex([])
assert index.calendar is None


@requires_cftime
@pytest.mark.parametrize(
"calendar",
[
"noleap",
"365_day",
"360_day",
"julian",
"gregorian",
"standard",
"proleptic_gregorian",
],
)
def test_cftimeindex_freq_property_none_size_lt_3(calendar):
for periods in range(3):
index = xr.cftime_range(start="2000", periods=periods, calendar=calendar)
assert index.freq is None


@requires_cftime
@pytest.mark.parametrize(
("calendar", "expected"),
Expand Down Expand Up @@ -1152,6 +1199,18 @@ def test_rounding_methods_against_datetimeindex(freq, method):
assert result.equals(expected)


@requires_cftime
@pytest.mark.parametrize("method", ["floor", "ceil", "round"])
def test_rounding_methods_empty_cftimindex(method):
index = CFTimeIndex([])
result = getattr(index, method)("2s")

expected = CFTimeIndex([])

assert result.equals(expected)
assert result is not index


@requires_cftime
@pytest.mark.parametrize("method", ["floor", "ceil", "round"])
def test_rounding_methods_invalid_freq(method):
Expand Down Expand Up @@ -1230,6 +1289,14 @@ def test_asi8_distant_date():
np.testing.assert_array_equal(result, expected)


@requires_cftime
def test_asi8_empty_cftimeindex():
index = xr.CFTimeIndex([])
result = index.asi8
expected = np.array([], dtype=int)
np.testing.assert_array_equal(result, expected)


@requires_cftime
def test_infer_freq_valid_types():
cf_indx = xr.cftime_range("2000-01-01", periods=3, freq="D")
Expand Down

0 comments on commit 0ce4291

Please sign in to comment.