Skip to content

[C++][Python] Python to Arrow array conversion sometimes ignores "safe=False" or is using unsafe instead of safe cast #36202

Open

Description

@AlenkaF

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

There are two cases where the array constructor taking the python conversion path (python_to_arrow.cc) doesn't handle the safe keyword properly.

The safe keyword is set to True by default and is ignored if passed

  • One example where python list is taking python_to_arrow.cc code path are Decimals (which are python objects). Here the conversion from Decimal to int does not overflow by default and one can’t turn it off with safe=False:
>>> import pyarrow as pa
>>> pa.array([Decimal('1234')]).cast(pa.int8(), safe=False)                                                                                                                 
<pyarrow.lib.Int8Array object at 0x7efbbd18e4b0>
[
  -46
]
>>> pa.array([Decimal('1234')], pa.int8(), safe=False)                                                                                                                      
Traceback (most recent call last):
  ...
ArrowInvalid: Value 1234 too large to fit in C integer type

In some cases the safe keyword is ignored and does unsafe conversions

  • Nested case
>>> import pyarrow as pa
>>> pa.array(np.array([[1.5], [2.5, 3.5]], dtype=object), type=pa.list_(pa.int64()), safe=True)
<pyarrow.lib.ListArray object at 0x7f004fc74700>
[
  [
    1
  ],
  [
    2,
    3
  ]
]
  • Primitive array case. Numpy array and python list take a different code path (numpy_to_array.cc vs python_to_arrow.cc):
>>> import pyarrow as pa
>>> pa.array(np.array([1.5, 2.5]), type=pa.int64(), safe=True)
...
ArrowInvalid: Float value 1.5 was truncated converting to int64

vs

>>> import pyarrow as pa
>>> pa.array([1.5, 2.5], type=pa.int64(), safe=True)
<pyarrow.lib.Int64Array object at 0x7f004fc72c40>
[
  1,
  2
]
  • Another example of wrong handling of safe keyword is when using nested data in pandas (an object thus taking the python_to_arrow.cc code path):
>>> import pandas as pd
>>> import pyarrow as pa
>>> int_dataframe = pd.DataFrame({"array": [[1, 2]]})
>>> float_dataframe = pd.DataFrame({"array": [[1.5, 2.3]]})
>>> int_table = pa.Table.from_pandas(int_dataframe)
>>> table = pa.Table.from_pandas(float_dataframe, schema=int_table.schema) 
>>> table
pyarrow.Table
array: list<item: int64>
  child 0, item: int64
----
array: [[[1,2]]]

cc @jorisvandenbossche @danepitkin

Component(s)

Python

Activity

  1. dane commented on Jun 21, 2023

    @dane

    @AlenkaF I may not be the Dane you're looking for, but I can confirm this is Python. Best of luck on addressing the bug!

  2. AlenkaF commented on Jun 21, 2023

    @AlenkaF
    MemberAuthor

    @AlenkaF I may not be the Dane you're looking for, but I can confirm this is Python. Best of luck on addressing the bug!

    Oh sorry about that! :)

  3. changed the title [-][Python] Python to array conversion sometimes ignores "safe=False" or is using unsafe instead of safe cast[/-] [+][Python] Python to Arrow array conversion sometimes ignores "safe=False" or is using unsafe instead of safe cast[/+] on Jun 22, 2023
  4. changed the title [-][Python] Python to Arrow array conversion sometimes ignores "safe=False" or is using unsafe instead of safe cast[/-] [+][C++][Python] Python to Arrow array conversion sometimes ignores "safe=False" or is using unsafe instead of safe cast[/+] on Apr 2, 2025
  5. added
    Status: confirmed
    on Nov 10, 2026
  6. RyukR1 commented on Nov 15, 2026

    @RyukR1

    Hi! I'd like to work on this issue.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Component: C++Component: PythonStatus: confirmedType: bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions