Do any one know about this Error in python? how can I resolve this?
Asked Answered
A

2

-1

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
Aharon answered 31/10, 2017 at 19:4 Comment(2)
stackoverflow.com/help/mcveScrimp
Thanks for editing, I was facing issues while posting also.Aharon
A
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

Aharon answered 1/11, 2017 at 19:30 Comment(0)
B
0

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().

Backed answered 31/10, 2017 at 19:12 Comment(3)
Seems like you've a NaN value in your dataset.Bouie
Yes there was one NAN column, I removed that, but still I have the same Issue, no more Nan or None values available in the DatasetAharon
I got the inf values in my dataframe which were causing issue with the floating points. its solved when I removed the inf values, i posted the solution.Aharon

© 2022 - 2024 — McMap. All rights reserved.