<p dir="auto">When using DataFrame.apply(), it would be helpful to be able to apply different functions to different columns. There's a nice workaround on <a href="http://stackoverflow.com/questions/26434123/pandas-apply-different-functions-to-different-columns" rel="nofollow">StackOverflow</a>:</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="import numpy as np
import pandas as pd

def multi_func(functions):
    def f(col):
        return functions[col.name](col)
    return f

df = pd.DataFrame(np.random.random((10, 2)), columns=['A', 'B'])
results = df.apply(multi_func({'A': np.mean, 'B': np.sum}))

results
A    0.401456
B    6.845529
dtype: float64"><pre class="notranslate"><span class="pl-k">import</span> <span class="pl-s1">numpy</span> <span class="pl-k">as</span> <span class="pl-s1">np</span>
<span class="pl-k">import</span> <span class="pl-s1">pandas</span> <span class="pl-k">as</span> <span class="pl-s1">pd</span>

<span class="pl-k">def</span> <span class="pl-en">multi_func</span>(<span class="pl-s1">functions</span>):
    <span class="pl-k">def</span> <span class="pl-en">f</span>(<span class="pl-s1">col</span>):
        <span class="pl-k">return</span> <span class="pl-s1">functions</span>[<span class="pl-s1">col</span>.<span class="pl-s1">name</span>](<span class="pl-s1">col</span>)
    <span class="pl-k">return</span> <span class="pl-s1">f</span>

<span class="pl-s1">df</span> <span class="pl-c1">=</span> <span class="pl-s1">pd</span>.<span class="pl-v">DataFrame</span>(<span class="pl-s1">np</span>.<span class="pl-s1">random</span>.<span class="pl-en">random</span>((<span class="pl-c1">10</span>, <span class="pl-c1">2</span>)), <span class="pl-s1">columns</span><span class="pl-c1">=</span>[<span class="pl-s">'A'</span>, <span class="pl-s">'B'</span>])
<span class="pl-s1">results</span> <span class="pl-c1">=</span> <span class="pl-s1">df</span>.<span class="pl-en">apply</span>(<span class="pl-en">multi_func</span>({<span class="pl-s">'A'</span>: <span class="pl-s1">np</span>.<span class="pl-s1">mean</span>, <span class="pl-s">'B'</span>: <span class="pl-s1">np</span>.<span class="pl-s1">sum</span>}))

<span class="pl-s1">results</span>
<span class="pl-v">A</span>    <span class="pl-c1">0.401456</span>
<span class="pl-v">B</span>    <span class="pl-c1">6.845529</span>
<span class="pl-s1">dtype</span>: <span class="pl-s1">float64</span></pre></div>
<p dir="auto">My guess is that the changes would be made in <a href="https://github.com/pandas-dev/pandas/blob/master/pandas/core/frame.py">pandas.core.frame.DataFrame</a>, potentially adding a new <code class="notranslate">_apply_X</code> method. Any thoughts/advice?</p>
<p dir="auto">I'm trying to use pandas to speed up timezone conversions on many datetimes and I can't get around <code class="notranslate">NonExistentTimeErrors</code>. Pandas <code class="notranslate">tz_localize()</code> seems to ignore the <code class="notranslate">ambiguous</code> argument for the non existent time case.</p>
<p dir="auto">Example:</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="import pytz
import pandas

tz = pytz.timezone('Europe/Warsaw')
non_existent = datetime.datetime(2015, 3, 29, 2, 30)

tz.normalize(tz.localize(non_existent))
#2015-03-29 03:30:00+02:00

tz.normalize(tz.localize(non_existent, is_dst=False))
#2015-03-29 03:30:00+02:00

pandas.Timestamp(non_existent).tz_localize(tz)
# NonExistentTimeError: 2015-03-29 02:30:00

pandas.Timestamp(non_existent).tz_localize(tz, ambiguous=0)
# NonExistentTimeError: 2015-03-29 02:30:00"><pre class="notranslate"><span class="pl-k">import</span> <span class="pl-s1">pytz</span>
<span class="pl-k">import</span> <span class="pl-s1">pandas</span>

