Skip to content

Commit a5f2a3a

Browse files
Merge pull request #1168 from developmentseed/ab/add-sel-by-datetime-to-xarray
Add datetime to _cast_to_type
2 parents c01062f + 0fed3c9 commit a5f2a3a

File tree

3 files changed

+48
-21
lines changed

3 files changed

+48
-21
lines changed

CHANGES.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## Unreleased
44

5+
### titiler.xarray
6+
7+
* use dimension's `dtype` to cast user *selection*
8+
59
## 0.22.2 (2025-06-02)
610

711
### titiler.application

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: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
from urllib.parse import urlparse
55

66
import attr
7-
import numpy
8-
import pandas
97
import xarray
108
from morecantile import TileMatrixSet
119
from rio_tiler.constants import WEB_MERCATOR_TMS
@@ -133,19 +131,6 @@ def _arrange_dims(da: xarray.DataArray) -> xarray.DataArray:
133131
return da
134132

135133

136-
def _cast_to_type(value, dtype: Any) -> Any:
137-
if "timedelta" in str(dtype):
138-
value = pandas.to_timedelta(value)
139-
140-
elif numpy.issubdtype(dtype, numpy.integer):
141-
value = int(value)
142-
143-
elif numpy.issubdtype(dtype, numpy.floating):
144-
value = float(value)
145-
146-
return value
147-
148-
149134
def get_variable(
150135
ds: xarray.Dataset,
151136
variable: str,
@@ -171,7 +156,10 @@ def get_variable(
171156
for s in sel:
172157
val: Union[str, slice]
173158
dim, val = s.split("=")
174-
val = _cast_to_type(val, da[dim].dtype)
159+
160+
# cast string to dtype of the dimension
161+
if da[dim].dtype != "O":
162+
val = da[dim].dtype.type(val)
175163

176164
if dim in _idx:
177165
_idx[dim].append(val)

0 commit comments

Comments
 (0)