Skip to content

Commit 8175010

Browse files
committed
BUG: DataFrame.explode fails with str dtype
1 parent b8371f5 commit 8175010

File tree

4 files changed

+61
-3
lines changed

4 files changed

+61
-3
lines changed

doc/source/whatsnew/v2.3.1.rst

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
.. _whatsnew_231:
2+
3+
What's new in 2.3.1 (Month XX, 2025)
4+
---------------------------------------
5+
6+
These are the changes in pandas 2.3.1. See :ref:`release` for a full changelog
7+
including other versions of pandas.
8+
9+
{{ header }}
10+
11+
.. ---------------------------------------------------------------------------
12+
.. _whatsnew_231.enhancements:
13+
14+
Enhancements
15+
~~~~~~~~~~~~
16+
-
17+
18+
.. _whatsnew_231.regressions:
19+
20+
Fixed regressions
21+
~~~~~~~~~~~~~~~~~
22+
-
23+
24+
.. ---------------------------------------------------------------------------
25+
.. _whatsnew_231.bug_fixes:
26+
27+
Bug fixes
28+
~~~~~~~~~
29+
- Fixed bug in :meth:`DataFrame.explode` and :meth:`Series.explode` where methods would fail with ``dtype="str"`` (:issue:`???`)
30+
31+
.. ---------------------------------------------------------------------------
32+
.. _whatsnew_231.other:
33+
34+
Other
35+
~~~~~
36+
-
37+
38+
.. ---------------------------------------------------------------------------
39+
.. _whatsnew_231.contributors:
40+
41+
Contributors
42+
~~~~~~~~~~~~
43+
44+
.. contributors:: v2.3.0..v2.3.1

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: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,3 +297,10 @@ 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+
df = pd.DataFrame({"a": ["x", "y"]}, dtype="str")
304+
result = df.explode(column="a")
305+
assert result is not df
306+
tm.assert_frame_equal(result, df)

pandas/tests/series/methods/test_explode.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,3 +175,10 @@ 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+
ser = pd.Series(["x", "y"], dtype="str")
182+
result = ser.explode()
183+
assert result is not ser
184+
tm.assert_series_equal(result, ser)

0 commit comments

Comments
 (0)