<span class="pl-s1">tz</span> <span class="pl-c1">=</span> <span class="pl-s1">pytz</span>.<span class="pl-en">timezone</span>(<span class="pl-s">'Europe/Warsaw'</span>)
<span class="pl-s1">non_existent</span> <span class="pl-c1">=</span> <span class="pl-s1">datetime</span>.<span class="pl-en">datetime</span>(<span class="pl-c1">2015</span>, <span class="pl-c1">3</span>, <span class="pl-c1">29</span>, <span class="pl-c1">2</span>, <span class="pl-c1">30</span>)

<span class="pl-s1">tz</span>.<span class="pl-en">normalize</span>(<span class="pl-s1">tz</span>.<span class="pl-en">localize</span>(<span class="pl-s1">non_existent</span>))
<span class="pl-c">#2015-03-29 03:30:00+02:00</span>

<span class="pl-s1">tz</span>.<span class="pl-en">normalize</span>(<span class="pl-s1">tz</span>.<span class="pl-en">localize</span>(<span class="pl-s1">non_existent</span>, <span class="pl-s1">is_dst</span><span class="pl-c1">=</span><span class="pl-c1">False</span>))
<span class="pl-c">#2015-03-29 03:30:00+02:00</span>

<span class="pl-s1">pandas</span>.<span class="pl-v">Timestamp</span>(<span class="pl-s1">non_existent</span>).<span class="pl-en">tz_localize</span>(<span class="pl-s1">tz</span>)
<span class="pl-c"># NonExistentTimeError: 2015-03-29 02:30:00</span>

<span class="pl-s1">pandas</span>.<span class="pl-v">Timestamp</span>(<span class="pl-s1">non_existent</span>).<span class="pl-en">tz_localize</span>(<span class="pl-s1">tz</span>, <span class="pl-s1">ambiguous</span><span class="pl-c1">=</span><span class="pl-c1">0</span>)
<span class="pl-c"># NonExistentTimeError: 2015-03-29 02:30:00</span></pre></div>
<p dir="auto">It would be nice if the <code class="notranslate">ambiguous</code> argument worked the same as <code class="notranslate">is_dst</code> in pytz. As it is now, it's impossible afaik to reliably localize a series of datetimes using pandas, since any of them might cause a <code class="notranslate">NonExistentTimeError</code> and there's no way of guiding pandas what to do with such datetimes.</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="INSTALLED VERSIONS
------------------
commit: None
python: 2.7.5.final.0
python-bits: 64
OS: Linux
OS-release: 3.13.0-39-generic
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8

pandas: 0.15.1
nose: 1.3.4
Cython: None
numpy: 1.9.1
scipy: None
statsmodels: None
IPython: 2.3.1
sphinx: 1.2.2
patsy: None
dateutil: 2.2
pytz: 2013.9
bottleneck: None
tables: None
numexpr: None
matplotlib: None
openpyxl: None
xlrd: None
xlwt: None
xlsxwriter: None
lxml: None
bs4: None
html5lib: None
httplib2: 0.9
apiclient: 1.3.1
rpy2: None
sqlalchemy: None
pymysql: None
psycopg2: 2.5.4 (dt dec pq3 ext)"><pre class="notranslate"><code class="notranslate">INSTALLED VERSIONS
------------------
commit: None
python: 2.7.5.final.0
python-bits: 64
OS: Linux
OS-release: 3.13.0-39-generic
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8

pandas: 0.15.1
nose: 1.3.4
Cython: None
numpy: 1.9.1
scipy: None
statsmodels: None
IPython: 2.3.1
sphinx: 1.2.2
patsy: None
dateutil: 2.2
pytz: 2013.9
bottleneck: None
tables: None
numexpr: None
matplotlib: None
openpyxl: None
xlrd: None
xlwt: None
xlsxwriter: None
lxml: None
bs4: None
html5lib: None
httplib2: 0.9
apiclient: 1.3.1
rpy2: None
sqlalchemy: None
pymysql: None
psycopg2: 2.5.4 (dt dec pq3 ext)
</code></pre></div>