Skip to content

Commit 73db25d

Browse files
authored
BUG: DataFrame.explode fails with str dtype (#61623)
1 parent 1da0d02 commit 73db25d

File tree

4 files changed

+20
-4
lines changed

4 files changed

+20
-4
lines changed

doc/source/whatsnew/v2.3.1.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ Fixed regressions
2626

2727
Bug fixes
2828
~~~~~~~~~
29-
-
29+
- Fixed bug in :meth:`DataFrame.explode` and :meth:`Series.explode` where methods would fail with ``dtype="str"`` (:issue:`61623`)
3030

3131
.. ---------------------------------------------------------------------------
3232
.. _whatsnew_231.other:

pandas/core/arrays/arrow/array.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1935,9 +1935,9 @@ def _explode(self):
19351935
"""
19361936
# child class explode method supports only list types; return
19371937
# default implementation for non list types.
1938-
if not (
1939-
pa.types.is_list(self.dtype.pyarrow_dtype)
1940-
or pa.types.is_large_list(self.dtype.pyarrow_dtype)
1938+
if not hasattr(self.dtype, "pyarrow_dtype") or (
1939+
not pa.types.is_list(self.dtype.pyarrow_dtype)
1940+
and not pa.types.is_large_list(self.dtype.pyarrow_dtype)
19411941
):
19421942
return super()._explode()
19431943
values = self

pandas/tests/frame/methods/test_explode.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,3 +297,11 @@ def test_multi_columns_nan_empty():
297297
index=[0, 0, 1, 2, 3, 3],
298298
)
299299
tm.assert_frame_equal(result, expected)
300+
301+
302+
def test_str_dtype():
303+
# https://github.com/pandas-dev/pandas/pull/61623
304+
df = pd.DataFrame({"a": ["x", "y"]}, dtype="str")
305+
result = df.explode(column="a")
306+
assert result is not df
307+
tm.assert_frame_equal(result, df)

pandas/tests/series/methods/test_explode.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,3 +175,11 @@ def test_explode_pyarrow_non_list_type(ignore_index):
175175
result = ser.explode(ignore_index=ignore_index)
176176
expected = pd.Series([1, 2, 3], dtype="int64[pyarrow]", index=[0, 1, 2])
177177
tm.assert_series_equal(result, expected)
178+
179+
180+
def test_str_dtype():
181+
# https://github.com/pandas-dev/pandas/pull/61623
182+
ser = pd.Series(["x", "y"], dtype="str")
183+
result = ser.explode()
184+
assert result is not ser
185+
tm.assert_series_equal(result, ser)

0 commit comments

Comments
 (0)