chore: 补 S 档卡片 / examples;clean.py 收尾,xfail 清零
实现收尾(Codex 代写,用户授权): - impute_missing 的 mode 分支改为返回新表(原先就地改调用方的 DataFrame,另外四支都返回新对象) - detect_outliers 补入参类型校验(§4.7:非法类型抛 TypeError,不静默兜底): 接受 DataFrame / Series / list / tuple / ndarray;tuple 同时补进 is_ld_array; dict 不再被接受(以前靠 pd.DataFrame() 碰巧能过,见决策 33) 测试: - 摘掉最后一条 xfail(mode 就地改)并加强断言 - 新增 3 条:非法类型抛 TypeError、元组按一维数组型处理、全 NaN 列与单元素列不抛异常 - 64 passed / 42 skipped / 0 xfailed(xfail 清零,7 条契约全部转正);ruff 0;pyright 0 文档: - 新增 docs/algorithms/detect_outliers.md —— 第一张 S 档函数卡片(roadmap 要求约 15 张) 除模板九节外多记三条判断:为什么不返回 Result、非数值列恒 False 的两条备选路各错在哪、 ddof 口径与 stats.zscore 不一致(前者 1、后者 0,小样本差一成) - 新增 examples/01_clean_and_describe.py —— 六个 data 函数的最小用法,实测跑通(exit 0) - STATUS.md:E 节收尾、验证结果表、下一步重写(transform/standardize 的待定项)、决策 33 - CHANGELOG.md:把 [Unreleased] 里重复 7 次的 Added/Changed 小节合并成 Added/Changed/Notes; 并修掉上轮遗留的缺陷 —— pyright 那条 bullet 的开头被吞掉,半句挂在 ruff 那条后面
This commit is contained in:
1 parent
b6dc4bc5a6
commit
5749e50e83
8 files changed
+344
-51
No files matched your search
@@ -109,12 +109,15 @@ def impute_missing(data: Any, method: str = "mean", **kwargs: Any) -> Any:
|
||||
if method == 'median':
|
||||
return data.fillna(data.median(numeric_only=True),**kwargs)
|
||||
if method == 'mode':
|
||||
mode_df = data.mode()
|
||||
for col in data.columns:
|
||||
# 只有这一支要逐列赋值,所以在这里单独复制一份,别就地改调用方的表
|
||||
# (其余四支走 fillna / ffill / interpolate,本来就返回新对象)。
|
||||
result = data.copy()
|
||||
mode_df = result.mode()
|
||||
for col in result.columns:
|
||||
if mode_df[col].empty:
|
||||
continue
|
||||
data[col] = data[col].fillna(mode_df[col].iloc[0])
|
||||
return data
|
||||
result[col] = result[col].fillna(mode_df[col].iloc[0])
|
||||
return result
|
||||
if method == 'ffill':
|
||||
return data.ffill(**kwargs)
|
||||
return data.interpolate(**kwargs)
|
||||
@@ -126,7 +129,7 @@ def detect_outliers(data: Any, method: Literal["iqr", "zscore"] = "iqr", **kwarg
|
||||
Parameters
|
||||
----------
|
||||
data:
|
||||
`pandas.DataFrame` / `pandas.Series`,或能转成表的数组型。
|
||||
`pandas.DataFrame` / `pandas.Series`,或 `list` / `tuple` / `numpy.ndarray`。
|
||||
method:
|
||||
`"iqr"`:Q1-1.5·IQR 之外(`k` 可调,默认 1.5);
|
||||
`"zscore"`:|z| > threshold(默认 3.0)。
|
||||
@@ -139,6 +142,8 @@ def detect_outliers(data: Any, method: Literal["iqr", "zscore"] = "iqr", **kwarg
|
||||
|
||||
Raises
|
||||
------
|
||||
TypeError
|
||||
`data` 既不是 `pandas.DataFrame` / `pandas.Series`,也不是 `list` / `tuple` / `numpy.ndarray`。
|
||||
ValueError
|
||||
`method` 既不是 `"iqr"` 也不是 `"zscore"`。
|
||||
|
||||
@@ -156,8 +161,11 @@ def detect_outliers(data: Any, method: Literal["iqr", "zscore"] = "iqr", **kwarg
|
||||
>>> bool(mask[-1])
|
||||
True
|
||||
"""
|
||||
if not isinstance(data, (pd.DataFrame, pd.Series, list, tuple, np.ndarray)):
|
||||
raise TypeError(f"data必须是pandas DataFrame/Series 或数组型,你传入了{type(data)}")
|
||||
|
||||
is_series = isinstance(data, pd.Series)
|
||||
is_ld_array = isinstance(data, (list, np.ndarray)) and np.array(data).ndim == 1
|
||||
is_ld_array = isinstance(data, (list, tuple, np.ndarray)) and np.array(data).ndim == 1
|
||||
|
||||
if isinstance(data,pd.Series):
|
||||
data = data.to_frame()
|
||||
|
||||
Reference in new issue
Block a user