|
| 1 | +import numpy as np |
| 2 | +import pytest |
| 3 | + |
| 4 | +import pandas as pd |
| 5 | +from pandas.util import testing as tm |
| 6 | + |
| 7 | + |
| 8 | +def test_error(): |
| 9 | + df = pd.DataFrame( |
| 10 | + {"A": pd.Series([[0, 1, 2], np.nan, [], (3, 4)], index=list("abcd")), "B": 1} |
| 11 | + ) |
| 12 | + df.columns = list("AA") |
| 13 | + with pytest.raises(ValueError): |
| 14 | + df.explode(subset=list("AA")) |
| 15 | + |
| 16 | + |
| 17 | +def test_basic(): |
| 18 | + df = pd.DataFrame( |
| 19 | + {"A": pd.Series([[0, 1, 2], np.nan, [], (3, 4)], index=list("abcd")), "B": 1} |
| 20 | + ) |
| 21 | + result = df.explode(subset=["A"]) |
| 22 | + expected = pd.DataFrame( |
| 23 | + { |
| 24 | + "A": pd.Series( |
| 25 | + [0, 1, 2, np.nan, np.nan, 3, 4], index=list("aaabcdd"), dtype=object |
| 26 | + ), |
| 27 | + "B": 1, |
| 28 | + } |
| 29 | + ) |
| 30 | + tm.assert_frame_equal(result, expected) |
| 31 | + |
| 32 | + |
| 33 | +def test_all_columns(): |
| 34 | + df = pd.DataFrame( |
| 35 | + {"A": pd.Series([[0, 1, 2], np.nan, [], (3, 4)], index=list("abcd")), "B": 1} |
| 36 | + ) |
| 37 | + result = df.explode(subset=["A", "B"]) |
| 38 | + expected = pd.DataFrame( |
| 39 | + { |
| 40 | + "A": pd.Series( |
| 41 | + [0, 1, 2, np.nan, np.nan, 3, 4], index=list("aaabcdd"), dtype=object |
| 42 | + ), |
| 43 | + "B": 1, |
| 44 | + } |
| 45 | + ) |
| 46 | + tm.assert_frame_equal(result, expected) |
| 47 | + |
| 48 | + |
| 49 | +def test_multiple_columns(): |
| 50 | + df = pd.DataFrame( |
| 51 | + { |
| 52 | + "A": pd.Series([[0, 1, 2], np.nan, [], (3, 4)], index=list("abcd")), |
| 53 | + "B": pd.Series([[0, 1, 2], np.nan, np.nan, 3], index=list("abcd")), |
| 54 | + } |
| 55 | + ) |
| 56 | + result = df.explode(subset=["A", "B"]) |
| 57 | + expected = pd.DataFrame( |
| 58 | + { |
| 59 | + "A": [0, 0, 0, 1, 1, 1, 2, 2, 2, np.nan, np.nan, 3, 4], |
| 60 | + "B": [0, 1, 2, 0, 1, 2, 0, 1, 2, np.nan, np.nan, 3, 3], |
| 61 | + }, |
| 62 | + dtype=object, |
| 63 | + index=list("aaaaaaaaabcdd"), |
| 64 | + ) |
| 65 | + tm.assert_frame_equal(result, expected) |
| 66 | + |
| 67 | + |
| 68 | +def test_usecase(): |
| 69 | + # explode a single column |
| 70 | + # gh-10511 |
| 71 | + df = pd.DataFrame( |
| 72 | + [[11, range(5), 10], [22, range(3), 20]], columns=["A", "B", "C"] |
| 73 | + ).set_index("C") |
| 74 | + result = df.explode(["B"]) |
| 75 | + |
| 76 | + expected = pd.DataFrame( |
| 77 | + { |
| 78 | + "A": [11, 11, 11, 11, 11, 22, 22, 22], |
| 79 | + "B": np.array([0, 1, 2, 3, 4, 0, 1, 2], dtype=object), |
| 80 | + "C": [10, 10, 10, 10, 10, 20, 20, 20], |
| 81 | + }, |
| 82 | + columns=list("ABC"), |
| 83 | + ).set_index("C") |
| 84 | + |
| 85 | + tm.assert_frame_equal(result, expected) |
| 86 | + |
| 87 | + # gh-8517 |
| 88 | + df = pd.DataFrame( |
| 89 | + [["2014-01-01", "Alice", "A B"], ["2014-01-02", "Bob", "C D"]], |
| 90 | + columns=["dt", "name", "text"], |
| 91 | + ) |
| 92 | + result = df.assign(text=df.text.str.split(" ")).explode(["text"]) |
| 93 | + expected = pd.DataFrame( |
| 94 | + [ |
| 95 | + ["2014-01-01", "Alice", "A"], |
| 96 | + ["2014-01-01", "Alice", "B"], |
| 97 | + ["2014-01-02", "Bob", "C"], |
| 98 | + ["2014-01-02", "Bob", "D"], |
| 99 | + ], |
| 100 | + columns=["dt", "name", "text"], |
| 101 | + index=[0, 0, 1, 1], |
| 102 | + ) |
| 103 | + tm.assert_frame_equal(result, expected) |
0 commit comments