1

I have two Excel sheets with x und y coordinates (EPSG 4326) and several other attributes. My aim is to match both Excel sheets via the coordinates (geopandas.sjoin_nearest). I try to convert the coordinates into float with

gdf1 = gpd.GeoDataFrame(df1,geometry = gpd.points_from_xy(df1.x.astype('float16'), df1.y.astype('float16')))

but then I get "could not convert into string". I already used "df1['x'] = pd.to_numeric(df1['x'], errors='coerce')", but it does not get any better. How can I solve this problem?

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[22], line 26
     18 df2.to_excel("C:\\Users\\...", index = False)

---> 26 gdf1 = gpd.GeoDataFrame(df1,geometry = gpd.points_from_xy(df1.x.astype('float16'), df1.y.astype('float16')))

   (...)
     
     37 result_match = gdf1.sjoin_nearest(gdf2, how = 'right', max_distance = 500)


File ~\.conda\envs\geo_env\Lib\site-packages\pandas\core\internals\managers.py:431, in BaseBlockManager.astype(self, dtype, copy, errors)
    428 elif using_copy_on_write():
    429     copy = False
--> 431 return self.apply(
    432     "astype",
    433     dtype=dtype,
    434     copy=copy,
    435     errors=errors,
    436     using_cow=using_copy_on_write(),
    437 )

File ~\.conda\envs\geo_env\Lib\site-packages\pandas\core\internals\managers.py:364, in BaseBlockManager.apply(self, f, align_keys, **kwargs)
    362         applied = b.apply(f, **kwargs)
    363     else:
--> 364         applied = getattr(b, f)(**kwargs)
    365     result_blocks = extend_blocks(applied, result_blocks)
    367 out = type(self).from_blocks(result_blocks, self.axes)

File ~\.conda\envs\geo_env\Lib\site-packages\pandas\core\internals\blocks.py:758, in Block.astype(self, dtype, copy, errors, using_cow, squeeze)
    755         raise ValueError("Can not squeeze with more than one column.")
    756     values = values[0, :]  # type: ignore[call-overload]
--> 758 new_values = astype_array_safe(values, dtype, copy=copy, errors=errors)
    760 new_values = maybe_coerce_values(new_values)
    762 refs = None

File ~\.conda\envs\geo_env\Lib\site-packages\pandas\core\dtypes\astype.py:237, in astype_array_safe(values, dtype, copy, errors)
    234     dtype = dtype.numpy_dtype
    236 try:
--> 237     new_values = astype_array(values, dtype, copy=copy)
    238 except (ValueError, TypeError):
    239     # e.g. _astype_nansafe can fail on object-dtype of strings
    240     #  trying to convert to float
    241     if errors == "ignore":

File ~\.conda\envs\geo_env\Lib\site-packages\pandas\core\dtypes\astype.py:182, in astype_array(values, dtype, copy)
    179     values = values.astype(dtype, copy=copy)
    181 else:
--> 182     values = _astype_nansafe(values, dtype, copy=copy)
    184 # in pandas we don't store numpy str dtypes, so convert to object
    185 if isinstance(dtype, np.dtype) and issubclass(values.dtype.type, str):

File ~\.conda\envs\geo_env\Lib\site-packages\pandas\core\dtypes\astype.py:133, in _astype_nansafe(arr, dtype, copy, skipna)
    129     raise ValueError(msg)
    131 if copy or arr.dtype == object or dtype == object:
    132     # Explicit copy, or required since NumPy can't view from / to object.
--> 133     return arr.astype(dtype, copy=True)
    135 return arr.astype(dtype, copy=copy)

ValueError: could not convert string to float: '8,9594966'

enter image description here

enter image description here

2
  • Can you share the excel files?
    – BERA
    Feb 13 at 17:01
  • Here is a short example from the excel sheets
    – noj
    Feb 13 at 20:16

1 Answer 1

2

Decimal separator

I think you live in a country with comma separator instead of point as i can see from your screenshot.

https://en.wikipedia.org/wiki/Decimal_separator

Python needs points and doesn't understand that '8,9594966' is '8.9594966'.

str.replace(",", ".")

or

df = pd.read_csv(r'data.csv', decimal=',')

df = pd.read_excel(r'data.xlsx', decimal=',')

You have to change " , " to " . " bofeore you convert to float.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.