Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions python/pyspark/sql/pandas/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,8 +226,7 @@ def create_array(s, t):
s = _check_series_convert_timestamps_internal(s, self._timezone)
elif t is not None and pa.types.is_map(t):
s = _convert_dict_to_map_items(s)
elif is_categorical_dtype(s.dtype):
# Note: This can be removed once minimum pyarrow version is >= 0.16.1
elif t is None and is_categorical_dtype(s.dtype):
s = s.astype(s.dtypes.categories.dtype)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@BryanCutler Seems like if t is None, pa.Array.from_pandas(s, mask=mask, type=t, safe=self._safecheck) handles the categorical type as integer (tinyint as its code) instead of some type of s.dtypes.categories.dtype. Is that an expected behavior?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah, it will be dictionary<values=string, indices=int8, ordered=0> if we don't specify type.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so we can't remove it if t is None. or we need to support dictionary type.

Copy link
Member

@BryanCutler BryanCutler May 4, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From what I remember, for versions >= 0.16.1 pyarrow would automatically cast a categorical type to what the requested type of s was and the result was the correct type without this elif block. This was the comment where it was brought up #26585 (comment)

I remember testing it out locally without this, but that was quite a while ago so something might have changed. That could have also only been the case for when t is not None. So it makes sense to keep it for that case.

try:
array = pa.Array.from_pandas(s, mask=mask, type=t, safe=self._safecheck)
Expand Down