Pandas有时,pd.Series.isin是可调用的,有时会引发Series对象不可调用的异常

时间:2018-05-29 04:37:43

标签: python-3.x pandas

以下代码抛出一个Series对象是可调用的异常。我添加了一个测试来查看isin方法是否可调用:它按预期返回True。但我仍然得到这个例外: ```

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)

哪个输出:

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
```

它让我很疯狂,它可以在.isin调用之前在行上调用,但是python认为它不可调用并引发异常。我试图在更小,更简洁的环境中重现问题,而且我无法这样做。

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]: 

也如预期的那样。

我无法在jupyter之外重现这个问题。

我是一名熊猫新手。我也是一个jupyter新手。我有点像jupyter,但如果我要遇到这些奇怪的错误,那么我必须回到使用pycharm,这并不是那么糟糕。我不知道其他信息可能有用或者寻找什么。特别是,我完全不理解为什么可调用(owners.isin)但引发异常。

谢谢

0 个答案:

没有答案