Skip to content

Commit

Permalink
Merge branch 'main' into bug#60723
Browse files Browse the repository at this point in the history
  • Loading branch information
Anurag-Varma authored Feb 25, 2025
2 parents 1750d6a + 840cb1f commit 952e292
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
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 @@ -791,6 +791,7 @@ ExtensionArray
^^^^^^^^^^^^^^
- Bug in :class:`Categorical` when constructing with an :class:`Index` with :class:`ArrowDtype` (:issue:`60563`)
- Bug in :meth:`.arrays.ArrowExtensionArray.__setitem__` which caused wrong behavior when using an integer array with repeated values as a key (:issue:`58530`)
- Bug in :meth:`ArrowExtensionArray.factorize` where NA values were dropped when input was dictionary-encoded even when dropna was set to False(:issue:`60567`)
- Bug in :meth:`api.types.is_datetime64_any_dtype` where a custom :class:`ExtensionDtype` would return ``False`` for array-likes (:issue:`57055`)
- Bug in comparison between object with :class:`ArrowDtype` and incompatible-dtyped (e.g. string vs bool) incorrectly raising instead of returning all-``False`` (for ``==``) or all-``True`` (for ``!=``) (:issue:`59505`)
- Bug in constructing pandas data structures when passing into ``dtype`` a string of the type followed by ``[pyarrow]`` while PyArrow is not installed would raise ``NameError`` rather than ``ImportError`` (:issue:`57928`)
Expand Down
7 changes: 6 additions & 1 deletion pandas/core/arrays/arrow/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -1208,7 +1208,12 @@ def factorize(
data = data.cast(pa.int64())

if pa.types.is_dictionary(data.type):
encoded = data
if null_encoding == "encode":
# dictionary encode does nothing if an already encoded array is given
data = data.cast(data.type.value_type)
encoded = data.dictionary_encode(null_encoding=null_encoding)
else:
encoded = data
else:
encoded = data.dictionary_encode(null_encoding=null_encoding)
if encoded.length() == 0:
Expand Down
12 changes: 12 additions & 0 deletions pandas/tests/extension/test_arrow.py
Original file line number Diff line number Diff line change
Expand Up @@ -3329,6 +3329,18 @@ def test_factorize_chunked_dictionary():
tm.assert_index_equal(res_uniques, exp_uniques)


def test_factorize_dictionary_with_na():
# GH#60567
arr = pd.array(
["a1", pd.NA], dtype=ArrowDtype(pa.dictionary(pa.int32(), pa.utf8()))
)
indices, uniques = arr.factorize(use_na_sentinel=False)
expected_indices = np.array([0, 1], dtype=np.intp)
expected_uniques = pd.array(["a1", None], dtype=ArrowDtype(pa.string()))
tm.assert_numpy_array_equal(indices, expected_indices)
tm.assert_extension_array_equal(uniques, expected_uniques)


def test_dictionary_astype_categorical():
# GH#56672
arrs = [
Expand Down

0 comments on commit 952e292

Please sign in to comment.