Skip to content

[Python] Can't ignore the overflow error. #31402

Closed as not planned
Closed as not planned

Description

@asfimport

I know that the argument safe=False in pa.Table.from_pandas ignores overflow errors, but it does not ignore overflow in list or in struct.

It works.

import pyarrow as pa
import pyarrow.parquet as pq
import pandas as pd
import json
test_json = [
    {
        "name": "taro",
        "id": 3046682132,
        "points": [2, 2, 2],
        "groups": {
            "group_name": "baseball", 
            "group_id": 1234
        }
    },
    { 
        "name": "taro",
        "id": 1234, 
    }
]
schema = pa.schema([
    pa.field('name', pa.string()),
    pa.field('id', pa.int32()),
    pa.field("points", pa.list_(pa.int32())),
    pa.field('groups', pa.struct([
        pa.field("group_name", pa.string()),
        pa.field("group_id", pa.int32()),
    ])),
])
writer = pq.ParquetWriter('test_schema.parquet', schema=schema)
df = pd.DataFrame(test_json)
table = pa.Table.from_pandas(df, schema=schema, safe=False)
writer.write_table(table)
writer.close()table = pq.read_table("test_schema.parquet")
print(table) 
name: [["taro","taro"]] id: [[-1248285164,1234]] points: [[[2,2,2],null]] groups: [ – is_valid: [ true, false ] – child 0 type: string [ "baseball", null ] – child 1 type: int32 [ 1234, null ]]

However, the following two do not work.

 

