是否可以附加到xarray.Dataset? [英] Is it possible to append to an xarray.Dataset?

查看:88
本文介绍了是否可以附加到xarray.Dataset?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用.append()方法连接熊猫中的两个表(具有相同的字段).不幸的是,xarray中不存在此方法,还有另一种方法吗?

I've been using the .append() method to concatenate two tables (with the same fields) in pandas. Unfortunately this method does not exist in xarray, is there another way to do it?

推荐答案

Xarray不具有append方法,因为它的数据结构建立在NumPy不可调整大小的数组之上,因此我们不能在不复制整个元素的情况下追加新元素大批.因此,我们不实现append方法.相反,您应该使用 xarray.concat .

Xarray doesn't have an append method because its data structures are built on top of NumPy's non-resizable arrays, so we cannot append new elements without copying the entire array. Hence, we don't implement an append method. Instead, you should use xarray.concat.

一种常见的模式是在列表中累积Dataset/DataArray对象,并在末尾连接一次:

One usual pattern is to accumulate Dataset/DataArray objects in a list, and concatenate once at the end:

datasets = []
for example in examples:
    ds = create_an_xarray_dataset(example)
    datasets.append(ds)
combined = xarray.concat(datasets, dim='example')

您不想在循环内进行连接-那样会使您的代码在二次时间内运行.

You don't want to concatenate inside the loop -- that would make your code run in quadratic time.

或者,您可以为结果分配一个Dataset/DataArray,然后 使用索引填充值,例如,

Alternatively, you could allocate a single Dataset/DataArray for the result, and fill in the values with indexing, e.g.,

dims = ('example', 'x', 'y')
combined = xarray.Dataset(
    data_vars={'my_variable': (dims, np.zeros((len(examples), 100, 200)))},
    coords={'example': examples})
for example in examples:
    combined.loc[dict(example=example)] = create_an_xarray_dataset(example)

(请注意,您始终需要使用带有[].loc[]之类的方括号的索引-用sel()isel()分配是行不通的.)

(Note that you always need to use indexing with square brackets like [] or .loc[] -- assigning with sel() and isel() doesn't work.)

这两种方法同样有效-实际上是一种品味,一个对您来说看起来更好还是对您的应用程序更好.

These two approaches are equally efficient -- it's really a matter of taste which one looks better to you or works better for your application.

就其价值而言,pandas具有相同的局限性:append方法确实确实在每次使用时复制整个数据帧.对于新用户来说,这是一个永久的惊喜,也是性能问题的根源.因此,我确实认为我们做出了正确的设计决策,而不将其包括在xarray中.

For what it's worth, pandas has the same limitation: the append method does indeed copy entire dataframes each time it is used. This is a perpetual surprise and source of performance issues for new users. So I do think that we made the right design decision not including it in xarray.

这篇关于是否可以附加到xarray.Dataset?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