简体   繁体   中英

Why is Python throwing this error?

I've defined a function as shown below:

def test_create_dataframe(df):
    cols=set(df.columns)
    return cols

When I put this function into a Jupyter notebook and call it, it returns what it's supposed to, that is:

{'Name', 'accno', 'idno'}

However, when I make a Python module, import that, and call the function through the Jupyter notebook, I get this error:

      9 def test_create_dataframe(df):
     10         cols=set(df.columns)
---> 11         return cols
     12 
     13 

AttributeError: 'list' object has no attribute 'intersection'

Can someone explain why?

The issue you're seeing is due to the underlying iPython implementation. If you:

  1. Import a module into your main script
  2. Run that main script
  3. Make changes to the module you import into the main script
  4. Run the main script again

Then you can end up with some very strange results if you then get tracebacks. I'm not exactly sure how the line of an error and the text displayed are knitted together (line numbers are correct but the code it displays for those line numbers doesn't match the actual module). The sure-fire fix is to restart the underlying iPython kernel when you've modified modules that you import, but that's not always practical. You can find more detail on this here

The fixes from that post:

There are several ways to work around this issue.

1) The simplest and most certain is to restart the ipython kernel after changing an imported module. But this has downsides as well, notably losing the data which exists in your ipython namespace and in any other imported modules.

2) For simple cases, you can use python's reload function. (This function is builtin in Python 2. Starting with Python 3.4, it is imported from standard module "importlib"). In many cases, this suffices after editing a module. It is described briefly in this discussion on Stack Overflow and elsewhere online in more detail.

3) For more complex cases, where reloading the module that you have edited also requires that its dependent/imported modules be reloaded (eg because they must be initialized as part of your edited module's initialization), ipython's autoreload extension can be useful. See http://ipython.readthedocs.org/en/stable/config/extensions/autoreload.html including the important caveats at the bottom of the page.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM