| 124 | |
| 125 | |
| 126 | def test_usecase(): |
| 127 | # explode a single column |
| 128 | # gh-10511 |
| 129 | df = pd.DataFrame( |
| 130 | [[11, range(5), 10], [22, range(3), 20]], columns=list("ABC") |
| 131 | ).set_index("C") |
| 132 | result = df.explode("B") |
| 133 | |
| 134 | expected = pd.DataFrame( |
| 135 | { |
| 136 | "A": [11, 11, 11, 11, 11, 22, 22, 22], |
| 137 | "B": np.array([0, 1, 2, 3, 4, 0, 1, 2], dtype=object), |
| 138 | "C": [10, 10, 10, 10, 10, 20, 20, 20], |
| 139 | }, |
| 140 | columns=list("ABC"), |
| 141 | ).set_index("C") |
| 142 | |
| 143 | tm.assert_frame_equal(result, expected) |
| 144 | |
| 145 | # gh-8517 |
| 146 | df = pd.DataFrame( |
| 147 | [["2014-01-01", "Alice", "A B"], ["2014-01-02", "Bob", "C D"]], |
| 148 | columns=["dt", "name", "text"], |
| 149 | ) |
| 150 | result = df.assign(text=df.text.str.split(" ")).explode("text") |
| 151 | expected = pd.DataFrame( |
| 152 | [ |
| 153 | ["2014-01-01", "Alice", "A"], |
| 154 | ["2014-01-01", "Alice", "B"], |
| 155 | ["2014-01-02", "Bob", "C"], |
| 156 | ["2014-01-02", "Bob", "D"], |
| 157 | ], |
| 158 | columns=["dt", "name", "text"], |
| 159 | index=[0, 0, 1, 1], |
| 160 | ) |
| 161 | tm.assert_frame_equal(result, expected) |
| 162 | |
| 163 | |
| 164 | @pytest.mark.parametrize( |