arrow icon indicating copy to clipboard operation
arrow copied to clipboard

[Python] PyArrow Table to Pandas int8 conversion issue

Open adampinky85 opened this issue 2 years ago • 3 comments

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

Hi team,

We extensively use Arrow / Parquet files for data analysis with Pandas, it's excellent! We've found an issue that occurs converting between PyArrow Tables and Pandas Dataframe.

Due to large size of our dataset we write the parquet files using minimal field data types e.g categories and specifically we're using an int8 in field_6. This field is unfortunately nullable which is likely the cause of the issue.

The issue is that the int8 field is converted to Pandas as a float64. In Pandas this can be fixed using the dtype_backend="numpy_nullable" argument and it converts to an Int8.

Is there any equivalent mechanism, using pyarrow.parquet.read_pandas to retrieve the field with an Int8 or equivalent? I assume the conversion to float64 is forced due to the field being nullable.

Many thanks!

PyArrow Parquet Metadata:

<pyarrow._parquet.FileMetaData object at 0x7f9ca8d65030>
  created_by: parquet-cpp-arrow version 15.0.2
  num_columns: 11
  num_rows: 35734802
  num_row_groups: 35
  format_version: 2.6
  serialized_size: 41957

PyArrow Parquet Schema:

<pyarrow._parquet.ParquetSchema object at 0x7f9ca8d55f80>
required group field_id=-1 schema {
  optional int64 field_id=-1 timestamp (Timestamp(isAdjustedToUTC=false, timeUnit=milliseconds, is_from_converted_type=false, force_set_converted_type=false));
  optional binary field_id=-1 field_1 (String);
  optional binary field_id=-1 field_2 (String);
  optional binary field_id=-1 field_3 (String);
  optional binary field_id=-1 field_4 (String);
  optional binary field_id=-1 field_5 (String);
  optional int32 field_id=-1 field_6 (Int(bitWidth=8, isSigned=true));       <------ note Int8
  optional double field_id=-1 field_7;
  optional double field_id=-1 field_8;
  optional double field_id=-1 field_9;
  optional double field_id=-1 field_10;
}
  1. Pandas read parquet:
df = pd.read_parquet(filename)
timestamp datetime64[ms]
field_1        category
field_2        category
field_3        category
field_4        category
field_5        category
field_6        float64          <---- note float64
field_7        float64
field_8        float64
field_9        float64
field_10       float64
  1. Pandas read parquet + numpy_nullable:
df = pd.read_parquet(filename, dtype_backend="numpy_nullable")
timestamp datetime64[ms]
field_1        category
field_2        category
field_3        category
field_4        category
field_5        category
field_6        Int8           <---- note Int8
field_7        Float64
field_8        Float64
field_9        Float64
field_10       Float64
  1. PyArrow parquet read table:
table = pyarrow.parquet.read_pandas(target_path, filesystem=s3_client)
df = table.to_pandas()
timestamp datetime64[ms]
field_1        category
field_2        category
field_3        category
field_4        category
field_5        category
field_6        float64          <---- note float64
field_7        float64
field_8        float64
field_9        float64
field_10       float64

Component(s)

Python

adampinky85 avatar Mar 27 '24 06:03 adampinky85

I believe that this is working as intended as per the note in the docs https://arrow.apache.org/docs/dev/python/pandas.html#arrow-pandas-conversion and should be a feature request rather than a bug. That being said, this also affects me!

SecretlyNotARobot avatar Jul 23 '24 18:07 SecretlyNotARobot

cc @jorisvandenbossche

pitrou avatar Sep 18 '24 14:09 pitrou

Indeed, this is expected. At the moment, pandas uses numpy integer dtypes by default, and those can't contain any missing values. Thus, when there are missing values we convert to float64 instead.

The current workarounds possible are the ones you already mentioned in the top-post, i.e. ensure you use the (opt-in) nullable Int8 dtype in pandas. This can be done from the pandas side using the dtype_backend keyword (as shown in the top post). If you want to achieve this from pyarrow through a Table.to_pandas() call, you can specify types_mapper keyword as documented in https://arrow.apache.org/docs/dev/python/pandas.html#nullable-types

jorisvandenbossche avatar Sep 20 '24 17:09 jorisvandenbossche