Skip to content

Commit 6da3e47

Browse files
committed
Handle infer_datetime_format bug
With #27, the `tz_convert` would fail when we tripped over pandas-dev/pandas#41047, since the DatetimeIndex would be tz-naive. Now, we assume tz-naive datetimes are already in UTC. Addresses #33 (comment)
1 parent 6203653 commit 6da3e47

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed

stackstac/prepare.py

+14-5
Original file line numberDiff line numberDiff line change
@@ -344,13 +344,22 @@ def to_coords(
344344
band_coords: bool = True,
345345
) -> Tuple[Dict[str, Union[pd.Index, np.ndarray, list]], List[str]]:
346346

347+
times = pd.to_datetime(
348+
[item["properties"]["datetime"] for item in items],
349+
infer_datetime_format=True,
350+
errors="coerce",
351+
)
352+
if times.tz is not None:
353+
# xarray can't handle tz-aware DatetimeIndexes, so we convert to UTC and drop the timezone
354+
# https://github.com/pydata/xarray/issues/3291.
355+
# The `tz is None` case is typically a manifestation of https://github.com/pandas-dev/pandas/issues/41047.
356+
# Since all STAC timestamps should be UTC (https://github.com/radiantearth/stac-spec/issues/1095),
357+
# we feel safe assuming that any tz-naive datetimes are already in UTC.
358+
times = times.tz_convert(None)
359+
347360
dims = ["time", "band", "y", "x"]
348361
coords = {
349-
"time": pd.to_datetime(
350-
[item["properties"]["datetime"] for item in items],
351-
infer_datetime_format=True,
352-
errors="coerce",
353-
).tz_convert('UTC').tz_localize(None),
362+
"time": times,
354363
"id": xr.Variable("time", [item["id"] for item in items]),
355364
"band": asset_ids,
356365
}

0 commit comments

Comments
 (0)