Skip to content

Commit 503c039

Browse files
Stricter implementation of _cast_to_type
1 parent cfac52c commit 503c039

File tree

1 file changed

+16
-5
lines changed
  • src/titiler/xarray/titiler/xarray

1 file changed

+16
-5
lines changed

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

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -134,11 +134,22 @@ def _arrange_dims(da: xarray.DataArray) -> xarray.DataArray:
134134

135135

136136
def _cast_to_type(value, dtype: Any) -> Any:
137-
if "timedelta" in str(dtype):
138-
value = pandas.to_timedelta(value)
139-
140-
if "datetime" in str(dtype):
141-
value = pandas.to_datetime(value)
137+
# Convert dtype to numpy dtype for consistent handling
138+
if hasattr(dtype, "type"):
139+
np_dtype = dtype
140+
else:
141+
np_dtype = numpy.dtype(dtype)
142+
143+
# Explicit datetime64 handling
144+
if np_dtype.kind == "M": # 'M' is numpy's datetime kind
145+
# Always parse as UTC first, then remove timezone - Handles "Z" suffix correctly
146+
parsed_value = pandas.to_datetime(value, utc=True)
147+
# .tz_localize(None) - Removes timezone information, making the datetime "naive"
148+
return parsed_value.tz_localize(None)
149+
150+
# Explicit timedelta64 handling
151+
elif np_dtype.kind == "m": # 'm' is numpy's timedelta kind
152+
return pandas.to_timedelta(value)
142153

143154
elif numpy.issubdtype(dtype, numpy.integer):
144155
value = int(value)

0 commit comments

Comments
 (0)