【问题标题】:Jupyter: Why does this second command stop the first one from working?Jupyter:为什么第二个命令会阻止第一个命令工作?
【发布时间】:2019-04-03 19:13:58
【问题描述】:

我在 Jupyter Notebook 中。我使用这些库:

from fastai.tabular import add_datepart
import pandas as pd

df_raw 是一个 pd 数据帧。

我遇到了这个非常奇怪的问题,当我使用第二个命令然后使用第一个命令重新运行单元时,第一个命令将停止工作:

第一:

>>> add_datepart(df_raw, 'saledate')

第二:

>>> df_raw.saleYear.head()

这是我得到的错误:

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
~/anaconda3/lib/python3.6/site-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance)
   2656             try:
-> 2657                 return self._engine.get_loc(key)
   2658             except KeyError:

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

KeyError: 'saledate'

During handling of the above exception, another exception occurred:

KeyError                                  Traceback (most recent call last)
<ipython-input-43-6b52dab581de> in <module>()
----> 1 add_datepart(df_raw, 'saledate')

~/anaconda3/lib/python3.6/site-packages/fastai/tabular/transform.py in add_datepart(df, field_name, prefix, drop, time)
     55 def add_datepart(df:DataFrame, field_name:str, prefix:str=None, drop:bool=True, time:bool=False):
     56     "Helper function that adds columns relevant to a date in the column `field_name` of `df`."
---> 57     make_date(df, field_name)
     58     field = df[field_name]
     59     prefix = ifnone(prefix, re.sub('[Dd]ate$', '', field_name))

~/anaconda3/lib/python3.6/site-packages/fastai/tabular/transform.py in make_date(df, date_field)
     10 def make_date(df:DataFrame, date_field:str):
     11     "Make sure `df[field_name]` is of the right date type."
---> 12     field_dtype = df[date_field].dtype
     13     if isinstance(field_dtype, pd.core.dtypes.dtypes.DatetimeTZDtype):
     14         field_dtype = np.datetime64

~/anaconda3/lib/python3.6/site-packages/pandas/core/frame.py in __getitem__(self, key)
   2925             if self.columns.nlevels > 1:
   2926                 return self._getitem_multilevel(key)
-> 2927             indexer = self.columns.get_loc(key)
   2928             if is_integer(indexer):
   2929                 indexer = [indexer]

~/anaconda3/lib/python3.6/site-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance)
   2657                 return self._engine.get_loc(key)
   2658             except KeyError:
-> 2659                 return self._engine.get_loc(self._maybe_cast_indexer(key))
   2660         indexer = self.get_indexer([key], method=method, tolerance=tolerance)
   2661         if indexer.ndim > 1 or indexer.size > 1:

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

KeyError: 'saledate'

我从来没有遇到过这样的问题,不知道是 pandas、fastai 还是 jupyter 造成的。你能帮忙吗?

edit:我什至不确定它是否只是同时使用两个命令。现在我在没有第二个命令的情况下得到了错误......当我一起运行所有单元格时,它会编译,但是一旦我用“第一个”命令重新运行那个单元格,这个单元格就会崩溃。

【问题讨论】:

    标签: python pandas jupyter-notebook fast-ai


    【解决方案1】:

    docs 中,看起来默认情况下 add_datepart 函数会从原始 DataFrame 中删除输入列。默默地发生这种情况似乎有点草率,但显然您可以通过传递 drop=False 来禁用该行为。

    所以你的电话是

    add_datepart(df_raw, 'saledate', drop=False)
    

    【讨论】:

    • 哦,对了。因此,由于我在第一次运行该命令时放弃了此功能,所以当我第二次运行它时将找不到该功能?感谢您的帮助!
    • 是的,没错。大多数使用 DataFrame 的函数都不会修改原始对象——它们返回修改后的副本。但是你永远不知道第三方库是否遵守规则,而这个似乎没有。
    猜你喜欢
    • 2018-07-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-15
    • 1970-01-01
    • 2018-11-01
    相关资源
    最近更新 更多