Skip to content

Commit 3b60759

Browse files
authored
BUG: Respect ignore_index in Series.explode (pandas-dev#40507)
1 parent f4b6cbe commit 3b60759

File tree

3 files changed

+11
-1
lines changed

3 files changed

+11
-1
lines changed

doc/source/whatsnew/v1.3.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -651,6 +651,7 @@ Reshaping
651651
- Allow :class:`Index` to be passed to the :func:`numpy.all` function (:issue:`40180`)
652652
- Bug in :meth:`DataFrame.stack` not preserving ``CategoricalDtype`` in a ``MultiIndex`` (:issue:`36991`)
653653
- Bug in :func:`to_datetime` raising error when input sequence contains unhashable items (:issue:`39756`)
654+
- Bug in :meth:`Series.explode` preserving index when ``ignore_index`` was ``True`` and values were scalars (:issue:`40487`)
654655

655656
Sparse
656657
^^^^^^

pandas/core/series.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -3857,7 +3857,8 @@ def explode(self, ignore_index: bool = False) -> Series:
38573857
dtype: object
38583858
"""
38593859
if not len(self) or not is_object_dtype(self):
3860-
return self.copy()
3860+
result = self.copy()
3861+
return result.reset_index(drop=True) if ignore_index else result
38613862

38623863
values, counts = reshape.explode(np.asarray(self._values))
38633864

pandas/tests/series/methods/test_explode.py

+8
Original file line numberDiff line numberDiff line change
@@ -134,3 +134,11 @@ def test_explode_sets():
134134
result = s.explode().sort_values()
135135
expected = pd.Series(["a", "b", "c"], index=[1, 1, 1])
136136
tm.assert_series_equal(result, expected)
137+
138+
139+
def test_explode_scalars_can_ignore_index():
140+
# https://github.com/pandas-dev/pandas/issues/40487
141+
s = pd.Series([1, 2, 3], index=["a", "b", "c"])
142+
result = s.explode(ignore_index=True)
143+
expected = pd.Series([1, 2, 3])
144+
tm.assert_series_equal(result, expected)

0 commit comments

Comments
 (0)