在 Jupyter 中使用单元格魔法“%%time”的意外行为

Gio*_*Gio 5 python pandas jupyter-notebook

我只是想将数据帧导入笔记本,运行以下单元格:

%%time
#df=pd.DataFrame()
#for chunk in pd.read_sql(qry,conn,chunksize=10**5):
#    df=pd.concat([df,chunk])
df=pd.read_csv(scriptdir+'\LocalSample\sample200k.txt', sep=";")
Run Code Online (Sandbox Code Playgroud)

虽然它没有返回任何错误,但在运行下一个命令时

df.head(20)
Run Code Online (Sandbox Code Playgroud)

我收到一个名称错误:

---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-8-e9cb96795106> in <module>
----> 1 df.head(20)

NameError: name 'df' is not defined
Run Code Online (Sandbox Code Playgroud)

移除%%time魔法解决了这个问题。这是一种已知的行为吗?有没有人对为什么会发生这种情况有任何解释?