Skip to content

BUG: astype not casting values for dictionary dtype correctly #58479

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 29, 2024
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v3.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,7 @@ Numeric

Conversion
^^^^^^^^^^
- Bug in :meth:`DataFrame.astype` not casting ``values`` for Arrow-based dictionary dtype correctly (:issue:`58479`)
- Bug in :meth:`DataFrame.update` bool dtype being converted to object (:issue:`55509`)
- Bug in :meth:`Series.astype` might modify read-only array inplace when casting to a string dtype (:issue:`57212`)
- Bug in :meth:`Series.reindex` not maintaining ``float32`` type when a ``reindex`` introduces a missing value (:issue:`45857`)
Expand Down
2 changes: 2 additions & 0 deletions pandas/core/arrays/arrow/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -525,6 +525,8 @@ def _box_pa_array(
if pa_type is not None and pa_array.type != pa_type:
if pa.types.is_dictionary(pa_type):
pa_array = pa_array.dictionary_encode()
if pa_array.type != pa_type:
pa_array = pa_array.cast(pa_type)
else:
try:
pa_array = pa_array.cast(pa_type)
Expand Down
8 changes: 8 additions & 0 deletions pandas/tests/extension/test_arrow.py
Original file line number Diff line number Diff line change
Expand Up @@ -3498,6 +3498,14 @@ def test_to_numpy_timestamp_to_int():
tm.assert_numpy_array_equal(result, expected)


@pytest.mark.parametrize("arrow_type", [pa.large_string(), pa.string()])
def test_cast_dictionary_different_value_dtype(arrow_type):
df = pd.DataFrame({"a": ["x", "y"]}, dtype="string[pyarrow]")
data_type = ArrowDtype(pa.dictionary(pa.int32(), arrow_type))
result = df.astype({"a": data_type})
assert result.dtypes.iloc[0] == data_type


def test_map_numeric_na_action():
ser = pd.Series([32, 40, None], dtype="int64[pyarrow]")
result = ser.map(lambda x: 42, na_action="ignore")
Expand Down