[Python] Casting an empty dictionary array loses category information
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
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
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
Why do we need to do this? Could you explain your use case?
@pitrou Do you have any opinion for this case?
I agree that the original dictionary should probably be preserved for consistency, if it's not too costly to do so. cc @felipecrv @jorisvandenbossche
Why do we need to do this? Could you explain your use case?
For Polars, we use pyarrow for the converting our data to pandas. Our Enum data type is similar to a pyarrow Dictionary type with a set number of categories. This converts into a pandas categorical type which also has a set number of categories.
In this conversion, we have to cast the index type from UInt32 (our default) to Int64 since pandas does not support unsigned indices. If the data is empty, this cast now loses the category information, and data type after the conversion is wrong.
See the original issue opened in our repo: https://github.com/pola-rs/polars/issues/14582
@stinodego Do you think you would be able to work on a PR?
I'm not familiar with the Arrow code base, but I suppose I could give it a shot.
EDIT: If the implementation here is in C++ then it's probably better if someone else picks this up - I have no experience programming in that language.
Agreed that we should be consistent for empty arrays.
Haven't tested this, but it might be caused by shortcutting calling the actual kernel for empty input here:
https://github.com/apache/arrow/blob/4dc3d04ae84d97d02443c0cef555a46535925c2b/cpp/src/arrow/compute/exec.cc#L781-L795
This creates a generic array of length 0 of the output type, but so in case of a dictionary output type, it will never have an actual dictionary attached to that return value.
We might want to generally skip that fast-path in case output_type_ is a dictionary type?
We might want to generally skip that fast-path in case output_type_ is a dictionary type?
Ideally we would also do that for nested types that have a dictionary field somewhere.
But we can start with your idea and see what happens (not all scalar kernels might like zero-length inputs?).
I am not sure if we actually have many scalar kernels apart from cast that would work for dictionary ànd return dictionary?
Checking some non-scalar (vector) kernels, those seem to preserve the dictionary correctly for empty input (using arr from the top post):
In [8]: pc.filter(arr[:0], pa.array([], pa.bool_()))
Out[8]:
<pyarrow.lib.DictionaryArray object at 0x7f2f399f2ff0>
-- dictionary:
[
"foo",
"bar"
]
-- indices:
[]
In [9]: pc.take(arr[:0], pa.array([], pa.int64()))
Out[9]:
<pyarrow.lib.DictionaryArray object at 0x7f2f3b048040>
-- dictionary:
[
"foo",
"bar"
]
-- indices:
[]
We do:
>>> a = pa.array([1,2,3,1]).dictionary_encode()
>>> pc.coalesce(a[:1])
<pyarrow.lib.DictionaryArray object at 0x7f0ef4948ba0>
-- dictionary:
[
1,
2,
3
]
-- indices:
[
0
]
>>> pc.coalesce(a[:0])
<pyarrow.lib.DictionaryArray object at 0x7f0ef49d70d0>
-- dictionary:
[]
-- indices:
[]
My take here is that dictionaries are for compression and might be aggressively further-compressed by some operations [1], but cast is special and should preserve the original dictionary because the goal of a cast is to change only the encoding of the data with as little as possible impact on its semantic properties.
I would keep the fast path and make only cast kernels the exception. I don't know exactly how without looking at the code.
My reasoning: we don't want code accidentally relying on a guarantee that is stronger than "the dictionary contains the values that are referenced at least once". The path to full determinism here is probably being more aggressive in removing values, than preserving them.
[1] many operations act on the array[0..length) values and might gather only the dictionary values that have references to them
Workaround in the meantime:
import pyarrow as pa
arr = pa.array(["foo", "bar"], pa.dictionary(pa.int32(), pa.string()))
arr_empty = arr[:0]
arr_empty_cast = pa.DictionaryArray.from_arrays(pa.array([], type=pa.int64()), arr_empty.dictionary)
print(arr_empty_cast.dictionary) # ['foo', 'bar']
take