Skip to content

Commit 340346c

Browse files
authored
Backport PR pandas-dev#53232 on branch 2.0.x (BUG: sort_values raising for dictionary arrow dtype) (pandas-dev#53241)
BUG: sort_values raising for dictionary arrow dtype (pandas-dev#53232) (cherry picked from commit e1b657a)
1 parent 7a28ced commit 340346c

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-2
lines changed

doc/source/whatsnew/v2.0.2.rst

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ Bug fixes
3030
- Bug in :func:`api.interchange.from_dataframe` was unnecessarily raising on bitmasks (:issue:`49888`)
3131
- Bug in :func:`merge` when merging on datetime columns on different resolutions (:issue:`53200`)
3232
- Bug in :meth:`DataFrame.convert_dtypes` ignores ``convert_*`` keywords when set to False ``dtype_backend="pyarrow"`` (:issue:`52872`)
33+
- Bug in :meth:`DataFrame.sort_values` raising for PyArrow ``dictionary`` dtype (:issue:`53232`)
3334
- Bug in :meth:`Series.describe` treating pyarrow-backed timestamps and timedeltas as categorical data (:issue:`53001`)
3435
- Bug in :meth:`Series.rename` not making a lazy copy when Copy-on-Write is enabled when a scalar is passed to it (:issue:`52450`)
3536
- Bug in :meth:`pd.array` raising for ``NumPy`` array and ``pa.large_string`` or ``pa.large_binary`` (:issue:`52590`)

pandas/core/arrays/arrow/array.py

+8-2
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,10 @@ def _from_sequence(cls, scalars, *, dtype: Dtype | None = None, copy: bool = Fal
269269
# GH50430: let pyarrow infer type, then cast
270270
scalars = pa.array(scalars, from_pandas=True)
271271
if pa_dtype:
272-
scalars = scalars.cast(pa_dtype)
272+
if pa.types.is_dictionary(pa_dtype):
273+
scalars = scalars.dictionary_encode()
274+
else:
275+
scalars = scalars.cast(pa_dtype)
273276
arr = cls(scalars)
274277
if pa.types.is_duration(scalars.type) and scalars.null_count > 0:
275278
# GH52843: upstream bug for duration types when originally
@@ -868,7 +871,10 @@ def factorize(
868871
else:
869872
data = self._data
870873

871-
encoded = data.dictionary_encode(null_encoding=null_encoding)
874+
if pa.types.is_dictionary(data.type):
875+
encoded = data
876+
else:
877+
encoded = data.dictionary_encode(null_encoding=null_encoding)
872878
if encoded.length() == 0:
873879
indices = np.array([], dtype=np.intp)
874880
uniques = type(self)(pa.chunked_array([], type=encoded.type.value_type))

pandas/tests/extension/test_arrow.py

+14
Original file line numberDiff line numberDiff line change
@@ -1831,6 +1831,20 @@ def test_searchsorted_with_na_raises(data_for_sorting, as_series):
18311831
arr.searchsorted(b)
18321832

18331833

1834+
def test_sort_values_dictionary():
1835+
df = pd.DataFrame(
1836+
{
1837+
"a": pd.Series(
1838+
["x", "y"], dtype=ArrowDtype(pa.dictionary(pa.int32(), pa.string()))
1839+
),
1840+
"b": [1, 2],
1841+
},
1842+
)
1843+
expected = df.copy()
1844+
result = df.sort_values(by=["a", "b"])
1845+
tm.assert_frame_equal(result, expected)
1846+
1847+
18341848
@pytest.mark.parametrize("pat", ["abc", "a[a-z]{2}"])
18351849
def test_str_count(pat):
18361850
ser = pd.Series(["abc", None], dtype=ArrowDtype(pa.string()))

0 commit comments

Comments
 (0)