Skip to content

Commit ebc2e76

Browse files
Backport PR #43802: REG: Regression in explode when column is non string (#43817)
Co-authored-by: Patrick Hoefler <61934744+phofl@users.noreply.github.com>
1 parent 1ae9b9f commit ebc2e76

File tree

3 files changed

+12
-8
lines changed

3 files changed

+12
-8
lines changed

doc/source/whatsnew/v1.3.4.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ Fixed regressions
2222
- Fixed regression in :meth:`Series.cat.reorder_categories` failing to update the categories on the ``Series`` (:issue:`43232`)
2323
- Fixed regression in :meth:`Series.cat.categories` setter failing to update the categories on the ``Series`` (:issue:`43334`)
2424
- Fixed regression in :meth:`pandas.read_csv` raising ``UnicodeDecodeError`` exception when ``memory_map=True`` (:issue:`43540`)
25+
- Fixed regression in :meth:`DataFrame.explode` raising ``AssertionError`` when ``column`` is any scalar which is not a string (:issue:`43314`)
2526
- Fixed regression in :meth:`Series.aggregate` attempting to pass ``args`` and ``kwargs`` multiple times to the user supplied ``func`` in certain cases (:issue:`43357`)
2627

2728
.. ---------------------------------------------------------------------------

pandas/core/frame.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8137,7 +8137,7 @@ def stack(self, level: Level = -1, dropna: bool = True):
81378137

81388138
def explode(
81398139
self,
8140-
column: str | tuple | list[str | tuple],
8140+
column: Scalar | tuple | list[Scalar | tuple],
81418141
ignore_index: bool = False,
81428142
) -> DataFrame:
81438143
"""
@@ -8147,7 +8147,7 @@ def explode(
81478147
81488148
Parameters
81498149
----------
8150-
column : str or tuple or list thereof
8150+
column : Scalar or tuple or list thereof
81518151
Column(s) to explode.
81528152
For multiple columns, specify a non-empty list with each element
81538153
be str or tuple, and all specified columns their list-like data
@@ -8229,9 +8229,8 @@ def explode(
82298229
if not self.columns.is_unique:
82308230
raise ValueError("columns must be unique")
82318231

8232-
columns: list[str | tuple]
8232+
columns: list[Scalar | tuple]
82338233
if is_scalar(column) or isinstance(column, tuple):
8234-
assert isinstance(column, (str, tuple))
82358234
columns = [column]
82368235
elif isinstance(column, list) and all(
82378236
map(lambda c: is_scalar(c) or isinstance(c, tuple), column)

pandas/tests/frame/methods/test_explode.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,18 @@ def test_error_multi_columns(input_subset, error_message):
5353
df.explode(input_subset)
5454

5555

56-
def test_basic():
56+
@pytest.mark.parametrize(
57+
"scalar",
58+
["a", 0, 1.5, pd.Timedelta("1 days"), pd.Timestamp("2019-12-31")],
59+
)
60+
def test_basic(scalar):
5761
df = pd.DataFrame(
58-
{"A": pd.Series([[0, 1, 2], np.nan, [], (3, 4)], index=list("abcd")), "B": 1}
62+
{scalar: pd.Series([[0, 1, 2], np.nan, [], (3, 4)], index=list("abcd")), "B": 1}
5963
)
60-
result = df.explode("A")
64+
result = df.explode(scalar)
6165
expected = pd.DataFrame(
6266
{
63-
"A": pd.Series(
67+
scalar: pd.Series(
6468
[0, 1, 2, np.nan, np.nan, 3, 4], index=list("aaabcdd"), dtype=object
6569
),
6670
"B": 1,

0 commit comments

Comments
 (0)