本文介绍了TypeError:比较时无法确定大小的对象的len(),我无法理解的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过在传感器的地理坐标周围放置一个框来选择传感器:

I am trying to select sensors by placing a box around their geographic coordinates:

In [1]: lat_min, lat_max = lats(data)
        lon_min, lon_max = lons(data)

        print(np.around(np.array([lat_min, lat_max, lon_min, lon_max]), 5))
Out[1]: [ 32.87248  33.10181 -94.37297 -94.21224]

In [2]: select_sens = sens[(lat_min<=sens['LATITUDE']) & (sens['LATITUDE']<=lat_max) &
                           (lon_min<=sens['LONGITUDE']) & (sens['LONGITUDE']<=lon_max)].copy()
Out[2]: ---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-12-7881f6717415> in <module>()
      4 lon_min, lon_max = lons(data)
      5 select_sens = sens[(lat_min<=sens['LATITUDE']) & (sens['LATITUDE']<=lat_max) &
----> 6                    (lon_min<=sens['LONGITUDE']) & (sens['LONGITUDE']<=lon_max)].copy()
      7 sens_data = data[data['ID'].isin(select_sens['ID'])].copy()
      8 sens_data.describe()

/home/kartik/miniconda3/lib/python3.5/site-packages/pandas/core/ops.py in wrapper(self, other, axis)
    703             return NotImplemented
    704         elif isinstance(other, (np.ndarray, pd.Index)):
--> 705             if len(self) != len(other):
    706                 raise ValueError('Lengths must match to compare')
    707             return self._constructor(na_op(self.values, np.asarray(other)),

TypeError: len() of unsized object

当然,sens是熊猫DataFrame.即使当我使用.where()时,它也会引发相同的错误.我完全感到困惑,因为这是一个简单的比较,不会引起任何错误.甚至数据类型都匹配:

Of course, sens is a pandas DataFrame. Even when I use .where() it raises the same error. I am completely stumped, because it is a simple comparison that shouldn't raise any errors. Even the data types match:

In [3]: sens.dtypes
Out[3]: ID              object
        COUNTRY         object
        STATE           object
        COUNTY          object
        LENGTH         float64
        NUMBER          object
        NAME            object
        LATITUDE       float64
        LONGITUDE      float64
        dtype: object

那是怎么回事?!?

-----编辑------ 根据Ethan Furman的回答,我进行了以下更改:

-----EDIT------ As per Ethan Furman's answer, I made the following changes:

In [2]: select_sens = sens[([lat_min]<=sens['LATITUDE']) & (sens['LATITUDE']<=[lat_max]) &
                           ([lon_min]<=sens['LONGITUDE']) & (sens['LONGITUDE']<=[lon_max])].copy()

(鼓声)奏效了...但是为什么呢?

And (drumroll) it worked... But why?

推荐答案

我不熟悉NumPy和Pandas,但错误是说比较if len(self) != len(other)中的对象之一没有__len__方法,因此没有长度.

I'm not familiar with NumPy nor Pandas, but the error is saying that one of the objects in the comparison if len(self) != len(other) does not have a __len__ method and therefore has no length.

尝试执行print(sens_data),看看是否出现类似的错误.

Try doing print(sens_data) to see if you get a similar error.

这篇关于TypeError:比较时无法确定大小的对象的len(),我无法理解的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 09:57
查看更多