# How to save out-of-memory Xarray file to .csv

**URL:** <https://discourse.pangeo.io/t/how-to-save-out-of-memory-xarray-file-to-csv/3579>\
**Category:** Data\
**Created:** [July 20, 2023, 7:05pm UTC](https://discourse.pangeo.io/t/how-to-save-out-of-memory-xarray-file-to-csv/3579 "2023-07-20T19:05:03Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![rabernat](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.pangeo.io/rabernat/32/22_2.png) [@rabernat](https://discourse.pangeo.io/u/rabernat)\
**Post date:** [July 20, 2023, 7:31pm UTC](https://discourse.pangeo.io/t/how-to-save-out-of-memory-xarray-file-to-csv/3579/2 "2023-07-20T19:31:17Z")

</div>

Thanks for posting this interesting question.

> [@BEStecher](#):
>
> Which leaves me with the exact data I want (yay!)

I’m not convinced that this is actually the exact data you want. Based on your description, it looks like you want a dataset with 1121 different points in it (and 31411 timesteps); i.e. a 2D array. However, here you have a 3D array (31411 x 1121 x 1121); a subsample of your original datacube. This is 1121 times bigger than what you want, which could be why you are running out of memory.

You should probably be using Xarray’s “vectorized indexing” here:

> **[Indexing and selecting data](https://docs.xarray.dev/en/stable/user-guide/indexing.html#vectorized-indexing)**
>
> Xarray offers extremely flexible indexing routines that combine the best features of NumPy and pandas for data selection. The most basic way to access elements of a DataArray object is to use Pytho...

For example, something like

```python
lat_index = xr.DataArray(gem_df.lat, dims="point")
lon_index = xr.DataArray(gem_df.lon, dims="point")
data = xarray_data_ssp126.sel(lon=lon_index, lat=lat_index, method="nearest")

```

I’m still not certain that will work properly with Dask. But at least it should be the right data structure.

Try this out and report back here!

---

_[View the full topic](https://discourse.pangeo.io/t/how-to-save-out-of-memory-xarray-file-to-csv/3579)._
