Skip to content

Commit 05ee576

Browse files
sarah-hlsnchahankpeanutfun
authored
Change default Hazard.event_name to string of event_id (#795)
* Update Hazard base.py Change event_name default for Hazard.from_xarray_raster to string of event id. Change variable description in class method. * Update test_base_xarray.py adapted event_name in test_assert_default_values and deleted event_name tests from test_missing_dims since the time dimension does not affect event_name anymore * change default name to string of date * save event_name as string of date and time * ensure list of event names is flat * Update CHANGELOG.md * Update CHANGELOG.md * update changelog * Apply suggestions from code review Co-authored-by: Lukas Riedel <[email protected]> * Update AUTHORS.md --------- Co-authored-by: Chahan M. Kropf <[email protected]> Co-authored-by: Lukas Riedel <[email protected]>
1 parent 158b1d8 commit 05ee576

File tree

4 files changed

+9
-4
lines changed

4 files changed

+9
-4
lines changed

AUTHORS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,4 @@
3030
* Nicolas Colombi
3131
* Leonie Villiger
3232
* Kam Lam Yeung
33+
* Sarah Hülsen

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ Code freeze date: YYYY-MM-DD
2020

2121
### Fixed
2222

23+
- `Hazard.from_xarray_raster` now stores strings as default values for `Hazard.event_name` [#795](https://github.com/CLIMADA-project/climada_python/pull/795)
2324
- Fix the dist_approx util function when used with method="geosphere" and log=True and points that are very close. [#792](https://github.com/CLIMADA-project/climada_python/pull/792)
2425

2526
### Deprecated

climada/hazard/base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -840,7 +840,7 @@ def maybe_repeat(values: np.ndarray, times: int) -> np.ndarray:
840840
None,
841841
np.ones(num_events),
842842
np.array(range(num_events), dtype=int) + 1,
843-
list(data[coords["event"]].values),
843+
data[coords["event"]].dt.strftime("%Y-%m-%d").values.flatten().tolist(),
844844
np.array(u_dt.datetime64_to_ordinal(data[coords["event"]].values)),
845845
],
846846
# The accessor for the data in the Dataset

climada/hazard/test/test_base_xarray.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ def _assert_default_values(self, hazard):
7272
self.assertEqual(hazard.units, "")
7373
np.testing.assert_array_equal(hazard.event_id, [1, 2])
7474
np.testing.assert_array_equal(
75-
hazard.event_name, [np.datetime64(val) for val in self.time]
75+
hazard.event_name, [x.strftime("%Y-%m-%d") for x in self.time]
7676
)
7777
np.testing.assert_array_equal(
7878
hazard.date, [val.toordinal() for val in self.time]
@@ -343,7 +343,7 @@ def test_missing_dims(self):
343343
hazard = Hazard.from_xarray_raster(ds, "", "")
344344
self._assert_default_types(hazard)
345345
np.testing.assert_array_equal(
346-
hazard.event_name, [np.datetime64(self.time[0])]
346+
hazard.event_name, [self.time[0].strftime("%Y-%m-%d")]
347347
)
348348
np.testing.assert_array_equal(hazard.date, [self.time[0].toordinal()])
349349
np.testing.assert_array_equal(hazard.centroids.lat, [0, 0, 0, 1, 1, 1])
@@ -370,7 +370,7 @@ def test_missing_dims(self):
370370
hazard = Hazard.from_xarray_raster(ds, "", "")
371371
self._assert_default_types(hazard)
372372
np.testing.assert_array_equal(
373-
hazard.event_name, [np.datetime64("2022-01-01")]
373+
hazard.event_name, ["2022-01-01"]
374374
)
375375
np.testing.assert_array_equal(
376376
hazard.date, [dt.datetime(2022, 1, 1).toordinal()]
@@ -551,6 +551,9 @@ def test_2D_time(self):
551551
np.testing.assert_array_equal(
552552
hazard.date, [val.toordinal() for val in time.flat]
553553
)
554+
np.testing.assert_array_equal(
555+
hazard.event_name, ["1999-01-01", "1999-02-01", "2000-01-01", "2000-02-01"]
556+
)
554557

555558
def test_errors(self):
556559
"""Check if expected errors are thrown"""

0 commit comments

Comments
 (0)