Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error using .sel on time index if timestamp not in np.datetime64 #240

Open
JotaFan opened this issue Feb 6, 2024 · 0 comments
Open

Error using .sel on time index if timestamp not in np.datetime64 #240

JotaFan opened this issue Feb 6, 2024 · 0 comments

Comments

@JotaFan
Copy link

JotaFan commented Feb 6, 2024

While trying to use .sel to get certain times from the xarray if the timestamp used for compared was not in np.datetime64 it would either:

  1. Not show any result although there was a clear result
  2. Error comparing (using method)

Code to replicate:


import dateutil
import pystac_client
import shapely
import stackstac

URL = "https://earth-search.aws.element84.com/v1"
catalog = pystac_client.Client.open(URL)
collection = ["sentinel-2-l2a"]

start_date = dateutil.parser.parse("2019-03-21")

end_date = dateutil.parser.parse("2019-06-01")

poly_wkt = 'POLYGON ((-6.492295176188869 41.34667865915695, -6.492295176188869 41.35406317236619, -6.512299741612849 41.35406317236619, -6.512299741612849 41.34667865915695, -6.492295176188869 41.34667865915695))'
polygon = shapely.from_wkt(poly_wkt)

dt = f"{start_date.isoformat()}/{end_date.isoformat()}"
items = catalog.search(
    intersects=polygon,
    collections=collection,
    datetime=dt,
).item_collection()

stack = stackstac.stack(items, epsg=32630)

Now to get the erros:
<class 'datetime.datetime'>


tt = items[0].datetime
stack.sel(time=tt)

error message:
KeyError: "not all values found in index 'time'. Try setting the method keyword argument (example: method='nearest')."

with method:
TypeError: Cannot compare dtypes datetime64[ns] and datetime64[ns, tzutc()]

I find an issue was needed, although a easy fix is doable (just pass to np.datatime64), many datasets dont have the timestamp in that format so, an easy fix on this side was just to change the formating whenever the indexing selection occures.

If you agree I can open a PR to solve this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant