Код: Выделить всё
class Cat(pydantic.BaseModel):
name: str
age: int
cats = [Cat(name="a", age=1), Cat(name="b", age=2)]
df = pl.DataFrame({"cats": cats})
df = df.with_columns(pl.lit(0).alias("acq_num"))
def wrap(batch):
return Cat(name="c", age=3)
df = df.group_by("acq_num").agg(pl.col("cats").map_batches(wrap, return_dtype=pl.Struct).alias("cats"))
type(df["cats"][0][0])
# dict
Значит, Polars автоматически преобразует его в словарь, вызывая model_dump из pydantic?
Изменение на
Код: Выделить всё
df = df.group_by("acq_num").agg(pl.col("cats").map_batches(wrap, return_dtype=pl.Object).alias("cats"))
Код: Выделить всё
SchemaError: expected output type 'Object("object", None)', got 'Struct([Field { name: "name", dtype: String }, Field { name: "age", dtype: Int64 }])'; set `return_dtype` to the proper datatype
Как я могу это предотвратить?
Подробнее здесь: https://stackoverflow.com/questions/792 ... onverted-t
Мобильная версия