home / github / issue_comments

Menu
  • Search all tables
  • GraphQL API

issue_comments: 426250976

This data as json

html_url issue_url id node_id user created_at updated_at author_association body reactions performed_via_github_app issue
https://github.com/pydata/xarray/issues/2450#issuecomment-426250976 https://api.github.com/repos/pydata/xarray/issues/2450 426250976 MDEyOklzc3VlQ29tbWVudDQyNjI1MDk3Ng== 6628425 2018-10-02T12:16:03Z 2018-10-02T12:16:03Z MEMBER

Thanks @am-thyst, this happens to be expected behavior. For a dimension indexed by a DatetimeIndex (e.g. your 'time' dimension), label-based indexing with strings uses so-called "partial string indexing". This behavior in xarray is inherited from pandas. Essentially what it means is that if you provide a string to a label-based indexer (like .loc), then it will return values from all times that would begin with the string you provided if expressed in string form. So, in your example, if you provide only the year, month, and day (e.g. '1979-01-01') it will return values from '1979-01-01' and 1979-01-01T12:00:00, because both string representations start with '1979-01-01'.

As you inferred, if you add detail to the string you provide, you can refine the selection. That said, you need to be careful of the format you use (I think the reason you get a KeyError in your second example is that pandas does not know how to interpret the string you provided). While pandas has some flexibility in how it parses datetime-strings I might recommend sticking to the ISO 8601 format, which should work in all cases. For example to select just the values associated with the first date in your array you could do something like this: ``` In [1]: import numpy as np; import pandas as pd; import xarray as xr

In [2]: times = pd.date_range('1979', periods=8, freq='12H')

In [3]: da = xr.DataArray(np.arange(8), coords=[times], dims=['time'])

In [4]: da Out[4]: <xarray.DataArray (time: 8)> array([0, 1, 2, 3, 4, 5, 6, 7]) Coordinates: * time (time) datetime64[ns] 1979-01-01 ... 1979-01-04T12:00:00

In [5]: da.loc['1979-01-01T00'] Out[5]: <xarray.DataArray ()> array(0) Coordinates: time datetime64[ns] 1979-01-01 One other thing to note, in general, is that you can pass datetimes themselves to label-based indexers if you want to select specific dates; this way you don't need to go through the intermediate step of converting a date to a string: In [6]: da.loc[datetime(1979, 1, 1)] Out[6]: <xarray.DataArray ()> array(0) Coordinates: time datetime64[ns] 1979-01-01 ```

{
    "total_count": 0,
    "+1": 0,
    "-1": 0,
    "laugh": 0,
    "hooray": 0,
    "confused": 0,
    "heart": 0,
    "rocket": 0,
    "eyes": 0
}
  365438396
Powered by Datasette · Queries took 78.131ms · About: xarray-datasette