You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

StaticFrame导出Yarn到zip_npz遇RuntimeError,求修复方案

解决StaticFrame Yarn导出zip_npz时的"Store label is not a string"错误

问题描述

尝试将StaticFrame的Yarn导出为zip_npz格式时,触发运行时错误:
StaticFrame RuntimeError: Store label {label} is not a string; provide a label_encoder to StoreConfig

复现代码:

from pathlib import Path
import numpy as np
import static_frame as sf

path = Path(f'datasets')
npz_file = path / 'npz_yarn_test.zip'

frame_data = np.array([[1,2,3],[4,5,6],[7,8,9]])
frames = []
for i in range(1,4):
    frame = sf.Frame(frame_data, columns=('first', 'second', 'third'), name = f'frame_{i}')
    frames.append(frame)

buses = []
for i in range(1,4):
    bus = sf.Bus.from_frames(frames, name = f'bus_{i}')
    buses.append(bus)

sf.Yarn.from_buses(buses, retain_labels=True).to_zip_npz(npz_file)

错误原因

当使用retain_labels=True创建Yarn时,默认会用整数类型的索引作为每个Bus的标签,但NPZ文件要求所有存储的键必须是字符串类型,因此StaticFrame抛出错误,要求提供标签编码器来转换非字符串标签。

解决方案

方案1:创建Yarn时指定字符串标签

手动传入字符串类型的标签(比如使用每个Bus的name属性),确保标签符合NPZ的字符串要求:

from pathlib import Path
import numpy as np
import static_frame as sf

path = Path(f'datasets')
npz_file = path / 'npz_yarn_test.zip'

frame_data = np.array([[1,2,3],[4,5,6],[7,8,9]])
frames = []
for i in range(1,4):
    frame = sf.Frame(frame_data, columns=('first', 'second', 'third'), name = f'frame_{i}')
    frames.append(frame)

buses = []
for i in range(1,4):
    bus = sf.Bus.from_frames(frames, name = f'bus_{i}')
    buses.append(bus)

# 用Bus的name作为字符串标签创建Yarn
yarn = sf.Yarn.from_buses(buses, labels=[bus.name for bus in buses])
yarn.to_zip_npz(npz_file)

方案2:导出时提供标签编码器

如果希望保留原有的整数标签,可以通过StoreConfig传入一个将整数转换为字符串的编码器:

from pathlib import Path
import numpy as np
import static_frame as sf

path = Path(f'datasets')
npz_file = path / 'npz_yarn_test.zip'

frame_data = np.array([[1,2,3],[4,5,6],[7,8,9]])
frames = []
for i in range(1,4):
    frame = sf.Frame(frame_data, columns=('first', 'second', 'third'), name = f'frame_{i}')
    frames.append(frame)

buses = []
for i in range(1,4):
    bus = sf.Bus.from_frames(frames, name = f'bus_{i}')
    buses.append(bus)

# 配置标签编码器,将整数转为字符串
store_config = sf.StoreConfig(label_encoder=str)
sf.Yarn.from_buses(buses, retain_labels=True).to_zip_npz(npz_file, config=store_config)

内容的提问来源于stack exchange,提问作者ddiaz312

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.19 10:35:35