Search code examples
pythonpyarrow

Ways of creating a `pyarrow.StructScalar` directly?


I know I can create a pa.StructScalar by casting:

import pyarrow as pa
import pyarrow.compute as pac

struct_scalar = pac.cast(
    {"hello": "greetings", "world": 5},
    target_type=pa.struct([("hello", pa.string()), ("world", pa.int16())]),
)
print(f"{struct_scalar=}")
print(f"{struct_scalar.type=}")

But is there any other way in which I can create a struct scalar?


Solution

  • Use pyarrow.scalar

    import pyarrow as pa
    
    scalar = pa.scalar(
        {"hello": "greetings", "world": 5},
        type=pa.struct([("hello", pa.string()), ("world", pa.int16())]),
    )