TypeError: Cannot interpolate with all object-dtype columns in the DataFrame

1. 问题说明

Traceback (most recent call last):
  File "D:\python3.8.5\lib\multiprocessing\pool.py", line 125, in worker
    result = (True, func(*args, **kwds))
  File "E:\01-code\02-wind_profile\all_state\other.py", line 559, in main_function
    df_10min = spectral_wind_profile.shear_process(ws_start=ws_start, ws_end=ws_end)
  File "E:\01-code\02-wind_profile\all_state\other.py", line 128, in shear_process
    df_ten_minute = self.data_processing(df_ten_minute)
  File "E:\01-code\02-wind_profile\all_state\other.py", line 144, in data_processing
    data = data.interpolate(method='linear', limit_direction='backward', axis=1)
  File "D:\python3.8.5\lib\site-packages\pandas\util\_decorators.py", line 311, in wrapper
    return func(*args, **kwargs)
  File "D:\python3.8.5\lib\site-packages\pandas\core\frame.py", line 10931, in interpolate
    return super().interpolate(
  File "D:\python3.8.5\lib\site-packages\pandas\core\generic.py", line 7002, in interpolate
    raise TypeError(
TypeError: Cannot interpolate with all object-dtype columns in the DataFrame. Try setting at least one column to a numeric dtype.
"""
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
  File "D:/PyCharm2023_profession/PyCharm 2023.1/plugins/python/helpers/pydev/pydevd.py", line 1496, in _exec
    pydev_imports.execfile(file, globals, locals)  # execute the script
  File "D:\PyCharm2023_profession\PyCharm 2023.1\plugins\python\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "E:\01-code\02-wind_profile\all_state\other.py", line 706, in <module>
    print('ok')
  File "E:\01-code\02-wind_profile\all_state\other.py", line 696, in main
    ans.append(data)
  File "D:\python3.8.5\lib\multiprocessing\pool.py", line 771, in get
    raise self._value
TypeError: Cannot interpolate with all object-dtype columns in the DataFrame. Try setting at least one column to a numeric dtype.

pandas 不能使用所有的 object 类型的列进行插值。这意味着在使用 pandas 的插值函数(例如 interpolate())时,所有的列都必须是数值类型的。如果其中有一列是 object 类型的,则会抛出异常。

要解决这个问题,你需要将 object 类型的列转换为数值类型。你可以使用 pandas.to_numeric 函数来实现这一点。例如:

import pandasas pd
 
df['column_name'] = pd.to_numeric(df['column_name'], errors='coerce')

参考链接
[1] pandas Cannot interpolate with all object-dtype columns 2023.1;

猜你喜欢

转载自blog.csdn.net/weixin_46713695/article/details/130936363