Skip to content

Commit fad258a

Browse files
committed
DOC: Panel.apply whatsnew docs
DOC: add Panel.appy to basics.rst
1 parent 1d7c9e5 commit fad258a

File tree

3 files changed

+135
-5
lines changed

3 files changed

+135
-5
lines changed

doc/source/api.rst

+4-3
Original file line numberDiff line numberDiff line change
@@ -785,6 +785,7 @@ Attributes and underlying data
785785
Panel.axes
786786
Panel.ndim
787787
Panel.shape
788+
Panel.dtypes
788789

789790
Conversion
790791
~~~~~~~~~~
@@ -1122,7 +1123,7 @@ Indexing, iteration
11221123
~~~~~~~~~~~~~~~~~~~
11231124
.. autosummary::
11241125
:toctree: generated/
1125-
1126+
11261127
GroupBy.__iter__
11271128
GroupBy.groups
11281129
GroupBy.indices
@@ -1141,7 +1142,7 @@ Computations / Descriptive Stats
11411142
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
11421143
.. autosummary::
11431144
:toctree: generated/
1144-
1145+
11451146
GroupBy.mean
11461147
GroupBy.median
11471148
GroupBy.std
@@ -1155,7 +1156,7 @@ Computations / Descriptive Stats
11551156
11561157
.. toctree::
11571158
:hidden:
1158-
1159+
11591160
generated/pandas.core.common.isnull
11601161
generated/pandas.core.common.notnull
11611162
generated/pandas.core.reshape.get_dummies

doc/source/basics.rst

+77-2
Original file line numberDiff line numberDiff line change
@@ -637,6 +637,81 @@ to :ref:`merging/joining functionality <merging>`:
637637
s
638638
s.map(t)
639639
640+
641+
.. _basics.apply_panel:
642+
643+
Applying with a Panel
644+
~~~~~~~~~~~~~~~~~~~~~
645+
646+
Applying with a ``Panel`` will pass a ``Series`` to the applied function. If the applied
647+
function returns a ``Series``, the result of the application will be a ``Panel``. If the applied function
648+
reduces to a scalar, the result of the application will be a ``DataFrame``.
649+
650+
.. note::
651+
652+
Prior to 0.13.1 ``apply`` on a ``Panel`` would only work on ``ufuncs`` (e.g. ``np.sum/np.max``).
653+
654+
.. ipython:: python
655+
656+
import pandas.util.testing as tm
657+
panel = tm.makePanel(5)
658+
panel
659+
panel['ItemA']
660+
661+
A transformational apply.
662+
663+
.. ipython:: python
664+
665+
result = panel.apply(lambda x: x*2, axis='items')
666+
result
667+
result['ItemA']
668+
669+
A reduction operation.
670+
671+
.. ipython:: python
672+
673+
panel.apply(lambda x: x.dtype, axis='items')
674+
675+
A similar reduction type operation
676+
677+
.. ipython:: python
678+
679+
panel.apply(lambda x: x.sum(), axis='major_axis')
680+
681+
This last reduction is equivalent to
682+
683+
.. ipython:: python
684+
685+
panel.sum('major_axis')
686+
687+
A transformation operation that returns a ``Panel``, but is computing
688+
the z-score across the ``major_axis``.
689+
690+
.. ipython:: python
691+
692+
result = panel.apply(lambda x: (x-x.mean())/x.std(), axis='major_axis')
693+
result
694+
result['ItemA']
695+
696+
Apply can also accept multiple axes in the ``axis`` argument. This will pass a
697+
``DataFrame`` of the cross-section to the applied function.
698+
699+
.. ipython:: python
700+
701+
f = lambda x: (x-x.mean(1)/x.std(1))
702+
703+
result = panel.apply(f, axis = ['items','major_axis'])
704+
result
705+
result.loc[:,:,'ItemA']
706+
707+
This is equivalent to the following
708+
709+
.. ipython:: python
710+
711+
result = Panel(dict([ (ax,f(panel.loc[:,:,ax])) for ax in panel.minor_axis ]))
712+
result
713+
result.loc[:,:,'ItemA']
714+
640715
.. _basics.reindexing:
641716

642717
Reindexing and altering labels
@@ -1066,7 +1141,7 @@ or match a pattern:
10661141
10671142
Series(['1', '2', '3a', '3b', '03c']).str.match(pattern, as_indexer=True)
10681143
1069-
The distinction between ``match`` and ``contains`` is strictness: ``match``
1144+
The distinction between ``match`` and ``contains`` is strictness: ``match``
10701145
relies on strict ``re.match``, while ``contains`` relies on ``re.search``.
10711146

10721147
.. warning::
@@ -1078,7 +1153,7 @@ relies on strict ``re.match``, while ``contains`` relies on ``re.search``.
10781153
This old, deprecated behavior of ``match`` is still the default. As
10791154
demonstrated above, use the new behavior by setting ``as_indexer=True``.
10801155
In this mode, ``match`` is analagous to ``contains``, returning a boolean
1081-
Series. The new behavior will become the default behavior in a future
1156+
Series. The new behavior will become the default behavior in a future
10821157
release.
10831158

10841159
Methods like ``match``, ``contains``, ``startswith``, and ``endswith`` take

doc/source/v0.13.1.txt

+54
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,60 @@ Deprecations
2929
Enhancements
3030
~~~~~~~~~~~~
3131

32+
- ``Panel.apply`` will work on non-ufuncs. See :ref:`the docs<basics.apply_panel>`.
33+
34+
.. ipython:: python
35+
36+
import pandas.util.testing as tm
37+
panel = tm.makePanel(5)
38+
panel
39+
panel['ItemA']
40+
41+
Specifying an ``apply`` that operates on a Series (to return a single element)
42+
43+
.. ipython:: python
44+
45+
panel.apply(lambda x: x.dtype, axis='items')
46+
47+
A similar reduction type operation
48+
49+
.. ipython:: python
50+
51+
panel.apply(lambda x: x.sum(), axis='major_axis')
52+
53+
This is equivalent to
54+
55+
.. ipython:: python
56+
57+
panel.sum('major_axis')
58+
59+
A transformation operation that returns a Panel, but is computing
60+
the z-score across the major_axis
61+
62+
.. ipython:: python
63+
64+
result = panel.apply(lambda x: (x-x.mean())/x.std(), axis='major_axis')
65+
result
66+
result['ItemA']
67+
68+
- ``Panel.apply`` operating on cross-sectional slabs. (:issue:`1148`)
69+
70+
.. ipython:: python
71+
72+
f = lambda x: (x-x.mean(1)/x.std(1))
73+
74+
result = panel.apply(f, axis = ['items','major_axis'])
75+
result
76+
result.loc[:,:,'ItemA']
77+
78+
This is equivalent to the following
79+
80+
.. ipython:: python
81+
82+
result = Panel(dict([ (ax,f(panel.loc[:,:,ax])) for ax in panel.minor_axis ]))
83+
result
84+
result.loc[:,:,'ItemA']
85+
3286
Experimental
3387
~~~~~~~~~~~~
3488

0 commit comments

Comments
 (0)