简体   繁体   English

无法使用 dms2dec 将 Pandas 数据帧坐标转换为小数

[英]Unable to convert Pandas data frame coordinates to decimals using dms2dec

This the dataset:这是数据集:

uk_quar = uk_quar[['latitude','longitude']]
uk_quar.head()

    latitude    longitude
0   31°03'58.9"N    78°07'15.2"E
1   30°20'42.9"N    78°27'37.1"E
2   30°19'15.3"N    78°32'10.3"E
3   30°17'55.3"N    78°26'09.2"E
4   NaN NaN
5   30°23'58.8"N    77°45'20.7"E

When I try to use dms2dec to convert to decimal coordinates as below:当我尝试使用 dms2dec 转换为十进制坐标时,如下所示:

uk_quar['lat_dms']=uk_quar['latitude'].apply(dms2dec)

I get the TypeError: expected string or bytes-like object as below:我得到TypeError: expected string or bytes-like object如下:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-567-2fee9b81372c> in <module>
      1 #Translate the ukd haven radian coordinates into dms coordinates
----> 2 uk_quar['lat_dms']=uk_quar['latitude'].apply(dms2dec)

~/opt/anaconda3/envs/ds/lib/python3.8/site-packages/pandas/core/series.py in apply(self, func, convert_dtype, args, **kwds)
   3846             else:
   3847                 values = self.astype(object).values
-> 3848                 mapped = lib.map_infer(values, f, convert=convert_dtype)
   3849 
   3850         if len(mapped) and isinstance(mapped[0], Series):

pandas/_libs/lib.pyx in pandas._libs.lib.map_infer()

~/opt/anaconda3/envs/ds/lib/python3.8/site-packages/dms2dec/dms_convert.py in dms2dec(dms_str)
     30     """
     31 
---> 32     dms_str = re.sub(r'\s', '', dms_str)
     33 
     34     sign = -1 if re.search('[swSW]', dms_str) else 1

~/opt/anaconda3/envs/ds/lib/python3.8/re.py in sub(pattern, repl, string, count, flags)
    208     a callable, it's passed the Match object and must return
    209     a replacement string to be used."""
--> 210     return _compile(pattern, flags).sub(repl, string, count)
    211 
    212 def subn(pattern, repl, string, count=0, flags=0):

TypeError: expected string or bytes-like object

Column's current datatype for reference.列的当前数据类型供参考。

uk_quar['latitude'].dtypes
dtype('O')

How can I resolve this Error and convert to decimal coordinates?如何解决此错误并转换为十进制坐标?

I think the problem is NaN values, try:我认为问题在于 NaN 值,请尝试:

uk_quar['lat_dms'] = np.nan
uk_quar['lat_dms']=uk_quar.loc[uk_quar['latitude'].notnull(), 'latitude'].apply(dms2dec)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM