我正试图在gmaps中创建一个气泡图,气泡的大小基于pandas数据帧中一列中的值。我弄错了"TraitError:Symbol实例的"scale"特征必须是int,但指定的值为2335。">
我的数据集是这样的:
https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_confirmed_global.csv
我将数据帧命名为"cgr",然后我的代码是:
data_layer = gmaps.symbol_layer(
cgr[['Lat','Long']], fill_color='green', stroke_color='green', scale=cgr['5/1/20']
)
fig = gmaps.figure()
fig.add_layer(data_layer)
fig
我不明白为什么gmaps不能将numpy.int64数字识别为int,而且我无法将它们转换为原生python int。有什么想法吗?
这是完整的Traceback
TraitError Traceback (most recent call last)
<ipython-input-7-202e1cec5d37> in <module>
1 data_layer = gmaps.symbol_layer(
----> 2 cgr[['Lat','Long']], fill_color='green', stroke_color='green', scale=cgr['5/1/20']
3 )
4 fig = gmaps.figure()
5 fig.add_layer(data_layer)
~anaconda3libsite-packagesgmapsmarker.py in symbol_layer(locations, hover_text, fill_color, fill_opacity, stroke_color, stroke_opacity, scale, info_box_content, display_info_box)
479 fill_opacity, stroke_color, stroke_opacity, scale,
480 info_box_content, display_info_box)
--> 481 symbols = [Symbol(**option) for option in options]
482 return Markers(markers=symbols)
483
~anaconda3libsite-packagesgmapsmarker.py in <listcomp>(.0)
479 fill_opacity, stroke_color, stroke_opacity, scale,
480 info_box_content, display_info_box)
--> 481 symbols = [Symbol(**option) for option in options]
482 return Markers(markers=symbols)
483
~anaconda3libsite-packagesgmapsmarker.py in __init__(self, location, **kwargs)
162 kwargs = _resolve_info_box_kwargs(**kwargs)
163 kwargs['location'] = location
--> 164 super(Symbol, self).__init__(**kwargs)
165
166
~anaconda3libsite-packagesipywidgetswidgetswidget.py in __init__(self, **kwargs)
410 """Public constructor"""
411 self._model_id = kwargs.pop('model_id', None)
--> 412 super(Widget, self).__init__(**kwargs)
413
414 Widget._call_widget_constructed(self)
~anaconda3libsite-packagestraitletstraitlets.py in __init__(self, *args, **kwargs)
995 for key, value in kwargs.items():
996 if self.has_trait(key):
--> 997 setattr(self, key, value)
998 else:
999 # passthrough args that don't set traits to super
~anaconda3libsite-packagestraitletstraitlets.py in __set__(self, obj, value)
583 raise TraitError('The "%s" trait is read-only.' % self.name)
584 else:
--> 585 self.set(obj, value)
586
587 def _validate(self, obj, value):
~anaconda3libsite-packagestraitletstraitlets.py in set(self, obj, value)
557
558 def set(self, obj, value):
--> 559 new_value = self._validate(obj, value)
560 try:
561 old_value = obj._trait_values[self.name]
~anaconda3libsite-packagestraitletstraitlets.py in _validate(self, obj, value)
589 return value
590 if hasattr(self, 'validate'):
--> 591 value = self.validate(obj, value)
592 if obj._cross_validation_lock is False:
593 value = self._cross_validate(obj, value)
~anaconda3libsite-packagestraitletstraitlets.py in validate(self, obj, value)
1868 def validate(self, obj, value):
1869 if not isinstance(value, int):
-> 1870 self.error(obj, value)
1871 return _validate_bounds(self, obj, value)
1872
~anaconda3libsite-packagestraitletstraitlets.py in error(self, obj, value)
623 e = "The '%s' trait must be %s, but a value of %r was specified."
624 % (self.name, self.info(), repr_type(value))
--> 625 raise TraitError(e)
626
627 def get_metadata(self, key, default=None):
TraitError: The 'scale' trait of a Symbol instance must be an int, but a value of 2335 <class 'numpy.int64'> was specified.
如果您查看gmaps的代码,它会使用库traitlets来确保有效的类型使用。
https://github.com/pbugnion/gmaps/blob/4f75c8301de00fd831449fe685bd209b5f5ee3e3/gmaps/marker.py#L137
具体部分:
...
from traitlets import Unicode, Int, List, observe, HasTraits, Bool
...
scale = Int(
default_value=4, allow_none=True, min=1
).tag(sync=True)
据我所知,traitlets不支持Numpy类型,因此您需要将其转换回Python Integer。