Skip to content

Commit

Permalink
fix errors
Browse files Browse the repository at this point in the history
  • Loading branch information
OnnoEbbens committed Oct 11, 2024
1 parent 8d21573 commit bb1b115
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 12 deletions.
1 change: 1 addition & 0 deletions nlmod/dims/time.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,7 @@ def set_ds_time(
if isinstance(start, (int, np.integer, float)):
if isinstance(time[0], (int, np.integer, float, str)):
raise TypeError("Make sure 'start' or 'time' argument is a valid TimeStamp")
start = time[0] - pd.to_timedelta(start, "D")
elif isinstance(start, str):
start = pd.Timestamp(start)
elif isinstance(start, (pd.Timestamp, cftime.datetime)):
Expand Down
30 changes: 18 additions & 12 deletions tests/test_016_time.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ def test_time_out_of_bounds():
)

# start cf.datetime and time list of str (no general method to convert str to cftime)
with pytest.raises(TypeError):
with pytest.raises(OutOfBoundsDatetime):
nlmod.dims.set_ds_time(ds, start=start_model, time=["1000-01-02", "1000-01-03"])

# start cf.datetime and perlen int
Expand All @@ -126,27 +126,31 @@ def test_time_out_of_bounds():
_ = nlmod.dims.set_ds_time(ds, start="1000-01-01", time=cftime_ind)

# start str and time int
_ = nlmod.dims.set_ds_time(ds, start="1000-01-01", time=1)
with pytest.raises(OutOfBoundsDatetime):
nlmod.dims.set_ds_time(ds, start="1000-01-01", time=1)

# start str and time list of int
_ = nlmod.dims.set_ds_time(ds, start="1000-01-01", time=[10, 20, 21, 55])
with pytest.raises(OutOfBoundsDatetime):
nlmod.dims.set_ds_time(ds, start="1000-01-01", time=[10, 20, 21, 55])

# start str and time list of timestamp
_ = nlmod.dims.set_ds_time(
ds, start="1000-01-01", time=pd.to_datetime(["2000-2-1", "2000-3-1"])
)

# start str and time list of str (no general method to convert str to cftime)
with pytest.raises(TypeError):
with pytest.raises(OutOfBoundsDatetime):
nlmod.dims.set_ds_time(ds, start="1000-01-01", time=["1000-2-1", "1000-3-1"])

# start str and perlen int
_ = nlmod.dims.set_ds_time(ds, start="1000-01-01", perlen=365000)
with pytest.raises(OutOfBoundsTimedelta):
nlmod.dims.set_ds_time(ds, start="1000-01-01", perlen=365000)

# start numpy datetime and perlen list of int
_ = nlmod.dims.set_ds_time(
with pytest.raises(OutOfBoundsDatetime):
nlmod.dims.set_ds_time(
ds, start=np.datetime64("1000-01-01"), perlen=[10, 100, 24]
)
)

# start numpy datetime and time list of timestamps
_ = nlmod.dims.set_ds_time(
Expand All @@ -156,13 +160,14 @@ def test_time_out_of_bounds():
)

# start numpy datetime and time list of str
with pytest.raises(TypeError):
with pytest.raises(OutOfBoundsDatetime):
nlmod.dims.set_ds_time(
ds, start=np.datetime64("1000-01-01"), time=["1000-2-1", "1000-3-1"]
)

# start timestamp and perlen list of int
_ = nlmod.dims.set_ds_time(
with pytest.raises(OutOfBoundsDatetime):
nlmod.dims.set_ds_time(
ds, start=pd.Timestamp("1000-01-01"), perlen=[10, 100, 24]
)

Expand All @@ -173,7 +178,8 @@ def test_time_out_of_bounds():
_ = nlmod.dims.set_ds_time(ds, start=96500, time=cftime_ind)

# start int and time timestamp
_ = nlmod.dims.set_ds_time(ds, start=96500, time=pd.Timestamp("1000-01-01"))
with pytest.raises(OutOfBoundsDatetime):
nlmod.dims.set_ds_time(ds, start=96500, time=pd.Timestamp("1000-01-01"))

# start int and time str
with pytest.raises(TypeError):
Expand All @@ -184,9 +190,9 @@ def test_numerical_time_index():
ds = nlmod.get_ds([0, 1000, 2000, 3000])

# start str and time floats
_ = nlmod.dims.set_ds_time_numerical(ds, start="2000-1-1", time=[10.0, 20.0, 30.1])
_ = nlmod.dims.set_ds_time_numeric(ds, start="2000-1-1", time=[10.0, 20.0, 30.1])

# start timestamp and time ints
_ = nlmod.dims.set_ds_time_numerical(
_ = nlmod.dims.set_ds_time_numeric(
ds, start=pd.Timestamp("1000-01-01"), time=[10, 20, 30]
)

0 comments on commit bb1b115

Please sign in to comment.