python - Applying a custom function to Xarray resample drops the dimension coordinates - Stack Overflow

When using the map method to apply a custom function in Xarray resample, the dimension coordinates are

When using the map method to apply a custom function in Xarray resample, the dimension coordinates are lost, and the resulting Dataset has a sequence instead of the actual coordinate values.

The following MWE will properly demonstrate the issue:

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

def custom_fn(ds):
    return ds

if __name__=='__main__':
    times=pd.date_range(
            '2000-01-01 00:00', '2000-01-05 23:59:59',
            freq='5min',
            )
    data1=np.arange(len(times))
    data2=data1+1
    data=xr.Dataset(
            data_vars={
                    'data1':(['time',], data1,),
                    'data2':(['time',], data2,),
                    },
            coords={'time':times}
            )

    new=data.resample(time='30min').map(custom_fn)
    print(data)
    print(new)

And the output is:

'''

<xarray.Dataset> Size: 35kB
Dimensions: (time: 1440)
Coordinates:
* time (time) datetime64[ns] 12kB 2000-01-01 ... 2000-01-05T23:55:00
Data variables:
data1 (time) int64 12kB 0 1 2 3 4 5 6 ... 1434 1435 1436 1437 1438 1439
data2 (time) int64 12kB 1 2 3 4 5 6 7 ... 1435 1436 1437 1438 1439 1440

<xarray.Dataset> Size: 23kB
Dimensions: (time: 1440)
Dimensions without coordinates: time
Data variables:
data1 (time) int64 12kB 0 1 2 3 4 5 6 ... 1434 1435 1436 1437 1438 1439
data2 (time) int64 12kB 1 2 3 4 5 6 7 ... 1435 1436 1437 1438 1439 1440

'''

I have kept the custom_fn very simple, notice that the result has no time coordinate values. I used breakpoint inside the custom function if it is not receiving the coordinates, but it is. So, I assume the issue is with how map function (or whatever) is merging the results.

Does anyone know what is happning and how ot fix it?

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744929160a4601626.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信