From 9c0967eba747620337613f411e297c29df24dd94 Mon Sep 17 00:00:00 2001 From: Mark Harfouche Date: Sat, 29 Jun 2024 12:01:35 -0400 Subject: [PATCH] MRC -- Selecting with string for cftime See discussion in #9138 This commit and pull request mostly serves as a staging group for a potential fix. Test with: ``` pytest xarray/tests/test_cftimeindex.py::test_cftime_noleap_with_str ``` --- xarray/tests/test_cftimeindex.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/xarray/tests/test_cftimeindex.py b/xarray/tests/test_cftimeindex.py index f6eb15fa373..23e66e1ad83 100644 --- a/xarray/tests/test_cftimeindex.py +++ b/xarray/tests/test_cftimeindex.py @@ -988,6 +988,30 @@ def test_cftimeindex_calendar_property(calendar, expected): assert index.calendar == expected +def test_cftime_noleap_with_str(): + time=xr.cftime_range('2000-01-01','2006-01-01', freq='D', calendar='noleap') + temperature = np.ones(len(time)) + da = xr.DataArray( + data=temperature, + dims=["time"], + coords=dict(time=time,), + ) + out=da.sel(time=slice('2001','2002')) # works if array is built with cftime + da1=da.convert_calendar('noleap') + out1=da1.sel(time=slice('2001','2002')) # works if array is built with cftime and converted + + time=pd.date_range('2000-01-01','2006-01-01', freq='D',) + temperature = np.ones(len(time)) + da2 = xr.DataArray( + data=temperature, + dims=["time"], + coords=dict(time=time,), + ) + out2=da2.sel(time=slice('2001','2002')) # # works if array is built with pandas time + da3=da2.convert_calendar('noleap') + out3=da3.sel(time=slice('2001','2002')) # fails if array is built with pandas time and convert to noleap + + @requires_cftime def test_empty_cftimeindex_calendar_property(): index = CFTimeIndex([])