Skip to content

Commit

Permalink
Backport PR pandas-dev#43802: REG: Regression in explode when column …
Browse files Browse the repository at this point in the history
…is non string
  • Loading branch information
phofl authored and meeseeksmachine committed Sep 30, 2021
1 parent 23ed54f commit 90dbf6c
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 8 deletions.
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

0 comments on commit 90dbf6c

Please sign in to comment.