apache / apache/arrow

[Python] Casting an empty dictionary array loses category information

Open
#40,128 13 comments 0 reactions 1 assignee Claimed by @LarryHu0217 View on GitHub
Component: C++ Component: Python good-second-issue Type: bug
Dominant language
C++
Stars
17.1k
Forks
4.3k
Avg merge
3d 13h
Merged PRs (30d)
88

Description

### 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

```python
import pyarrow as pa

arr = 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

```python
import pyarrow as pa

arr = 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

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.