Skip to content

Backport PR #43802 on branch 1.3.x (REG: Regression in explode when column is non string) #43817

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.3.4.rst
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Fixed regressions
- Fixed regression in :meth:`Series.cat.reorder_categories` failing to update the categories on the ``Series`` (:issue:`43232`)
- Fixed regression in :meth:`Series.cat.categories` setter failing to update the categories on the ``Series`` (:issue:`43334`)
- Fixed regression in :meth:`pandas.read_csv` raising ``UnicodeDecodeError`` exception when ``memory_map=True`` (:issue:`43540`)
- Fixed regression in :meth:`DataFrame.explode` raising ``AssertionError`` when ``column`` is any scalar which is not a string (:issue:`43314`)
- Fixed regression in :meth:`Series.aggregate` attempting to pass ``args`` and ``kwargs`` multiple times to the user supplied ``func`` in certain cases (:issue:`43357`)

.. ---------------------------------------------------------------------------
Expand Down
7 changes: 3 additions & 4 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -8137,7 +8137,7 @@ def stack(self, level: Level = -1, dropna: bool = True):

def explode(
self,
column: str | tuple | list[str | tuple],
column: Scalar | tuple | list[Scalar | tuple],
ignore_index: bool = False,
) -> DataFrame:
"""
Expand All @@ -8147,7 +8147,7 @@ def explode(

Parameters
----------
column : str or tuple or list thereof
column : Scalar or tuple or list thereof
Column(s) to explode.
For multiple columns, specify a non-empty list with each element
be str or tuple, and all specified columns their list-like data
Expand Down Expand Up @@ -8229,9 +8229,8 @@ def explode(
if not self.columns.is_unique:
raise ValueError("columns must be unique")

columns: list[str | tuple]
columns: list[Scalar | tuple]
if is_scalar(column) or isinstance(column, tuple):
assert isinstance(column, (str, tuple))
columns = [column]
elif isinstance(column, list) and all(
map(lambda c: is_scalar(c) or isinstance(c, tuple), column)
Expand Down
12 changes: 8 additions & 4 deletions pandas/tests/frame/methods/test_explode.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,18 @@ def test_error_multi_columns(input_subset, error_message):
df.explode(input_subset)


def test_basic():
@pytest.mark.parametrize(
"scalar",
["a", 0, 1.5, pd.Timedelta("1 days"), pd.Timestamp("2019-12-31")],
)
def test_basic(scalar):
df = pd.DataFrame(
{"A": pd.Series([[0, 1, 2], np.nan, [], (3, 4)], index=list("abcd")), "B": 1}
{scalar: pd.Series([[0, 1, 2], np.nan, [], (3, 4)], index=list("abcd")), "B": 1}
)
result = df.explode("A")
result = df.explode(scalar)
expected = pd.DataFrame(
{
"A": pd.Series(
scalar: pd.Series(
[0, 1, 2, np.nan, np.nan, 3, 4], index=list("aaabcdd"), dtype=object
),
"B": 1,
Expand Down