Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
485 views
in Technique[技术] by (71.8m points)

python - Is it possible to append to an xarray.Dataset?

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

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.

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)

(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.

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...