Skip to content

Commit fa55030

Browse files
[backport 2.3.x] BUG: DataFrame.explode fails with str dtype (#61623) (#61699)
BUG: DataFrame.explode fails with str dtype (#61623) Co-authored-by: Richard Shadrach <[email protected]>
1 parent 90ec3a1 commit fa55030

File tree

4 files changed

+20
-2
lines changed

4 files changed

+20
-2
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 & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1923,7 +1923,9 @@ def _explode(self):
19231923
"""
19241924
# child class explode method supports only list types; return
19251925
# default implementation for non list types.
1926-
if not pa.types.is_list(self.dtype.pyarrow_dtype):
1926+
if not hasattr(self.dtype, "pyarrow_dtype") or (
1927+
not pa.types.is_list(self.dtype.pyarrow_dtype)
1928+
):
19271929
return super()._explode()
19281930
values = self
19291931
counts = pa.compute.list_value_length(values._pa_array)

pandas/tests/frame/methods/test_explode.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,3 +301,11 @@ def test_multi_columns_nan_empty():
301301
index=[0, 0, 1, 2, 3, 3],
302302
)
303303
tm.assert_frame_equal(result, expected)
304+
305+
306+
def test_str_dtype():
307+
# https://github.com/pandas-dev/pandas/pull/61623
308+
df = pd.DataFrame({"a": ["x", "y"]}, dtype="str")
309+
result = df.explode(column="a")
310+
assert result is not df
311+
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
@@ -173,3 +173,11 @@ def test_explode_pyarrow_non_list_type(ignore_index):
173173
result = ser.explode(ignore_index=ignore_index)
174174
expected = pd.Series([1, 2, 3], dtype="int64[pyarrow]", index=[0, 1, 2])
175175
tm.assert_series_equal(result, expected)
176+
177+
178+
def test_str_dtype():
179+
# https://github.com/pandas-dev/pandas/pull/61623
180+
ser = pd.Series(["x", "y"], dtype="str")
181+
result = ser.explode()
182+
assert result is not ser
183+
tm.assert_series_equal(result, ser)

0 commit comments

Comments
 (0)