问题
I am plotting the data with MapBoxGl Python Library on maps, here is my code which is taking the latitude, longitude and points from the Pandas DataFrame and trying to make the geojson, here is the code
data4 = df_to_geojson(result, properties= ['speed'], lat='lat', lon='lon')
print (data4)
but I am getting this error, I am not familiar with the error, I looked for it but didn't find any solution:
> --------------------------------------------------------------------------- ValueError Traceback (most recent call
> last) ~\Anaconda3\lib\site-packages\IPython\core\formatters.py in
> __call__(self, obj)
> 691 type_pprinters=self.type_printers,
> 692 deferred_pprinters=self.deferred_printers)
> --> 693 printer.pretty(obj)
> 694 printer.flush()
> 695 return stream.getvalue()
>
> ~\Anaconda3\lib\site-packages\IPython\lib\pretty.py in pretty(self,
> obj)
> 363 if cls in self.type_pprinters:
> 364 # printer registered in self.type_pprinters
> --> 365 return self.type_pprinters[cls](obj, self, cycle)
> 366 else:
> 367 # deferred printer
>
> ~\Anaconda3\lib\site-packages\IPython\lib\pretty.py in inner(obj, p,
> cycle)
> 594 if basetype is not None and typ is not basetype and typ.__repr__ != basetype.__repr__:
> 595 # If the subclass provides its own repr, use it instead.
> --> 596 return p.text(typ.__repr__(obj))
> 597
> 598 if cycle:
>
> ~\Anaconda3\lib\site-packages\geojson\base.py in __repr__(self)
> 25
> 26 def __repr__(self):
> ---> 27 return geojson.dumps(self, sort_keys=True)
> 28
> 29 __str__ = __repr__
>
> ~\Anaconda3\lib\site-packages\geojson\codec.py in dumps(obj, cls,
> allow_nan, **kwargs)
> 30 def dumps(obj, cls=GeoJSONEncoder, allow_nan=False, **kwargs):
> 31 return json.dumps(to_mapping(obj),
> ---> 32 cls=cls, allow_nan=allow_nan, **kwargs)
> 33
> 34
>
> ~\Anaconda3\lib\json\__init__.py in dumps(obj, skipkeys, ensure_ascii,
> check_circular, allow_nan, cls, indent, separators, default,
> sort_keys, **kw)
> 236 check_circular=check_circular, allow_nan=allow_nan, indent=indent,
> 237 separators=separators, default=default, sort_keys=sort_keys,
> --> 238 **kw).encode(obj)
> 239
> 240
>
> ~\Anaconda3\lib\json\encoder.py in encode(self, o)
> 197 # exceptions aren't as detailed. The list call should be roughly
> 198 # equivalent to the PySequence_Fast that ''.join() would do.
> --> 199 chunks = self.iterencode(o, _one_shot=True)
> 200 if not isinstance(chunks, (list, tuple)):
> 201 chunks = list(chunks)
>
> ~\Anaconda3\lib\json\encoder.py in iterencode(self, o, _one_shot)
> 255 self.key_separator, self.item_separator, self.sort_keys,
> 256 self.skipkeys, _one_shot)
> --> 257 return _iterencode(o, 0)
> 258
> 259 def _make_iterencode(markers, _default, _encoder, _indent, _floatstr,
>
> ValueError: Out of range float values are not JSON compliant
回答1:
Are there any NaNs in your dataframe? If so, this issue seems to be related. Hard for me to tell what exactly is wrong without seeing the contents of the dataframe in question, but a possible fix is to remove any NaNs or other unserializable values before making the call to df_to_geojson().
回答2:
well, I got the correct answer for my question of course I removed all NAN values but still there were inf values in my dataframe, so as Instructed by someone I tried to find the description of the whole column such as
df['column'].describe()
this line gave the min, max, mean std and other values, so my max value was going to inf, so I removed this inf value with the following command and it worked
df = df[~df.isin([np.nan, np.inf, -np.inf]).any(1)]
this solved my issue. Reference for the solution
来源:https://stackoverflow.com/questions/47042871/do-any-one-know-about-this-error-in-python-how-can-i-resolve-this