test_json = [
    {
        "name": "taro",
        "id": 2,
        "points": [2, 3046682132, 2],
        "groups": {
            "group_name": "baseball", 
            "group_id": 1234
        }
    },
    { 
        "name": "taro",
        "id": 1234, 
    }
]
Traceback (most recent call last):
File "test_pyarrow.py", line 35, in <module>
table = pa.Table.from_pandas(df, schema=schema, safe=False)
File "pyarrow/table.pxi", line 1782, in pyarrow.lib.Table.from_pandas
File "/home/s0108403058/.pyenv/versions/3.8.0/lib/python3.8/site-packages/pyarrow/pandas_compat.py", line 594, in dataframe_to_arrays
arrays = [convert_column(c, f)
File "/home/s0108403058/.pyenv/versions/3.8.0/lib/python3.8/site-packages/pyarrow/pandas_compat.py", line 594, in <listcomp>
arrays = [convert_column(c, f)
File "/home/s0108403058/.pyenv/versions/3.8.0/lib/python3.8/site-packages/pyarrow/pandas_compat.py", line 581, in convert_column
raise e
File "/home/s0108403058/.pyenv/versions/3.8.0/lib/python3.8/site-packages/pyarrow/pandas_compat.py", line 575, in convert_column
result = pa.array(col, type=type_, from_pandas=True, safe=safe)
File "pyarrow/array.pxi", line 312, in pyarrow.lib.array
File "pyarrow/array.pxi", line 83, in pyarrow.lib._ndarray_to_array
File "pyarrow/error.pxi", line 99, in pyarrow.lib.check_status
pyarrow.lib.ArrowInvalid: ('Value 3046682132 too large to fit in C integer type', 'Conversion failed for column points with type object') 
test_json = [
    {
        "name": "taro",
        "id": 2,
        "points": [2, 2, 2],
        "groups": {
            "group_name": "baseball", 
            "group_id": 3046682132
        }
    },
    { 
        "name": "taro",
        "id": 1234, 
    }
] 
Traceback (most recent call last):
File "test_pyarrow.py", line 35, in <module>
table = pa.Table.from_pandas(df, schema=schema, safe=False)
File "pyarrow/table.pxi", line 1782, in pyarrow.lib.Table.from_pandas
File "/home/s0108403058/.pyenv/versions/3.8.0/lib/python3.8/site-packages/pyarrow/pandas_compat.py", line 594, in dataframe_to_arrays
arrays = [convert_column(c, f)
File "/home/s0108403058/.pyenv/versions/3.8.0/lib/python3.8/site-packages/pyarrow/pandas_compat.py", line 594, in <listcomp>
arrays = [convert_column(c, f)
File "/home/s0108403058/.pyenv/versions/3.8.0/lib/python3.8/site-packages/pyarrow/pandas_compat.py", line 581, in convert_column
raise e
File "/home/s0108403058/.pyenv/versions/3.8.0/lib/python3.8/site-packages/pyarrow/pandas_compat.py", line 575, in convert_column
result = pa.array(col, type=type_, from_pandas=True, safe=safe)
File "pyarrow/array.pxi", line 312, in pyarrow.lib.array
File "pyarrow/array.pxi", line 83, in pyarrow.lib._ndarray_to_array
File "pyarrow/error.pxi", line 99, in pyarrow.lib.check_status
pyarrow.lib.ArrowInvalid: ('Value 3046682132 too large to fit in C integer type', 'Conversion failed for column groups with type object') 

Could you please fix this bug?

pyarrow==7.0.0

Reporter: taichi kato

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

Activity

  1. asfimport commented on Mar 21, 2022

    @asfimport
    Author

    Alessandro Molina / @amol-:
    I think that the problem might be that we are not propagating the conversion options. At least for structs that seems to be happening here:

    sub_converters.emplace_back(pool_, sub_array, nullptr /* mask */, field->type(),
    from_pandas_);
    the cast_options argument is not provided (
    NumPyConverter(MemoryPool* pool, PyObject* arr, PyObject* mo,
    const std::shared_ptr<DataType>& type, bool from_pandas,
    const compute::CastOptions& cast_options = compute::CastOptions())
    )

     

    I'll need to investigate more were the ListType behaviour is coming from instead as that doesn't seem to be visitable by the NumPyConverter

  2. asfimport commented on Jun 30, 2022

    @asfimport
    Author

    Raúl Cumplido / @raulcd:
    I have tried to investigate the issue but I'll need a lot of time to understand our numpy_to_arrow.cc workflow. I am just adding a small diff here with some minor unit tests to reproduce the issue in case it helps someone:

    diff --git a/python/pyarrow/tests/test_pandas.py b/python/pyarrow/tests/test_pandas.py
    index 215bf2f..26108f5 100644
    --- a/python/pyarrow/tests/test_pandas.py
    +++ b/python/pyarrow/tests/test_pandas.py
    @@ -697,6 +697,29 @@ class TestConvertPrimitiveTypes:
             assert table[0].to_pylist() == [1, 2, None]
             tm.assert_frame_equal(df, table.to_pandas())
     
    +    def test_int_overflow(self):
    +        # ARROW-15977
    +        df = pd.DataFrame([{"a": 3046682132}])
    +        schema = pa.schema([pa.field("a", pa.int32(), nullable=True)])
    +        table = pa.Table.from_pandas(df, schema=schema, safe=False)
    +        assert table[0].to_pylist() == [-1248285164]
    +
    +    def test_list_int_overflow(self):
    +        # ARROW-15977
    +        df = pd.DataFrame([{"a": [1, 3046682132, 0]}])
    +        schema = pa.schema([pa.field("a", pa.list_(pa.int32()), nullable=True)])
    +        table = pa.Table.from_pandas(df, schema=schema, safe=False)
    +        # TODO Fix assert
    +        assert table[0].to_pylist() == [-1248285164]
    +
    +    def test_struct_int_overflow(self):
    +        # ARROW-15977
    +        df = pd.DataFrame([{"a": {"b": 3046682132}}])
    +        schema = pa.schema([pa.field("a", pa.struct([pa.field("b", pa.int32())]), nullable=True)])
    +        table = pa.Table.from_pandas(df, schema=schema, safe=False)
    +        # TODO Fix assert
    +        assert table[0].to_pylist() == [-1248285164]
    +
  3. AlenkaF commented on Jun 21, 2023

    @AlenkaF
    Member

    Duplicate of #12652

  4. AlenkaF commented on Jun 21, 2023

    @AlenkaF
    Member

    We created an "umbrella" issue that includes all the issues opened and connected to the Python to array conversion following the codepath in python_to_arrow.cc. See: #36202.

    As this example includes JSON with nested data therefore taking the code path of the python-sequence conversion is python_to_arrow.cc which seems to ignore safe=False and does not overflow by default.

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