From 0ce42916497e6f4a16fc1ec135f71042bafa8b3e Mon Sep 17 00:00:00 2001 From: Mathias Hauser Date: Fri, 12 Jan 2024 20:27:35 +0100 Subject: [PATCH] add tests --- xarray/tests/test_cftimeindex.py | 67 ++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) diff --git a/xarray/tests/test_cftimeindex.py b/xarray/tests/test_cftimeindex.py index e09fe2461ce..c145ea93644 100644 --- a/xarray/tests/test_cftimeindex.py +++ b/xarray/tests/test_cftimeindex.py @@ -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 @@ -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"), @@ -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): @@ -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")