read_excel() got an unexpected keyword argument ‘parse_date‘

博客提及parse_dates,在信息技术领域,这可能与Python数据处理相关,常用于日期解析等操作。

改为:parse_dates

代码【import pandas as pd HISTORICAL_DATA_PATH = r"C:\Users\THINK\Desktop\新建文件夹 (2)\2025年度枢纽公司议题收集统计表1.xlsx" df_old = pd.read_excel(HISTORICAL_DATA_PATH, sheet_name='Sheet1', dtype=str ) df_old】 报错【--------------------------------------------------------------------------- TypeError Traceback (most recent call last) Cell In[73], line 3 1 import pandas as pd 2 HISTORICAL_DATA_PATH = r"C:\Users\THINK\Desktop\新建文件夹 (2)\2025年度枢纽公司议题收集统计表1.xlsx" ----> 3 df_old = pd.read_excel(HISTORICAL_DATA_PATH, sheet_name='Sheet1', dtype=str ) 4 df_old File d:\安装目录\python\Python312\Lib\site-packages\pandas\io\excel\_base.py:517, in read_excel(io, sheet_name, header, names, index_col, usecols, dtype, engine, converters, true_values, false_values, skiprows, nrows, na_values, keep_default_na, na_filter, verbose, parse_dates, date_parser, date_format, thousands, decimal, comment, skipfooter, storage_options, dtype_backend, engine_kwargs) 511 raise ValueError( 512 "Engine should not be specified when passing " 513 "an ExcelFile - ExcelFile already has the engine set" 514 ) 516 try: --> 517 data = io.parse( 518 sheet_name=sheet_name, 519 header=header, 520 names=names, 521 index_col=index_col, 522 usecols=usecols, 523 dtype=dtype, 524 converters=converters, 525 true_values=true_values, 526 false_values=false_values, 527 skiprows=skiprows, ... 100 else: 101 attrib[tag] = obj --> 103 return cls(**attrib) TypeError: DataValidation.__init__() got an unexpected keyword argument 'id' Output is truncated. View as a scrollable element or open in a text editor. Adjust cell output settings...】
03-25
你遇到的这个错误: ``` TypeError: to_numeric() got an unexpected keyword argument 'error' ``` 说明你使用的是 **旧版本的 pandas**(版本号低于 `0.20.0`),因为 `pd.to_numeric(..., errors='coerce')` 这个参数是在 pandas **0.20.0** 版本中才引入的。旧版本中使用的是 `error='ignore'` 或 `error='raise'`,不支持 `coerce`。 --- ## ✅ 解决方案:兼容旧版本的 `pd.to_numeric` 我们可以改用 `try-except` 或者使用 `convert_objects`(旧版本方法)来实现兼容性更强的转换逻辑。 --- ### ✅ 方法 1:使用 `convert_objects`(适用于 pandas < 0.20.0) ```python # 适用于 pandas < 0.20.0 X_processed[col] = X_processed[col].convert_objects(convert_numeric=True) ``` 这会将列中可以转为数字的部分转换为数字,不能转换的变为 `NaN`。 --- ### ✅ 方法 2:手动转换 + 异常处理(兼容所有版本) ```python def safe_to_numeric(s): """ 安全地将 Series 转换为 numeric 类型(兼容旧版本 pandas) """ def convert(x): try: return float(x) except (ValueError, TypeError): return np.nan return pd.Series([convert(x) for x in s], index=s.index, dtype='float64') ``` 然后在你的 `transform` 函数中替换: ```python # 原代码(新版 pandas) # X_processed[col] = pd.to_numeric(X_processed[col], errors='coerce') # 改为(兼容旧版) X_processed[col] = safe_to_numeric(X_processed[col]) ``` --- ## ✅ 更新后的 `transform` 函数(兼容旧 pandas) ```python def safe_to_numeric(s): def convert(x): try: return float(x) except (ValueError, TypeError): return np.nan return pd.Series([convert(x) for x in s], index=s.index, dtype='float64') def transform(X, preprocessor, special_values=None): if isinstance(X, np.ndarray): X = pd.DataFrame(X) if special_values is None: special_values = {} X_processed = X.copy() for col in X.columns: if col not in preprocessor['col_params']: continue params = preprocessor['col_params'][col] lower = params['winsor_lower'] upper = params['winsor_upper'] scaler = params['scaler'] # 替换特殊值为 NaN special_vals = special_values.get(col, []) mask = X_processed[col].isin(special_vals) X_processed.loc[mask, col] = np.nan # 安全转换为 numeric(兼容旧版 pandas) X_processed[col] = safe_to_numeric(X_processed[col]) # 缩尾 X_processed[col] = np.clip(X_processed[col], lower, upper) # 标准化 values = X_processed[col].values.reshape(-1, 1) X_processed[col] = scaler.transform(values) return X_processed.values ``` --- ## ✅ 检查你的 pandas 版本 你可以用以下命令检查你的 pandas 版本: ```python import pandas as pd print(pd.__version__) ``` 如果你的版本低于 `0.20.0`,建议升级(如果可以): ```bash pip install --upgrade pandas ``` --- ## ✅ 总结 | 问题 | 原因 | 解决方法 | |------|------|----------| | `to_numeric() got an unexpected keyword argument 'error'` | pandas 版本过低 | 使用 `convert_objects()` 或手动转换函数 `safe_to_numeric()` | | `pd.to_numeric(..., errors='coerce')` 不可用 | pandas < 0.20.0 | 使用兼容性代码替代 | ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值