Skip to content

[Python] Scalar constructors should be callable #31648

Closed as not planned
Closed as not planned

Description

@asfimport

Currently, to create a Scalar instance you must call the pa.scalar factory function. This works fine for the simplest types, but not for more elaborate ones.

For example, in C++ it is trivial to create a ListScalar from the embedded std::shared_ptr<Array>. But not in Python:

>>> arr = pa.array(["abc", "def", "ghi"])
>>> pa.ListScalar(arr)
Traceback (most recent call last):
  Input In [7] in <cell line: 1>
    pa.ListScalar(arr)
TypeError: __init__() takes exactly 0 positional arguments (1 given)

There is a clumsy workaround that creates an intermediate ListArray with trivial offsets:

>>> list_arr = pa.ListArray.from_arrays([0, len(arr)], arr)
>>> list_arr[0]
<pyarrow.ListScalar: ['abc', 'def', 'ghi']>

Reporter: Antoine Pitrou / @pitrou

Related issues:

Note: This issue was originally created as ARROW-16254. Please see the migration documentation for further details.

Activity

  1. asfimport commented on Apr 20, 2022

    @asfimport
    Author
  2. asfimport commented on Apr 21, 2022

    @asfimport
    Author

    Joris Van den Bossche / @jorisvandenbossche:
    For this specific example, the pa.scalar(..) factory function also seems to work?

    In [32]: pa.scalar(["abc", "def", "ghi"])
    Out[32]: <pyarrow.ListScalar: ['abc', 'def', 'ghi']>
  3. asfimport commented on Apr 21, 2022

    @asfimport
    Author

    Antoine Pitrou / @pitrou:
    It does not if you pass an existing PyArrow array:

    >>> arr = pa.array(["abc", "def", "ghi"])
    >>> pa.scalar(arr)
    Traceback (most recent call last):
      ...
    ArrowInvalid: Could not convert <pyarrow.lib.StringArray object at 0x7f79ab490880>
    [
      "abc",
      "def",
      "ghi"
    ] with type pyarrow.lib.StringArray: did not recognize Python value type when inferring an Arrow data type
    >>> pa.scalar(arr, type=pa.list_(arr.type))
    Traceback (most recent call last):
      ...
    ArrowTypeError: Expected bytes, got a 'pyarrow.lib.StringScalar' object
  4. asfimport commented on Apr 21, 2022

    @asfimport
    Author

    Joris Van den Bossche / @jorisvandenbossche:
    Ah, yes. Now, that relates to the issue that pa.array(..) also does not accept pyarrow arrays as scalars, see ARROW-5295

  5. github-actions commented on Mar 27, 2026

    @github-actions

    This issue has been marked as stale because it has had no activity in the past 365 days. Please remove the stale label or comment below, or this issue will be closed in 14 days. If this improvement is still desired but has no current owner, please add the 'Status: needs champion' label.

  6. added
    Status: stale-warningIssues and PRs flagged as stale which are due to be closed if no indication otherwise
    on Mar 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions