Skip to content

[Python] Casting an empty dictionary array loses category information #40128

Description

@stinodego

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

Casting a DictionaryArray to another dictionary type will preserve the original dictionary, unless the array is empty.

Minimal example

importpyarrowaspaarr=pa.array(["foo", "bar"], pa.dictionary(pa.int32(), pa.string()))
arr_empty=arr[:0]
arr_empty_cast=arr_empty.cast(pa.dictionary(pa.int64(), pa.string()))
print(arr_empty_cast.dictionary) # []

Additional examples

importpyarrowaspaarr=pa.array(["foo", "bar"], pa.dictionary(pa.int32(), pa.string()))
print(arr.dictionary) # ["foo", "bar"]arr_short_cast=arr[:1].cast(pa.dictionary(pa.int64(), pa.string()))
print(arr_short_cast.dictionary) # ["foo", "bar"]arr_empty=arr[:0]
print(arr_empty.dictionary) # ["foo", "bar"]arr_empty_cast=arr_empty.cast(pa.dictionary(pa.int64(), pa.string()))
print(arr_empty_cast.dictionary) # []

Expected output

The original categories should be preserved when changing the index type, even if the array is empty.

Component(s)

Python

Activity

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

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions