arrow icon indicating copy to clipboard operation
arrow copied to clipboard

[Python] Casting an empty dictionary array loses category information

Open stinodego opened this issue 2 years ago • 13 comments

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

stinodego avatar Feb 19 '24 11:02 stinodego

Why do we need to do this? Could you explain your use case?

kou avatar Feb 19 '24 21:02 kou

@pitrou Do you have any opinion for this case?

kou avatar Feb 19 '24 23:02 kou

I agree that the original dictionary should probably be preserved for consistency, if it's not too costly to do so. cc @felipecrv @jorisvandenbossche

pitrou avatar Feb 20 '24 10:02 pitrou

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 avatar Feb 20 '24 10:02 stinodego

@stinodego Do you think you would be able to work on a PR?

pitrou avatar Feb 20 '24 10:02 pitrou

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.

stinodego avatar Feb 20 '24 10:02 stinodego

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?

jorisvandenbossche avatar Feb 20 '24 11:02 jorisvandenbossche

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?).

pitrou avatar Feb 20 '24 12:02 pitrou

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:
  []

jorisvandenbossche avatar Feb 20 '24 13:02 jorisvandenbossche

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:
  []

pitrou avatar Feb 20 '24 13:02 pitrou

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

felipecrv avatar Feb 20 '24 14:02 felipecrv

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']

Wainberg avatar Feb 23 '24 14:02 Wainberg

take

LarryHu0217 avatar Aug 09 '26 22:08 LarryHu0217