Skip to content

Commit 6a07625

Browse files
committed
Removes int support for dates
1 parent b5d3378 commit 6a07625

2 files changed

Lines changed: 17 additions & 17 deletions

File tree

climada/trajectories/snapshot.py

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ class Snapshot:
4949
exposure : Exposures
5050
hazard : Hazard
5151
impfset : ImpactFuncSet
52-
date : int | datetime.date | str | pd.Timestamp
53-
The date of the Snapshot, it can be an integer representing a year,
52+
date : datetime.date | str | pd.Timestamp
53+
The date of the Snapshot, it can be an string representing a year,
5454
a datetime object or a string representation of a datetime object.
5555
ref_only : bool, default False
5656
Should the `Snapshot` contain deep copies of the Exposures, Hazard and Impfset (False)
@@ -83,7 +83,7 @@ def __init__(
8383
hazard: Hazard,
8484
impfset: ImpactFuncSet,
8585
measure: Measure | None,
86-
date: int | datetime.date | str | pd.Timestamp,
86+
date: datetime.date | str | pd.Timestamp,
8787
ref_only: bool = False,
8888
_from_factory: bool = False,
8989
) -> None:
@@ -107,7 +107,7 @@ def from_triplet(
107107
exposure: Exposures,
108108
hazard: Hazard,
109109
impfset: ImpactFuncSet,
110-
date: int | datetime.date | str | pd.Timestamp,
110+
date: datetime.date | str | pd.Timestamp,
111111
ref_only: bool = False,
112112
) -> "Snapshot":
113113
"""Create a Snapshot from exposure, hazard and impact functions set
@@ -122,7 +122,7 @@ def from_triplet(
122122
exposure : Exposures
123123
hazard : Hazard
124124
impfset : ImpactFuncSet
125-
date : int | datetime.date | str | pd.Timestamp
125+
date : datetime.date | str | pd.Timestamp
126126
ref_only : bool
127127
If true, uses references to the exposure, hazard and impact
128128
function objects. Note that modifying the original objects after
@@ -186,10 +186,7 @@ def impact_calc_data(self) -> dict:
186186

187187
@staticmethod
188188
def _convert_to_timestamp(date_arg) -> pd.Timestamp:
189-
"""Convert date argument of type int or str or datetime.date to pandas Timestamp object."""
190-
if isinstance(date_arg, int):
191-
# Assume the integer represents a year
192-
return pd.Timestamp(year=date_arg, month=1, day=1)
189+
"""Convert date argument of type str or datetime.date to pandas Timestamp object."""
193190
if isinstance(date_arg, str):
194191
# Try to parse the string as a date
195192
try:
@@ -201,9 +198,7 @@ def _convert_to_timestamp(date_arg) -> pd.Timestamp:
201198
if isinstance(date_arg, pd.Timestamp):
202199
return date_arg
203200

204-
raise TypeError(
205-
"date_arg must be an int, str, datetime.date or pandas.Timestamp"
206-
)
201+
raise TypeError("date_arg must be an str, datetime.date or pandas.Timestamp")
207202

208203
def apply_measure(self, measure: Measure) -> "Snapshot":
209204
"""Create a new snapshot by applying a Measure object.

climada/trajectories/test/test_snapshot.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ def mock_context(shared_data):
5757
"mod_exp": modified_exp,
5858
"mod_haz": modified_haz,
5959
"mod_imp": modified_imp,
60-
"date": pd.Timestamp(2023),
60+
"date": pd.Timestamp("2023"),
6161
}
6262

6363

@@ -72,14 +72,14 @@ def test_not_from_factory_warning(mock_context):
7272
hazard=mock_context["haz"],
7373
impfset=mock_context["imp"],
7474
measure=None,
75-
date=2001,
75+
date="2001",
7676
)
7777

7878

7979
@pytest.mark.parametrize(
8080
"input_date,expected",
8181
[
82-
(2023, pd.Timestamp(2023, 1, 1)),
82+
("2023", pd.Timestamp(2023, 1, 1)),
8383
("2023-01-01", pd.Timestamp(2023, 1, 1)),
8484
(datetime.date(2023, 1, 1), pd.Timestamp(2023, 1, 1)),
8585
],
@@ -108,9 +108,14 @@ def test_init_invalid_date_format(mock_context):
108108
def test_init_invalid_date_type(mock_context):
109109
with pytest.raises(
110110
TypeError,
111-
match=r"date_arg must be an int, str, datetime.date or pandas.Timestamp",
111+
match=r"date_arg must be an str, datetime.date or pandas.Timestamp",
112112
):
113-
Snapshot.from_triplet(exposure=mock_context["exp"], hazard=mock_context["haz"], impfset=mock_context["imp"], date=2023.5) # type: ignore
113+
Snapshot.from_triplet(
114+
exposure=mock_context["exp"],
115+
hazard=mock_context["haz"],
116+
impfset=mock_context["imp"],
117+
date=2023.5,
118+
) # type: ignore
114119

115120

116121
def test_properties(mock_context):

0 commit comments

Comments
 (0)