Как исправить ошибку "TypeError: ('должно быть действительным числом, а не numpy.str_', 'произошло с индексом 0')" в Python - PullRequest
0 голосов
/ 07 февраля 2019

Я пытаюсь применить функцию get_complaint_count ко всему моему фрейму данных, называемому treeLocations, который содержит значения координат x и y в отдельных столбцах.

Однако я не могу применить функцию ко всемуdataframe.Я не понимаю, где у меня пустая строка.

Спасибо за помощь!

Код:

radius_in_miles = 0.5

def get_complaint_count(r):
    xy = r['latitude'], r['longitude']
    distances, indices = trees_tree.query(xy, k=trees_count, distance_upper_bound=radius_in_miles)
    indices = indices[~np.isnan(indices)]
    return len(indices)

treeLocations['# of Complaints within 0.5 miles'] = treeLocations.apply(get_complaint_count,axis=1)

treeLocations

Ошибка:

TypeError                                 Traceback (most recent call last)
<ipython-input-48-3f1c9b54619b> in <module>
      7     return len(indices)
      8 
----> 9 treeLocations['# of Complaints within 0.5 miles'] = treeLocations.apply(get_complaint_count,axis=1)
     10 
     11 treeLocations

~\Anaconda3\lib\site-packages\pandas\core\frame.py in apply(self, func, axis, broadcast, raw, reduce, result_type, args, **kwds)
   6012                          args=args,
   6013                          kwds=kwds)
-> 6014         return op.get_result()
   6015 
   6016     def applymap(self, func):

~\Anaconda3\lib\site-packages\pandas\core\apply.py in get_result(self)
    140             return self.apply_raw()
    141 
--> 142         return self.apply_standard()
    143 
    144     def apply_empty_result(self):

~\Anaconda3\lib\site-packages\pandas\core\apply.py in apply_standard(self)
    246 
    247         # compute the result using the series generator
--> 248         self.apply_series_generator()
    249 
    250         # wrap results

~\Anaconda3\lib\site-packages\pandas\core\apply.py in apply_series_generator(self)
    275             try:
    276                 for i, v in enumerate(series_gen):
--> 277                     results[i] = self.f(v)
    278                     keys.append(v.name)
    279             except Exception as e:

<ipython-input-48-3f1c9b54619b> in get_complaint_count(r)
      3 def get_complaint_count(r):
      4     xy = r['latitude'], r['longitude']
----> 5     distances, indices = trees_tree.query(xy, k=trees_count, distance_upper_bound=radius_in_miles)
      6     indices = indices[~np.isnan(indices)]
      7     return len(indices)

~\Anaconda3\lib\site-packages\pysal\cg\kdtree.py in query(self, x, k, eps, p, distance_upper_bound)
    157             distance_upper_bound = sphere.arcdist2linear(
    158                 distance_upper_bound, self.radius)
--> 159         d, i = temp_KDTree.query(self, self._toXYZ(x), k,
    160                                           eps=eps, distance_upper_bound=distance_upper_bound)
    161         if isinstance(d, float):

~\Anaconda3\lib\site-packages\pysal\cg\kdtree.py in _toXYZ(self, x)
     98             return x
     99         elif len(x.shape) == 1:
--> 100             x = numpy.array(sphere.toXYZ(x))
    101         else:
    102             x = list(map(sphere.toXYZ, x))

~\Anaconda3\lib\site-packages\pysal\cg\sphere.py in toXYZ(pt)
    110     x, y, z
    111     """
--> 112     phi, theta = list(map(math.radians, pt))
    113     phi, theta = phi + pi, theta + (pi / 2)
    114     x = 1 * sin(theta) * cos(phi)

TypeError: ('must be real number, not numpy.str_', 'occurred at index 0')
...