question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

DataArray resample does not work if the time coordinate name is different from the corresponding dimension name.

See original GitHub issue

Please see the example below and the output it produces. And the exception is not really helpful…


from xarray import DataArray
from datetime import datetime, timedelta


def main():
    N = 48
    data = range(N)

    dates = [datetime(2001, 1, 1) + timedelta(hours=i) for i in range(N)]

    # time variable is called the same as time dimension: works
    scheme1 = {
        "dims": "t",
        "coords": {"t": {"dims": "t", "data": dates}},
        "data": data
    }

    # time variable is called differently from the time dimension: does not work
    scheme2 = {
        "dims": "t",
        "coords": {"time": {"dims": "t", "data": dates}},
        "data": data
    }

   # Create DataArray objects using the above defined schemes
    a1 = DataArray.from_dict(scheme1)
    print(a1)
    print(a1.resample("D", "t"))
    print("=======================")

    a2 = DataArray.from_dict(scheme2)
    print(a2)
    print(a2.resample("D", "t"))


if __name__ == '__main__':
    main()

Below is the output I get from running the script:

<xarray.DataArray (t: 48)>
array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16,
       17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33,
       34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47])
Coordinates:
  * t        (t) datetime64[ns] 2001-01-01 2001-01-01T01:00:00 ...
<xarray.DataArray (t: 2)>
array([ 11.5,  35.5])
Coordinates:
  * t        (t) datetime64[ns] 2001-01-01 2001-01-02
=======================
<xarray.DataArray (t: 48)>
array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16,
       17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33,
       34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47])
Coordinates:
    time     (t) datetime64[ns] 2001-01-01 2001-01-01T01:00:00 ...
  * t        (t) int64 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 ...
Traceback (most recent call last):
  File "/Users/huziy/skynet2/RESCUE/skynet3_rech1/huziy/Netbeans Projects/Python/RPN/src/demo/xarray/time_dim_and_ops.py", line 46, in <module>
    main()
  File "/Users/huziy/skynet2/RESCUE/skynet3_rech1/huziy/Netbeans Projects/Python/RPN/src/demo/xarray/time_dim_and_ops.py", line 38, in main
    print(a2.resample("D", "t"))
  File "/Users/huziy/virtualenvs/py3.4/lib/python3.4/site-packages/xarray/core/common.py", line 499, in resample
    gb = self.groupby_cls(self, group, grouper=time_grouper)
  File "/Users/huziy/virtualenvs/py3.4/lib/python3.4/site-packages/xarray/core/groupby.py", line 202, in __init__
    first_items = s.groupby(grouper).first()
  File "/Users/huziy/virtualenvs/py3.4/lib/python3.4/site-packages/pandas/core/generic.py", line 3974, in groupby
    **kwargs)
  File "/Users/huziy/virtualenvs/py3.4/lib/python3.4/site-packages/pandas/core/groupby.py", line 1501, in groupby
    return klass(obj, by, **kwds)
  File "/Users/huziy/virtualenvs/py3.4/lib/python3.4/site-packages/pandas/core/groupby.py", line 370, in __init__
    mutated=self.mutated)
  File "/Users/huziy/virtualenvs/py3.4/lib/python3.4/site-packages/pandas/core/groupby.py", line 2403, in _get_grouper
    binner, grouper, obj = key._get_grouper(obj)
  File "/Users/huziy/virtualenvs/py3.4/lib/python3.4/site-packages/pandas/tseries/resample.py", line 1061, in _get_grouper
    r = self._get_resampler(obj)
  File "/Users/huziy/virtualenvs/py3.4/lib/python3.4/site-packages/pandas/tseries/resample.py", line 1057, in _get_resampler
    "but got an instance of %r" % type(ax).__name__)
TypeError: Only valid with DatetimeIndex, TimedeltaIndex or PeriodIndex, but got an instance of 'Int64Index'


Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:11 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
shoyercommented, Nov 10, 2017

@chrwerner Yes, exactly.

0reactions
stale[bot]commented, Dec 24, 2019

In order to maintain a list of currently relevant issues, we mark issues as stale after a period of inactivity

If this issue remains relevant, please comment here or remove the stale label; otherwise it will be marked as closed automatically

Read more comments on GitHub >

github_iconTop Results From Across the Web

Resample DataArray on only one of TWO time dimensions
Issue: I have a DataArray with two time dimensions (initialization time and a forecast time. When I try to resample over just one...
Read more >
Data Structures - Xarray
DataArray is xarray's implementation of a labeled, multi-dimensional array. ... is the dimension name and the second element is the corresponding coordinate ......
Read more >
Xarray Interpolation, Groupby, Resample, Rolling, and Coarsen
In the previous lesson on Xarray, we learned how to select data based on its dimension coordinates and align data with dimension different...
Read more >
xarray.DataArray.resample
You are not reading the most recent version of this documentation. v2022.12.0 is the latest ... Name of the dimension to resample along...
Read more >
Source code for cf_xarray.accessor - CF Xarray
It should return a list in all other cases including when there are no results ... Variable name(s) in parent xarray object that...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found