这是数据集:uk_quar = uk_quar[['latitude','longitude']]uk_quar.head() latitude longitude0 31°03'58.9"N 78°07'15.2"E1 30°20'42.9"N 78°27'37.1"E2 30°19'15.3"N 78°32'10.3"E3 30°17'55.3"N 78°26'09.2"E4 NaN NaN5 30°23'58.8"N 77°45'20.7"E当我尝试使用 dms2dec 转换为十进制坐标时,如下所示:uk_quar['lat_dms']=uk_quar['latitude'].apply(dms2dec)我得到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列的当前数据类型以供参考。uk_quar['latitude'].dtypesdtype('O')如何解决此错误并转换为十进制坐标?
1 回答
不负相思意
TA贡献1777条经验 获得超10个赞
我认为问题是 NaN 值,请尝试:
uk_quar['lat_dms'] = np.nan
uk_quar['lat_dms']=uk_quar.loc[uk_quar['latitude'].notnull(), 'latitude'].apply(dms2dec)
添加回答
举报
0/150
提交
取消