猿问

python 如何让时间识别定义的输入

我有一个 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 已定义并正常工作。我怎样才能解决这个问题?


牛魔王的故事
浏览 231回答 1
1回答

Helenr

有两种选择,要么传递一个globals允许timeit解析名称的参数,df = pd.DataFrame(...)timeit.timeit(statement, globals={'df': df}) # globals=globals()...或者,传递一个为您setup设置的字符串参数df。timeit.timeit(statement, setup='import pandas as pd; df = pd.DataFrame(...)')
随时随地看视频慕课网APP

相关分类

Python
我要回答