Skip to content

[Python] How to avoid overflow error. #12652

Closed

Description

@kato1208

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')

How can I resolve this error?

pyarrow==7.0.0

Activity

  1. changed the title [-]How to avoid overflow error.[/-] [+][Python] How to avoid overflow error.[/+] on Mar 17, 2022
  2. wjones127 commented on Mar 17, 2022

    @wjones127
    Member

    @kato1208 The preferred way to solve overflow is to change your schema to use types that are appropriate for the data. Look at your first example: The "id" value starts as 3046682132, but when converted to Arrow (with safe=False) comes out as -1248285164. I doubt that's what you actually want.

    That id value is just too large to fit into an int32. If you change the schema to use int64 you can run this without any overflow errors and without needing safe=False. And it will give you correct data.

    schema = pa.schema([
        pa.field('name', pa.string()),
        pa.field('id', pa.int64()),
        pa.field("points", pa.list_(pa.int32())),
        pa.field('groups', pa.struct([
            pa.field("group_name", pa.string()),
            pa.field("group_id", pa.int64()),
        ])),
    ])
  3. kato1208 commented on Mar 18, 2022

    @kato1208
    Author

    @wjones127
    Thank you for your comment !
    Unfortunately there are reasons why the schema cannot be changed and we would like to allow for overflow error.
    How can I ignore overflow error?
    We apologize for the inconvenience and thank you in advance for your time.

  4. wjones127 commented on Mar 18, 2022

    @wjones127
    Member

    How can I ignore overflow error?

    Well it sounds like there is an issue where safe isn't being pushed down into nested arrays, so I don't think this is currently possible. You are welcome to file an issue on the Apache Jira to implement that pushdown.

  5. jorisvandenbossche commented on May 19, 2022

    @jorisvandenbossche
    Member

    Indeed it seems the safe keyword is ignored if we are using the python_to_arrow.cc code path (in contrast to the numpy_to_arrow.cc code path). This is similar as a recent issue that was opened with a variant on this (using list arrays): https://issues.apache.org/jira/browse/ARROW-16491

    A simplified example (even without using a nested type):

    # input is a python list -> safe keyword doesn't work
    >>> pa.array([3046682132], pa.int32(), safe=False)
    ...
    ArrowInvalid: Value 3046682132 too large to fit in C integer type
    
    # input is a numpy array -> safe=False has effect
    >>> pa.array(np.array([3046682132]), pa.int32(), safe=False)
    <pyarrow.lib.Int32Array object at 0x7f40e939f040>
    [
      -1248285164
    ]
    

    So going to close this, and add the example at https://issues.apache.org/jira/browse/ARROW-16491

  6. 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

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions