Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Python] ArrowInvalid: Could not convert <pyarrow.Int32Scalar: 1> with type pyarrow.lib.Int32Scalar: tried to convert to int32 #36088

Closed
randolf-scholz opened this issue Jun 15, 2023 · 2 comments

Comments

@randolf-scholz
Copy link

randolf-scholz commented Jun 15, 2023

Describe the bug, including details regarding any error messages, version, and platform.

Was trying to convert some json-data to list[int]:

import pyarrow as pa
import json

print(pa.__version__)
data = pa.array(["[1,2,3]", "[4,5,6]"], type=pa.string())
dtype = pa.list_(pa.int32())


def convert_py_list(x:pa.StringScalar) -> list[int]:
    return json.loads(x.as_py())

def convert_pa_list(x:pa.StringScalar) -> list[pa.Scalar]:
    return pa.scalar(json.loads(x.as_py()), type=dtype)

pa.array([convert_py_list(x) for x in data], type=dtype)  # ✔
pa.array([convert_pa_list(x) for x in data], type=dtype)  # ✘ ArrowInvalid

pyarrow version 12.0.1

Component(s)

Python

@raulcd raulcd changed the title ArrowInvalid: Could not convert <pyarrow.Int32Scalar: 1> with type pyarrow.lib.Int32Scalar: tried to convert to int32 [Python] ArrowInvalid: Could not convert <pyarrow.Int32Scalar: 1> with type pyarrow.lib.Int32Scalar: tried to convert to int32 Jun 15, 2023
@AlenkaF
Copy link
Member

AlenkaF commented Jun 15, 2023

convert_pa_list returns a list of pyarrow scalars (ListScalars) comparing to a list of lists:

>>> [convert_py_list(x) for x in data]
[[1, 2, 3], [4, 5, 6]]
>>> [convert_pa_list(x) for x in data]
[<pyarrow.ListScalar: [1, 2, 3]>, <pyarrow.ListScalar: [4, 5, 6]>]

Currently pa.array constructor doesn't except pyarrow scalars, see: #21761
We are currently working on this feature and I hope it will get added to 13.0.0 release.

@jorisvandenbossche
Copy link
Member

Closing as a duplicate of #21761

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants