Skip to content

Commit 2eb2811

Browse files
committed
worked on review comments
1 parent 527a587 commit 2eb2811

File tree

2 files changed

+22
-7
lines changed

2 files changed

+22
-7
lines changed

pandas/core/frame.py

+9-1
Original file line numberDiff line numberDiff line change
@@ -7927,6 +7927,10 @@ def explode(
79277927
For multiple columns, specify a non-empty list with each element
79287928
be str or tuple, and all specified columns their list-like data
79297929
on same row of the frame must have matching length.
7930+
7931+
.. versionadded:: 1.3.0
7932+
Multi-column explode
7933+
79307934
ignore_index : bool, default False
79317935
If True, the resulting index will be labeled 0, 1, …, n - 1.
79327936
@@ -7973,6 +7977,8 @@ def explode(
79737977
2 [] 1 []
79747978
3 [3, 4] 1 [d, e]
79757979
7980+
Single-column explode.
7981+
79767982
>>> df.explode('A')
79777983
A B C
79787984
0 0 1 [a, b, c]
@@ -7983,6 +7989,8 @@ def explode(
79837989
3 3 1 [d, e]
79847990
3 4 1 [d, e]
79857991
7992+
Multi-column explode.
7993+
79867994
>>> df.explode(list('AC'))
79877995
A B C
79887996
0 0 1 a
@@ -8005,7 +8013,7 @@ def explode(
80058013
elif isinstance(column, list) and all(
80068014
map(lambda c: is_scalar(c) or isinstance(c, tuple), column)
80078015
):
8008-
if len(column) == 0:
8016+
if not column:
80098017
raise ValueError("column must be nonempty")
80108018
if len(column) > len(set(column)):
80118019
raise ValueError("column must be unique")

pandas/tests/frame/methods/test_explode.py

+13-6
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,20 @@ def test_error():
2121
with pytest.raises(ValueError, match="columns must be unique"):
2222
df.explode("A")
2323

24+
25+
def test_error_multi_columns():
2426
# GH 39240
27+
df = pd.DataFrame(
28+
{"A": pd.Series([[0, 1, 2], np.nan, [], (3, 4)], index=list("abcd")), "B": 1}
29+
)
2530
df1 = df.assign(C=[["a", "b", "c"], "foo", [], ["d", "e", "f"]])
2631
df1.columns = list("ABC")
2732
with pytest.raises(ValueError, match="columns must have matching element counts"):
2833
df1.explode(list("AC"))
2934

30-
# GH 39240
3135
with pytest.raises(ValueError, match="column must be nonempty"):
3236
df1.explode([])
3337

34-
# GH 39240
3538
df2 = df.assign(C=[["a", "b", "c"], "foo", [], "d"])
3639
df2.columns = list("ABC")
3740
with pytest.raises(ValueError, match="columns must have matching element counts"):
@@ -207,19 +210,23 @@ def test_multi_columns():
207210
# GH 39240
208211
df = pd.DataFrame(
209212
{
210-
"A": pd.Series([[0, 1, 2], np.nan, [], (3, 4)], index=list("abcd")),
213+
"A": pd.Series(
214+
[[0, 1, 2], np.nan, [], (3, 4), np.nan], index=list("abcde")
215+
),
211216
"B": 1,
212-
"C": [["a", "b", "c"], "foo", [], ["d", "e"]],
217+
"C": [["a", "b", "c"], "foo", [], ["d", "e"], np.nan],
213218
}
214219
)
215220
result = df.explode(list("AC"))
216221
expected = pd.DataFrame(
217222
{
218223
"A": pd.Series(
219-
[0, 1, 2, np.nan, np.nan, 3, 4], index=list("aaabcdd"), dtype=object
224+
[0, 1, 2, np.nan, np.nan, 3, 4, np.nan],
225+
index=list("aaabcdde"),
226+
dtype=object,
220227
),
221228
"B": 1,
222-
"C": ["a", "b", "c", "foo", np.nan, "d", "e"],
229+
"C": ["a", "b", "c", "foo", np.nan, "d", "e", np.nan],
223230
}
224231
)
225232
tm.assert_frame_equal(result, expected)

0 commit comments

Comments
 (0)