擅长:python、mysql、java
<p>尝试使用此函数分析日期:</p>
<pre><code>def lookup(s):
"""
This is an extremely fast approach to datetime parsing.
For large data, the same dates are often repeated. Rather than
re-parse these, we store all unique dates, parse them, and
use a lookup to convert all dates.
"""
dates = {date:pd.to_datetime(date) for date in s.unique()}
return s.map(dates)
</code></pre>
<p>像这样使用:</p>
<pre><code>df['date-column'] = lookup(df['date-column'])
</code></pre>
<p>基准:</p>
<pre><code>$ python date-parse.py
to_datetime: 5799 ms
dateutil: 5162 ms
strptime: 1651 ms
manual: 242 ms
lookup: 32 ms
</code></pre>
<p>来源:<a href="https://github.com/sanand0/benchmarks/tree/master/date-parse">https://github.com/sanand0/benchmarks/tree/master/date-parse</a></p>