agentleFS
Kaalia0912Kaalia0912/dsh-data-analysis-mode/skills/pandas-3x/SKILL.md

pandas-3x

Skill2 starsChanged 3 months agoMITView on GitHub

pandas 3.x 高效数据分析技巧与新版行为坑位指南(Copy-on-Write、string dtype、groupby 默认参数等)。当分析任务使用 pandas 且版本为 3.x 时加载。High-performance pandas 3.x patterns and behaviour traps — Copy-on-Write, the new string dtype, changed groupby defaults. Load when an analysis uses pandas 3.x.

The file

Files
SKILL.md

pandas 3.x 实战指南

本技能覆盖 pandas 3.x(当前环境 pandas 3.0.3)的关键行为变化与高效用法,避免写「2.x 时代」的低效或失效代码。

3.x 关键行为变化(写代码前先记住)

  • Copy-on-Write(CoW)默认开启:df["a"] = ... 等链式赋值(chained assignment)不再可靠。改数据统一走显式路径:
    • 单列赋值:df["a"] = values(正常)
    • 按条件改子集:先 mask = df["x"] > 0,再 df.loc[mask, "a"] = 1(必须用 .loc)
    • 绝不写 df[df.x>0]["a"] = 1 这种链式赋值——在 CoW 下会静默无效。
  • 字符串默认 string dtype:str 列不再用 object dtype。不再需要 df["s"] = df["s"].astype("string");df["s"].str.* 直接可用。
  • groupby 的 observed 默认改为 True:分类列分组不再默认展开所有组合。需要「全组合计数」时显式 groupby(..., observed=False)。
  • 数值列缺失值:统一用 pd.NA,np.nan 在整数列会强制转 float;df.info() 看 dtype 时注意 Int64(大写,可空整数)与 int64 的区别。
  • read_csv 解析错误行:用 on_bad_lines="warn" 或 "error"(不要用已废弃的 error_bad_lines)。

高效模式(替代低效写法)

  • 分组统计一次算完:df.groupby("cat").agg(mean=("v","mean"), cnt=("v","count"), med=("v","median")),不要循环 groupby。
  • 组内变换:df["v_ratio"] = df.groupby("cat")["v"].transform(lambda s: s / s.sum())。
  • 透视:pd.crosstab(df["a"], df["b"], normalize="index") 出比例;df.pivot_table(index=..., columns=..., values=..., aggfunc="mean")。
  • 宽长互转:df.melt(id_vars=["id"], var_name="metric", value_name="val") 与 df.pivot(...)。
  • 大文件(>1GB 或百万行):别硬扛 pandas——先试 DuckDB(见 duckdb 技能)或 polars;pandas 读大 CSV 至少指定 dtype、usecols、parse_dates。

与可视化衔接

  • 图表规范见 data-analysis 技能的检查清单(标题、轴标签、单位)。
  • Windows 中文标签:matplotlib 用 plt.rcParams["font.sans-serif"] = ["Microsoft YaHei"],并 plt.rcParams["axes.unicode_minus"] = False,否则中文变方块、负号乱码。

交付检查

  • 无链式赋值(CoW 下静默失效)
  • 分组/聚合不用显式循环
  • 大文件已考虑 DuckDB/polars 或分块读取
  • 随机操作固定种子;结果可复现

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.