Skip to content

Commit a864b1d

Browse files
committed
moar docs
1 parent 8c3690e commit a864b1d

File tree

3 files changed

+22
-7
lines changed

3 files changed

+22
-7
lines changed

doc/source/user_guide/reshaping.rst

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -809,7 +809,7 @@ Exploding a List-like Column
809809

810810
.. versionadded:: 0.25.0
811811

812-
Sometimes the value column is list-like:
812+
Sometimes the value column is list-like.
813813

814814
.. ipython:: python
815815
@@ -818,15 +818,23 @@ Sometimes the value column is list-like:
818818
df = pd.DataFrame({'keys': keys, 'values': values})
819819
df
820820
821-
But we actually want to put each value onto its own row.
822-
For this purpose we can use ``~Series.explode``. This will replicate the index values:
821+
We can 'explode' this transforming each element of a list-like to a row, by using ``~Series.explode``. This will replicate the index values:
823822

824823
.. ipython:: python
825824
826825
df['values'].explode()
827826
828-
You can merge this back into the original to expand the dataframe.
827+
You can join this with the original to get an expanded ``DataFrame``.
829828

830829
.. ipython:: python
831830
832-
pd.merge(df, df['values'].explode(), left_index=True, right_index=True)
831+
df[['keys']]join(df['values'].explode())
832+
833+
834+
This routine will preserve replace empty lists with ``np.nan`` and preserve scalar entries. The dtype of the resulting ``Series`` is always ``object``.
835+
836+
.. ipython:: python
837+
838+
s = pd.Series([[1, 2, 3], np.nan, [], ['a', 'b']])
839+
s
840+
s.explode()

doc/source/whatsnew/v0.25.0.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ The repr now looks like this:
187187
Series.explode to split list-like values to rows
188188
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
189189

190-
:class:`Series` has gained the :meth:`Series.explode` method to tranform list-likes to onto individual rows. See :ref:`section on Exploding list-like column <reshaping.html>` in docs for more information (:issue:`16538`)
190+
:class:`Series` has gained the :meth:`Series.explode` method to transform list-likes to individual rows. See :ref:`section on Exploding list-like column <reshaping.html>` in docs for more information (:issue:`16538`)
191191

192192

193193
.. ipython:: python

pandas/core/series.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3637,7 +3637,7 @@ def reorder_levels(self, order):
36373637
result.index = result.index.reorder_levels(order)
36383638
return result
36393639

3640-
def explode(self):
3640+
def explode(self) -> 'Series':
36413641
"""
36423642
Create new Series expanding a list-like column.
36433643
@@ -3674,6 +3674,13 @@ def explode(self):
36743674
3 3
36753675
3 4
36763676
dtype: object
3677+
3678+
Notes
3679+
-----
3680+
This routine will explode list-likes including lists, tuples, Series, and np.ndarray.
3681+
The result dtype of the returned Series will always be object.
3682+
Scalars will be returned unchanged.
3683+
Empty list-likes will result in a np.nan for that row.
36773684
"""
36783685
if not len(self):
36793686
return self.copy()

0 commit comments

Comments
 (0)