0

The following code throws a Series object is callable exception. I added a test to see if the isin method is callable: it returns True as expected. But I still get this exception: ```

data = {}
index_lst = []
our_lst = []
their_lst = []
for filename in glob.glob('data/tysonl.2018-05-2*.csv'):
    # filename = f"fred_2018-05-0{m}.csv"
    print(f"Processing {filename}")
    mpd = pd.read_csv(filename, usecols=['Date', 'hostname', 'primary owner', 'status'])
    # This is from 
    # https://stackoverflow.com/questions/28133018/convert-pandas-series-to-datetime-in-a-dataframe
    datetime.date: df_date = pd.to_datetime(mpd["Date"]).dt.date[0]
    pd.core.series.Series: hostnames = mpd.hostname
    pd.core.series.Series: statuses = mpd.status
    pd.core.series.Series: owners = mpd['primary owner']    # Space in column name means has to be an index[] not attribute
    # I don't know why, but sometimes read_csv returns a numpy.ndarray in column primary owner.  It happens in some CSV
    # files and not others.  So I am doing this "hail Mary" conversion.
    if type(owners)!=type(pd.core.series.Series):
        print( f"owners should be of type pandas.core.series.Series but are actually {type(primary_owners)}.  Converting")
        pd.core.series.Series: owners = pd.Series( primary_owners )
        assert isinstance( owners, pd.core.series.Series), "Tried to convert owners to a Series and failed"
    print(type(owners.isin), callable(owners.isin)))
    ours=owners.isin(managers)

Which outputs:

Processing data/tysonl.2018-05-23-prod-quilt-status.csv
**<class 'method'> True**

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-70-235b583cc64f> in <module>()
     21         assert isinstance( owners, pd.core.series.Series), "Tried to convert owners to a Series and failed"
     22     print(type(owners.isin), callable(owners.isin))
---> 23     ours=owners.isin(managers)
     24     # assert df_date not in index_lst, f"df_date {df_date} is already in the index_lst\n{index_lst}\nfilename is {filename}."
     25     if dt_date not in index_lst:

/usr/local/lib/python3.6/dist-packages/pandas/core/series.py in isin(self, values)
   2802         """
   2803         result = algorithms.isin(_values_from_object(self), values)
-> 2804         return self._constructor(result, index=self.index).__finalize__(self)
   2805 
   2806     def between(self, left, right, inclusive=True):

TypeError: 'Series' object is not callable
```

It drives me nuts that it is callable on the line before the .isin call, and yet python thinks it is not callable and raises the exception. I tried to reproduce the problem in a smaller, more concise environment, and I was UNABLE to do so.

jeffs@jeffs-desktop:/home/jeffs/learn_pandas (development) *  $ python3
Python 3.6.5 (default, Apr  1 2018, 05:46:30) 
[GCC 7.3.0] on linux
>>> s = pd.Series(['lama', 'cow', 'lama', 'beetle', 'lama','hippo'], name='animal')
>>> s.isin(['cow'])
0    False
1     True
2    False
3    False
4    False
5    False
Name: animal, dtype: bool
>>> 
jeffs@jeffs-desktop:/home/jeffs/learn_pandas (development) *  $ ipython
Python 3.6.5 (default, Apr  1 2018, 05:46:30) 
Type 'copyright', 'credits' or 'license' for more information
IPython 6.3.1 -- An enhanced Interactive Python. Type '?' for help.

In [1]: import sys

In [2]: print(sys.version)
3.6.5 (default, Apr  1 2018, 05:46:30) 
[GCC 7.3.0]

In [3]: import pandas

In [4]: import pandas as pd

In [5]: s = pd.Series(['lama', 'cow', 'lama', 'beetle', 'lama','hippo'], name='animal')

In [6]: s.isin(['cow'])

Out[6]: 
0    False
1     True
2    False
3    False
4    False
5    False
Name: animal, dtype: bool

In [7]: 

Also as expected.

I cannot reproduce the problem at all outside of jupyter.

I am a pandas newbie. I'm also a jupyter newbie. I kinda like jupyter, but if I am going to have these kinds of strange errors, then I have to go back to using pycharm, which is not that bad. I have no idea what other information might be useful or what to look for. In particular, I utterly fail to understand why callable(owners.isin) but the exception is raised.

Thank you

Jeff Silverman
  • 692
  • 1
  • 8
  • 15

0 Answers0