You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by "Danferno (via GitHub)" <gi...@apache.org> on 2025/12/10 15:16:41 UTC
Re: [I] [Python] Unable to import arrow table to pandas if it has categorical columns with index types of unsigned ints [arrow]
Danferno commented on issue #47022:
URL: https://github.com/apache/arrow/issues/47022#issuecomment-3637584537
The only solution I've found for this so far is catching any pa.lib.ArrowTypeError and then using this manual parquet -> pyarrow -> pandas code that recasts the field type. It might break upon negative values in the indices?
```
def readParquetFileWithUnsignedDictionaryKeys(path, fs=None):
import pyarrow as pa
table = pa.parquet.read_table(path, filesystem=fs)
for field, column in zip(table.schema, table.columns): # fiter = iter(zip(table.schema, table.columns)); field, column = next(fiter)
if pa.types.is_dictionary(field.type):
# Convert unsigned types to their signed variant
index_type = field.type.index_type
if pa.types.is_uint8(index_type):
new_index_type = pa.int8()
elif pa.types.is_uint16(index_type):
new_index_type = pa.int16()
elif pa.types.is_uint32(index_type):
new_index_type = pa.int32()
elif pa.types.is_uint64(index_type):
new_index_type = pa.int64()
else:
new_index_type = index_type
new_type = pa.dictionary(new_index_type, field.type.value_type)
table = table.set_column(table.schema.get_field_index(field.name), field.name, column.cast(new_type))
return table.to_pandas()
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org