home / github / issues

Menu
  • Search all tables
  • GraphQL API

issues: 1421180629

This data as json

id node_id number title user state locked assignee milestone comments created_at updated_at closed_at author_association active_lock_reason draft pull_request body reactions performed_via_github_app state_reason repo type
1421180629 I_kwDOAMm_X85UtX7V 7207 Difficulties with selecting from numpy.datetime64[ns] dimensions 4753005 closed 0     3 2022-10-24T17:35:01Z 2022-10-24T22:45:36Z 2022-10-24T22:45:36Z NONE      

What is your issue?

I have a DataArray ("spgs") containing time-frequency data, with a time dimension of dtype numpy.datetime64[ns]. I used to be able to select using: ```

Select using datetime strings

spgs.sel(time=slice("2022-10-13T09:00:00", "2022-10-13T21:00:00")

Select using Timestamp objects

rng = tuple(pd.to_datetime(x) for x in ["2022-10-13T09:00:00", "2022-10-13T21:00:00"]) spgs.sel(time=slice(rng)) # Select using numpy.datetime64[ns] objects, such that rng[0].dtype == spgs.time.values.dtype rng = tuple(pd.to_datetime(["2022-10-13T09:00:00", "2022-10-13T21:00:00"]).values) spg.sel(time=slice(rng)) None of these work after upgrading to v2022.10.0. The first method yields: Traceback (most recent call last): File "<string>", line 1, in <module> File "/home/gfindlay/miniconda3/envs/seahorse/lib/python3.10/site-packages/xarray/core/dataarray.py", line 1523, in sel ds = self._to_temp_dataset().sel( File "/home/gfindlay/miniconda3/envs/seahorse/lib/python3.10/site-packages/xarray/core/dataset.py", line 2550, in sel query_results = map_index_queries( File "/home/gfindlay/miniconda3/envs/seahorse/lib/python3.10/site-packages/xarray/core/indexing.py", line 183, in map_index_queries results.append(index.sel(labels, **options)) # type: ignore[call-arg] File "/home/gfindlay/miniconda3/envs/seahorse/lib/python3.10/site-packages/xarray/core/indexes.py", line 434, in sel indexer = _query_slice(self.index, label, coord_name, method, tolerance) File "/home/gfindlay/miniconda3/envs/seahorse/lib/python3.10/site-packages/xarray/core/indexes.py", line 210, in _query_slice raise KeyError( KeyError: "cannot represent labeled-based slice indexer for coordinate 'time' with a slice over integer positions; the index is unsorted or non-unique" The second two methods yield: Traceback (most recent call last): File "pandas/_libs/index.pyx", line 545, in pandas._libs.index.DatetimeEngine.get_loc File "pandas/_libs/hashtable_class_helper.pxi", line 2131, in pandas._libs.hashtable.Int64HashTable.get_item File "pandas/_libs/hashtable_class_helper.pxi", line 2140, in pandas._libs.hashtable.Int64HashTable.get_item KeyError: 1665651600000000000 ... KeyError: Timestamp('2022-10-13 09:00:00') Interestingly, this works: start = spgs.time.values.min() stop = spgs.time.values.max() spgs.sel(time=slice(start, stop)) This does not: start = spgs.time.values.min() stop = start + pd.to_timedelta('10s') spgs.sel(time=slice(start, stop)) ```

I filed this as an issue and not a bug, because from reading other issues here and over at pandas, it seems like this may be an unintended consequence of changes to Datetime/Timestamp handling, especially within pandas, rather than a bug with xarray per se. This is supported by the fact that downgrading xarray to 2022.9.0, without touching other dependencies (e.g. pandas), does not restore the old behavior.

{
    "url": "https://api.github.com/repos/pydata/xarray/issues/7207/reactions",
    "total_count": 0,
    "+1": 0,
    "-1": 0,
    "laugh": 0,
    "hooray": 0,
    "confused": 0,
    "heart": 0,
    "rocket": 0,
    "eyes": 0
}
  completed 13221727 issue

Links from other tables

  • 1 row from issues_id in issues_labels
  • 3 rows from issue in issue_comments
Powered by Datasette · Queries took 0.621ms · About: xarray-datasette