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

xarray does not seem to work with timezone aware indexes #5594

Open
brunorigal opened this issue Jul 12, 2021 · 3 comments
Open

xarray does not seem to work with timezone aware indexes #5594

brunorigal opened this issue Jul 12, 2021 · 3 comments

Comments

@brunorigal
Copy link

brunorigal commented Jul 12, 2021

Using the following code, the created index is not a time index but has object type:

import numpy as np
import pandas as pd
import xarray as xr

data = xr.DataArray(np.random.random(5), dims=['time'], coords={'time': pd.date_range("2000-01-01", periods=5).tz_localize('UTC')})
print(data.time)

This can severely limit the practicality of time indexes. It also means that methods such as resample won't work.

Environment:
I use xarray==0.18.2 with ubuntu

@max-sixty
Copy link
Collaborator

I think this a problem with the repr rather than the index:

  ...: print(data.time)
<xarray.DataArray 'time' (time: 5)>
array([946684800000000000, 946771200000000000, 946857600000000000,
       946944000000000000, 947030400000000000], dtype=object)
Coordinates:
  * time     (time) object 946684800000000000 ... 947030400000000000

In [2]: data.indexes['time']
Out[2]:
DatetimeIndex(['2000-01-01 00:00:00+00:00', '2000-01-02 00:00:00+00:00',
               '2000-01-03 00:00:00+00:00', '2000-01-04 00:00:00+00:00',
               '2000-01-05 00:00:00+00:00'],
              dtype='datetime64[ns, UTC]', name='time', freq='D')

But I wouldn't be surprised if there were issues with this sort of index — please post issues if you find them.

@brunorigal
Copy link
Author

For example resample yields the following error:

>>> data.resample(time='1d')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/bruno/miniconda3/envs/torch/lib/python3.8/site-packages/xarray/core/common.py", line 1173, in resample
    resampler = self._resample_cls(
  File "/home/bruno/miniconda3/envs/torch/lib/python3.8/site-packages/xarray/core/resample.py", line 175, in __init__
    super().__init__(*args, **kwargs)
  File "/home/bruno/miniconda3/envs/torch/lib/python3.8/site-packages/xarray/core/groupby.py", line 348, in __init__
    full_index, first_items = self._get_index_and_items(index, grouper)
  File "/home/bruno/miniconda3/envs/torch/lib/python3.8/site-packages/xarray/core/groupby.py", line 449, in _get_index_and_items
    first_items = s.groupby(grouper).first()
  File "/home/bruno/miniconda3/envs/torch/lib/python3.8/site-packages/pandas/core/series.py", line 1720, in groupby
    return SeriesGroupBy(
  File "/home/bruno/miniconda3/envs/torch/lib/python3.8/site-packages/pandas/core/groupby/groupby.py", line 560, in __init__
    grouper, exclusions, obj = get_grouper(
  File "/home/bruno/miniconda3/envs/torch/lib/python3.8/site-packages/pandas/core/groupby/grouper.py", line 719, in get_grouper
    binner, grouper, obj = key._get_grouper(obj, validate=False)
  File "/home/bruno/miniconda3/envs/torch/lib/python3.8/site-packages/pandas/core/resample.py", line 1484, in _get_grouper
    r = self._get_resampler(obj)
  File "/home/bruno/miniconda3/envs/torch/lib/python3.8/site-packages/pandas/core/resample.py", line 1476, in _get_resampler
    raise TypeError(
TypeError: Only valid with DatetimeIndex, TimedeltaIndex or PeriodIndex, but got an instance of 'Index'

@dcherian
Copy link
Contributor

The resample issue is tracked in #1490. In general, (IIUC) we use numpy arrays for the data and those don't support timezones. So it's a mess (#1490 (comment))

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

No branches or pull requests

3 participants