From 817254916d29f3abe7bbfd5e880a8574041622f7 Mon Sep 17 00:00:00 2001 From: phofl Date: Wed, 29 Sep 2021 22:42:32 +0200 Subject: [PATCH 1/3] REG: Regression in explode when column is non string --- doc/source/whatsnew/v1.3.4.rst | 1 + pandas/core/frame.py | 1 - pandas/tests/frame/methods/test_explode.py | 12 ++++++++---- 3 files changed, 9 insertions(+), 5 deletions(-) diff --git a/doc/source/whatsnew/v1.3.4.rst b/doc/source/whatsnew/v1.3.4.rst index a4a5bed14d416..e00397305b51d 100644 --- a/doc/source/whatsnew/v1.3.4.rst +++ b/doc/source/whatsnew/v1.3.4.rst @@ -21,6 +21,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`) .. --------------------------------------------------------------------------- diff --git a/pandas/core/frame.py b/pandas/core/frame.py index bfdfeabbd389c..72ed4081f2691 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -8295,7 +8295,6 @@ def explode( columns: list[str | 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) diff --git a/pandas/tests/frame/methods/test_explode.py b/pandas/tests/frame/methods/test_explode.py index 6fdf5d806ac6b..8716a181120f6 100644 --- a/pandas/tests/frame/methods/test_explode.py +++ b/pandas/tests/frame/methods/test_explode.py @@ -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, From 1788e1ce65e1d04a0e04e30ec8124fb572d6733a Mon Sep 17 00:00:00 2001 From: phofl Date: Thu, 30 Sep 2021 09:37:24 +0200 Subject: [PATCH 2/3] Change type hint --- pandas/core/frame.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 72ed4081f2691..a4091fc71d628 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -8293,7 +8293,7 @@ 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): columns = [column] elif isinstance(column, list) and all( From dcb0232cfd02be73adc91ce98ff520609ced9a69 Mon Sep 17 00:00:00 2001 From: phofl Date: Thu, 30 Sep 2021 11:54:36 +0200 Subject: [PATCH 3/3] Fix typing --- pandas/core/frame.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index a4091fc71d628..613a4cc829514 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -8201,7 +8201,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: """ @@ -8211,7 +8211,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