python如何使timeit识别定义的输入

时间:2019-01-12 22:36:02

标签: python pandas timeit

我定义了一个成功运行操作的df。我想对迭代for循环和向量化操作之间的时间进行计时。我已经阅读了有关如何使用timeit的各种示例,但是当我尝试使用它们时,出现以下错误。我在做什么错了?

进口:

import h5py
import pandas as pd
import timeit

此循环有效:

for u in df['owner'].unique():
    print(u, ': ', len(df[(df['owner'] == u)]), sep = '')

但是当我尝试像这样计时时...:

s = """\
for u in df['owner'].unique():
    print(u, ': ', len(df[(df['owner'] == u)]), sep = '')"""

time_iter_1_1_1 = timeit.timeit(s)

...会产生此错误:

---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-34-7526e96d565c> in <module>()
      3 #     print(u, ': ', len(df[(df['owner'] == u)]), sep = '')""")
      4 
----> 5 time_iter_1_1_1 = timeit.timeit(s)

~\Anaconda2\envs\py36\lib\timeit.py in timeit(stmt, setup, timer, number, globals)
    231            number=default_number, globals=None):
    232     """Convenience function to create Timer object and call timeit method."""
--> 233     return Timer(stmt, setup, timer, globals).timeit(number)
    234 
    235 def repeat(stmt="pass", setup="pass", timer=default_timer,

~\Anaconda2\envs\py36\lib\timeit.py in timeit(self, number)
    176         gc.disable()
    177         try:
--> 178             timing = self.inner(it, self.timer)
    179         finally:
    180             if gcold:

~\Anaconda2\envs\py36\lib\timeit.py in inner(_it, _timer)

NameError: name 'df' is not defined

当我尝试这个时...:

time_iter_1_1_1 = timeit.timeit(
"""for u in df['owner'].unique():
    print(u, ': ', len(df[(df['owner'] == u)]), sep = '')""")

...我收到此错误:

ERROR:root:An unexpected error occurred while tokenizing input
The following traceback may be corrupted or invalid
The error message is: ('EOF in multi-line string', (1, 57))

...

NameError: name 'df' is not defined

df已定义且正在运行。我该如何解决?

1 个答案:

答案 0 :(得分:1)

有两个选项

  1. 传递参​​数globals,该参数允许timeit解析名称,

df = pd.DataFrame(...)
timeit.timeit(statement, globals={'df': df}) # globals=globals()

  1. ...或者,传递一个为您设置setup的字符串参数df

timeit.timeit(statement, setup='import pandas as pd; df = pd.DataFrame(...)')