Python 熊猫0.18对0.12的表现
我有以下代码:Python 熊猫0.18对0.12的表现,python,performance,pandas,dataframe,Python,Performance,Pandas,Dataframe,我有以下代码: from datetime import date, timedelta from time import time import pandas as pd sizes = [500] base_date = date(2016,10,31) for n in sizes: dates = [base_date - timedelta(days = x) for x in range(1, n, 1)] dates_df = pd.DataFrame({'D
from datetime import date, timedelta
from time import time
import pandas as pd
sizes = [500]
base_date = date(2016,10,31)
for n in sizes:
dates = [base_date - timedelta(days = x) for x in range(1, n, 1)]
dates_df = pd.DataFrame({'DATE' : dates, 'key' : 1})
identifiers = range(1, 5000)
identifiers_df = pd.DataFrame({'IDENTIFIER' : identifiers, 'key' : 1})
df = pd.merge(dates_df, identifiers_df, on='key')
df = df.set_index(['DATE', 'IDENTIFIER'])
df = df.sort_index(axis = 0, level = ['DATE', 'IDENTIFIER'], ascending=False)
start_time = time()
for d in dates:
temp = df.ix[d]
end_time = time()
print ('%s %s' % (n, end_time - start_time))
熊猫0.12的最终打印时间为0.15秒,而熊猫0.18的打印时间为8.5秒。你知道为什么会有这种行为差异吗?此外,看起来熊猫0.12使用随机访问,而0.18不使用,因为打印时间也是为0.18选择的大小的函数
正如下面的一条评论所建议的,我已经尝试用cProfile分析前面的代码,两者之间的主要区别似乎在于调用getitem:
Pandas 0.18
ncalls tottime percall cumtime percall filename:lineno(function)
998/499 0.006 0 6.027 0.012 indexing.py:1286(__getitem__)
Pandas 0.12
ncalls tottime percall cumtime percall filename:lineno(function)
499 0.001 0 0.163 0 indexing.py:695(__getitem__)
提前非常感谢您的帮助!
Giuliano在pandas的较新版本中,iloc是首选的,比ix更优化,不久将被弃用。尝试用iloc移植代码并检查性能 你试过分析吗?ix执行标签索引(并且可以依赖于位置),而loc只是标签索引。然而,iloc纯粹是位置性的。您似乎在暗示多重索引或按日期索引(时间序列分析的典型情况)应该在未来的版本中消失?如果是这样的话,你能发送一个链接来确认这个声明吗?谢谢你的链接!我将代码改为使用loc,而不是ix,它在0.18中比0.12慢得多,15秒比1.9秒慢得多。