File: test_3694_to_dataframe_align_multindex.py

package info (click to toggle)
python-awkward 2.9.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 25,524 kB
  • sloc: python: 187,940; cpp: 33,928; sh: 432; makefile: 21; javascript: 8
file content (72 lines) | stat: -rw-r--r-- 1,505 bytes parent folder | download
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
65
66
67
68
69
70
71
72
from __future__ import annotations

import numpy as np
import pytest

import awkward as ak

pd = pytest.importorskip("pandas")


_data = {
    "x": ["abc", "FG_12345"],
    "y": [None, ["g1", "g2"]],
}

_expected_inner = pd.DataFrame(
    {
        "x": ["FG_12345", "FG_12345"],
        "y": ["g1", "g2"],
    },
    index=pd.MultiIndex.from_tuples(
        [(1, 0), (1, 1)],
        names=["entry", "subentry"],
    ),
)

_expected_outer = pd.DataFrame(
    {
        "x": ["abc", "FG_12345", "FG_12345"],
        "y": [np.nan, "g1", "g2"],
    },
    index=pd.MultiIndex.from_tuples(
        [(0, 0), (1, 0), (1, 1)],
        names=["entry", "subentry"],
    ),
)

_expected_left = pd.DataFrame(
    {
        "x": ["abc", "FG_12345", "FG_12345"],
        "y": [np.nan, "g1", "g2"],
    },
    index=pd.MultiIndex.from_tuples(
        [(0, 0), (1, 0), (1, 1)],
        names=["entry", "subentry"],
    ),
)

_expected_right = pd.DataFrame(
    {
        "x": ["FG_12345", "FG_12345"],
        "y": ["g1", "g2"],
    },
    index=pd.MultiIndex.from_tuples(
        [(1, 0), (1, 1)],
        names=["entry", "subentry"],
    ),
)

params = [
    ("inner", _expected_inner),
    ("outer", _expected_outer),
    ("left", _expected_left),
    ("right", _expected_right),
]


@pytest.mark.parametrize("how, expected", params)
def test_merge_option(how: str, expected: pd.DataFrame) -> None:
    a = ak.Array(_data)
    actual = ak.to_dataframe(a, how=how)
    pd.testing.assert_frame_equal(actual, expected)