From 9653a012a49ce54e9e1ef25b815ecfad8b960b72 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kai=20M=C3=BChlbauer?= Date: Tue, 10 Dec 2024 13:57:33 +0100 Subject: [PATCH] fix tests after merge --- xarray/tests/test_variable.py | 58 ++++++++++++++++++++--------------- 1 file changed, 34 insertions(+), 24 deletions(-) diff --git a/xarray/tests/test_variable.py b/xarray/tests/test_variable.py index 6c5fc3ac9e1..32c4f8130d6 100644 --- a/xarray/tests/test_variable.py +++ b/xarray/tests/test_variable.py @@ -275,33 +275,43 @@ def test_0d_time_data(self): expected = np.datetime64("2000-01-01", "ns") assert x[0].values == expected + dt64_data = pd.date_range("2000-01-01", periods=3) + @pytest.mark.filterwarnings("ignore:Converting non-default") - def test_datetime64_conversion(self): - times = pd.date_range("2000-01-01", periods=3) - for values in [ - times, - times.values, - times.values.astype("datetime64[s]"), - times.to_pydatetime(), - ]: - v = self.cls(["t"], values) - assert v.dtype == np.dtype("datetime64[ns]") - assert_array_equal(v.values, times.values) - assert v.values.dtype == np.dtype("datetime64[ns]") + @pytest.mark.parametrize( + "values, unit", + [ + (dt64_data, "ns"), + (dt64_data.values, "ns"), + (dt64_data.values.astype("datetime64[s]"), "s"), + (dt64_data.to_pydatetime(), "ns"), + ], + ) + def test_datetime64_conversion(self, values, unit): + # todo: check, if this test is OK + v = self.cls(["t"], values) + assert v.dtype == np.dtype(f"datetime64[{unit}]") + assert_array_equal(v.values, self.dt64_data.values) + assert v.values.dtype == np.dtype(f"datetime64[{unit}]") + + td64_data = pd.timedelta_range(start=0, periods=3) @pytest.mark.filterwarnings("ignore:Converting non-default") - def test_timedelta64_conversion(self): - times = pd.timedelta_range(start=0, periods=3) - for values in [ - times, - times.values, - times.values.astype("timedelta64[s]"), - times.to_pytimedelta(), - ]: - v = self.cls(["t"], values) - assert v.dtype == np.dtype("timedelta64[ns]") - assert_array_equal(v.values, times.values) - assert v.values.dtype == np.dtype("timedelta64[ns]") + @pytest.mark.parametrize( + "values, unit", + [ + (td64_data, "ns"), + (td64_data.values, "ns"), + (td64_data.values.astype("timedelta64[s]"), "s"), + (td64_data.to_pytimedelta(), "ns"), + ], + ) + def test_timedelta64_conversion(self, values, unit): + # todo: check, if this test is OK + v = self.cls(["t"], values) + assert v.dtype == np.dtype(f"timedelta64[{unit}]") + assert_array_equal(v.values, self.td64_data.values) + assert v.values.dtype == np.dtype(f"timedelta64[{unit}]") def test_object_conversion(self): data = np.arange(5).astype(str).astype(object)