File: unstacking.py

package info (click to toggle)
python-xarray 2025.08.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 11,796 kB
  • sloc: python: 115,416; makefile: 258; sh: 47
file content (64 lines) | stat: -rw-r--r-- 1,863 bytes parent folder | download | duplicates (2)
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import numpy as np
import pandas as pd

import xarray as xr

from . import requires_dask, requires_sparse


class Unstacking:
    def setup(self):
        data = np.random.default_rng(0).random((250, 500))
        self.da_full = xr.DataArray(data, dims=list("ab")).stack(flat_dim=[...])
        self.da_missing = self.da_full[:-1]
        self.df_missing = self.da_missing.to_pandas()

    def time_unstack_fast(self):
        self.da_full.unstack("flat_dim")

    def time_unstack_slow(self):
        self.da_missing.unstack("flat_dim")

    def time_unstack_pandas_slow(self):
        self.df_missing.unstack()


class UnstackingDask(Unstacking):
    def setup(self, *args, **kwargs):
        requires_dask()
        super().setup(**kwargs)
        self.da_full = self.da_full.chunk({"flat_dim": 25})


class UnstackingSparse(Unstacking):
    def setup(self, *args, **kwargs):
        requires_sparse()

        import sparse

        data = sparse.random((500, 1000), random_state=0, fill_value=0)
        self.da_full = xr.DataArray(data, dims=list("ab")).stack(flat_dim=[...])
        self.da_missing = self.da_full[:-1]

        mindex = pd.MultiIndex.from_arrays([np.arange(100), np.arange(100)])
        self.da_eye_2d = xr.DataArray(np.ones((100,)), dims="z", coords={"z": mindex})
        self.da_eye_3d = xr.DataArray(
            np.ones((100, 50)),
            dims=("z", "foo"),
            coords={"z": mindex, "foo": np.arange(50)},
        )

    def time_unstack_to_sparse_2d(self):
        self.da_eye_2d.unstack(sparse=True)

    def time_unstack_to_sparse_3d(self):
        self.da_eye_3d.unstack(sparse=True)

    def peakmem_unstack_to_sparse_2d(self):
        self.da_eye_2d.unstack(sparse=True)

    def peakmem_unstack_to_sparse_3d(self):
        self.da_eye_3d.unstack(sparse=True)

    def time_unstack_pandas_slow(self):
        pass