mirror of
https://ghfast.top/https://github.com/aeroxw/tick-stock-panel.git
synced 2026-09-12 15:34:16 +08:00
fix(ext-data): List→string 字段直接 cast 报错 — 先 join 再 cast
手动创建的扩展配置拉取 THS 概念接口时, concepts 列为 List(String), 声明为 string 后 cast_df_to_schema 直接 cast(pl.Utf8) 抛 「cannot cast List type (inner: String, to: String)」。 cast_df_to_schema 检测到源列是 List 且目标为 Utf8 时, 先把元素 转字符串再以分号拼接 (与 _flatten_concept_rows 一致), 然后再 cast。 保护任意返回数组字段的配置, 不再依赖预设 id 走 flatten 分支。
This commit is contained in:
@@ -451,10 +451,23 @@ def parse_upload_file(file_path: Path, symbol_col: str = "symbol", data_dir: Pat
|
||||
|
||||
|
||||
def cast_df_to_schema(df: pl.DataFrame, fields: list[ExtField]) -> pl.DataFrame:
|
||||
"""按配置的字段类型转换 DataFrame 列类型。"""
|
||||
"""按配置的字段类型转换 DataFrame 列类型。
|
||||
|
||||
List → string 的处理: 上游接口常返回数组字段 (如 concepts: ["AI", "芯片"]),
|
||||
若声明为 string, 直接 cast 会抛 `cannot cast List type`。
|
||||
这里把列表元素先转字符串再以分号拼接, 与 _flatten_concept_rows 保持一致。
|
||||
"""
|
||||
schema = df.schema
|
||||
for f in fields:
|
||||
if f.name in df.columns:
|
||||
target = _POLARS_DTYPE_MAP.get(f.dtype, pl.Utf8)
|
||||
if f.name not in df.columns:
|
||||
continue
|
||||
target = _POLARS_DTYPE_MAP.get(f.dtype, pl.Utf8)
|
||||
src = schema[f.name]
|
||||
if isinstance(src, pl.List) and target == pl.Utf8:
|
||||
df = df.with_columns(
|
||||
pl.col(f.name).cast(pl.List(pl.Utf8)).list.join(";").cast(target)
|
||||
)
|
||||
else:
|
||||
df = df.with_columns(pl.col(f.name).cast(target))
|
||||
return df
|
||||
|
||||
|
||||
Reference in New Issue
Block a user