Skip to content

Commit 05a0183

Browse files
committed
update tests
1 parent 80a26c1 commit 05a0183

File tree

2 files changed

+43
-6
lines changed

2 files changed

+43
-6
lines changed

src/titiler/xarray/tests/test_io_tools.py

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,6 @@ def test_get_variable():
6767
assert da["time"][0] == numpy.datetime64("2022-01-01")
6868
assert da["time"][1] == numpy.datetime64("2023-01-01")
6969

70-
da = get_variable(ds, "dataset", sel=["time=1st of January 2023"])
71-
assert da.rio.crs
72-
assert da.dims == ("y", "x")
73-
assert da["time"] == numpy.datetime64("2023-01-01")
74-
7570
# Select the Nearest Time
7671
da = get_variable(ds, "dataset", sel=["time=2024-01-01T01:00:00"], method="nearest")
7772
assert da.rio.crs
@@ -169,6 +164,46 @@ def test_get_variable():
169164
da = get_variable(ds, "dataset")
170165

171166

167+
def test_get_variable_datetime_tz():
168+
"""test io.get_variable with datetime and timezones."""
169+
arr = numpy.arange(0, 33 * 35 * 2).reshape(2, 33, 35)
170+
data = xarray.DataArray(
171+
arr,
172+
dims=("time", "y", "x"),
173+
coords={
174+
"x": numpy.arange(-170, 180, 10),
175+
"y": numpy.arange(-80, 85, 5),
176+
"time": [
177+
datetime(2022, 1, 1),
178+
datetime(2023, 1, 1),
179+
],
180+
},
181+
)
182+
data.attrs.update({"valid_min": arr.min(), "valid_max": arr.max()})
183+
assert not data.rio.crs
184+
assert data.dims == ("time", "y", "x")
185+
ds = data.to_dataset(name="dataset")
186+
187+
da = get_variable(ds, "dataset", sel=["time=2023-01-01T00:00:00"], method="nearest")
188+
assert da.rio.crs
189+
assert da.dims == ("y", "x")
190+
assert da["time"] == numpy.datetime64("2023-01-01")
191+
192+
da = get_variable(
193+
ds, "dataset", sel=["time=2023-01-01T00:00:00Z"], method="nearest"
194+
)
195+
assert da.rio.crs
196+
assert da.dims == ("y", "x")
197+
assert da["time"] == numpy.datetime64("2023-01-01")
198+
199+
da = get_variable(
200+
ds, "dataset", sel=["time=2023-01-01T00:00:00+03:00"], method="nearest"
201+
)
202+
assert da.rio.crs
203+
assert da.dims == ("y", "x")
204+
assert da["time"] == numpy.datetime64("2023-01-01")
205+
206+
172207
@pytest.mark.parametrize(
173208
"protocol,filename",
174209
[

src/titiler/xarray/titiler/xarray/io.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,8 +156,10 @@ def get_variable(
156156
for s in sel:
157157
val: Union[str, slice]
158158
dim, val = s.split("=")
159+
159160
# cast string to dtype of the dimension
160-
val = da[dim].dtype.type(val)
161+
if da[dim].dtype != "O":
162+
val = da[dim].dtype.type(val)
161163

162164
if dim in _idx:
163165
_idx[dim].append(val)

0 commit comments

Comments
 (0)