diff --git a/climada/hazard/base.py b/climada/hazard/base.py index 783d42616a..6e056bf7af 100644 --- a/climada/hazard/base.py +++ b/climada/hazard/base.py @@ -840,7 +840,7 @@ def maybe_repeat(values: np.ndarray, times: int) -> np.ndarray: None, np.ones(num_events), np.array(range(num_events), dtype=int) + 1, - [str(x) for x in data[coords["event"]].values], + list(data[coords["event"]].dt.strftime("%Y-%m-%d")), np.array(u_dt.datetime64_to_ordinal(data[coords["event"]].values)), ], # The accessor for the data in the Dataset diff --git a/climada/hazard/test/test_base_xarray.py b/climada/hazard/test/test_base_xarray.py index 53e8217e67..e2b644972b 100644 --- a/climada/hazard/test/test_base_xarray.py +++ b/climada/hazard/test/test_base_xarray.py @@ -72,7 +72,7 @@ def _assert_default_values(self, hazard): self.assertEqual(hazard.units, "") np.testing.assert_array_equal(hazard.event_id, [1, 2]) np.testing.assert_array_equal( - hazard.event_name, [str(x) for x in hazard.event_id] + hazard.event_name, [x.strftime("%Y-%m-%d") for x in self.time] ) np.testing.assert_array_equal( hazard.date, [val.toordinal() for val in self.time] @@ -342,7 +342,9 @@ def test_missing_dims(self): ds = ds.isel(time=0).squeeze() hazard = Hazard.from_xarray_raster(ds, "", "") self._assert_default_types(hazard) - + np.testing.assert_array_equal( + hazard.event_name, [self.time[0].strftime("%Y-%m-%d")] + ) np.testing.assert_array_equal(hazard.date, [self.time[0].toordinal()]) np.testing.assert_array_equal(hazard.centroids.lat, [0, 0, 0, 1, 1, 1]) np.testing.assert_array_equal(hazard.centroids.lon, [0, 1, 2, 0, 1, 2]) @@ -367,6 +369,9 @@ def test_missing_dims(self): ds = ds.expand_dims(time=[np.datetime64("2022-01-01")]) hazard = Hazard.from_xarray_raster(ds, "", "") self._assert_default_types(hazard) + np.testing.assert_array_equal( + hazard.event_name, ["2022-01-01"] + ) np.testing.assert_array_equal( hazard.date, [dt.datetime(2022, 1, 1).toordinal()] )