<p dir="auto">I had a series containing strings like these:</p>
<p dir="auto">"November 1, 2013"</p>
<p dir="auto">Series length was about 500,000</p>
<p dir="auto">A)<br>
running pd.to_datetime(s) takes just over a minute.<br>
B)<br>
running pd.to_datetime(s, format="%B %d, %Y") takes about 7 seconds!</p>
<p dir="auto">My suggestion is a way to make case A (where user doesn't specify the format type) take about as long as case B (user does specify).</p>
<p dir="auto">Basically it looks like the code is always using date_util parser for case A.</p>
<p dir="auto">My suggestion is based upon the idea that it's highly likely that the date strings are all in a consistent format (it's highly unlikely in this case that they would be in 500K separate formats!).</p>
<p dir="auto">In a nutshell:</p>
<ul dir="auto">
<li>figure out the date format of the first entry.</li>
<li>try to use that against the entire series, using the speedy code in tslib.array_strptime</li>
<li>if that works, we've saved heaps of time, if not fall back to the current slower behaviour of using dateutil parse each time.</li>
</ul>
<p dir="auto">Here's some pseudo-code::</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="datestr1 = s[0]
# I'm assuming dateutil has something like this, that can tell you what the format is for a given date string.
date_format = figure_out_datetime_format(datestr1)

try:
    # use the super speed code that pandas uses when you tell it what the format is.
    dt_series = tslib.array_strptime(s, format=datestr1, *, ...)
except:
    # date strings aren't consistent after all. Let's do it the old slow way.
    dt_series = tslib.array_to_datetime(s, format=None)"><pre class="notranslate"><code class="notranslate">datestr1 = s[0]
# I'm assuming dateutil has something like this, that can tell you what the format is for a given date string.
date_format = figure_out_datetime_format(datestr1)

try:
    # use the super speed code that pandas uses when you tell it what the format is.
    dt_series = tslib.array_strptime(s, format=datestr1, *, ...)
except:
    # date strings aren't consistent after all. Let's do it the old slow way.
    dt_series = tslib.array_to_datetime(s, format=None)
</code></pre></div>
<p dir="auto">return dt_series</p>
<h4 dir="auto">Code Sample, a copy-pastable example if possible</h4>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="import pandas as pd
import numpy as np

df = pd.DataFrame({'a': [1, 10, 8, 11, 8],
    'b': list('abdce'),
    'c': [1.0, 2.0, np.nan, 3.0, 4.0]})
print('_________')
print(df.nlargest(10,['a','b']))"><pre class="notranslate"><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">import</span> <span class="pl-s1">numpy</span> <span class="pl-k">as</span> <span class="pl-s1">np</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-s">'a'</span>: [<span class="pl-c1">1</span>, <span class="pl-c1">10</span>, <span class="pl-c1">8</span>, <span class="pl-c1">11</span>, <span class="pl-c1">8</span>],
    <span class="pl-s">'b'</span>: <span class="pl-en">list</span>(<span class="pl-s">'abdce'</span>),
    <span class="pl-s">'c'</span>: [<span class="pl-c1">1.0</span>, <span class="pl-c1">2.0</span>, <span class="pl-s1">np</span>.<span class="pl-s1">nan</span>, <span class="pl-c1">3.0</span>, <span class="pl-c1">4.0</span>]})
<span class="pl-en">print</span>(<span class="pl-s">'_________'</span>)
<span class="pl-en">print</span>(<span class="pl-s1">df</span>.<span class="pl-en">nlargest</span>(<span class="pl-c1">10</span>,[<span class="pl-s">'a'</span>,<span class="pl-s">'b'</span>]))</pre></div>
<h4 dir="auto">Problem description</h4>
<p dir="auto">DataFrame的nlargest在遇到rank相同的情况时，结果错误。如下，第二行和第四行反复出现了。</p>
<h4 dir="auto">Expected Output</h4>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="    a  b    c
3  11  c  3.0
1  10  b  2.0
2   8  d  NaN
4   8  e  4.0
2   8  d  NaN
4   8  e  4.0
0   1  a  1.0
[Finished in 0.6s]"><pre class="notranslate"><code class="notranslate">    a  b    c
3  11  c  3.0
1  10  b  2.0
2   8  d  NaN
4   8  e  4.0
2   8  d  NaN
4   8  e  4.0
0   1  a  1.0
[Finished in 0.6s]
</code></pre></div>
<h4 dir="auto">Output of <code class="notranslate">pd.show_versions()</code></h4>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="INSTALLED VERSIONS
------------------
commit: None
python: 3.6.0.final.0
python-bits: 64
OS: Windows
OS-release: 10
machine: AMD64
processor: Intel64 Family 6 Model 94 Stepping 3, GenuineIntel
byteorder: little
LC_ALL: None
LANG: None
LOCALE: None.None"><pre class="notranslate"><code class="notranslate">INSTALLED VERSIONS
------------------
commit: None
python: 3.6.0.final.0
python-bits: 64
OS: Windows
OS-release: 10
machine: AMD64
processor: Intel64 Family 6 Model 94 Stepping 3, GenuineIntel
byteorder: little
LC_ALL: None
LANG: None
LOCALE: None.None
</code></pre></div>