Skip to content

Commit 2a1f226

Browse files
committed
Merge branch 'main' of https://github.com/pandas-dev/pandas into bug#60695
2 parents 1cf61a6 + 10762c6 commit 2a1f226

File tree

5 files changed

+25
-2
lines changed

5 files changed

+25
-2
lines changed

doc/source/development/contributing_codebase.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ In some cases you may be tempted to use ``cast`` from the typing module when you
198198
obj = cast(str, obj) # Mypy complains without this!
199199
return obj.upper()
200200
201-
The limitation here is that while a human can reasonably understand that ``is_number`` would catch the ``int`` and ``float`` types mypy cannot make that same inference just yet (see `mypy #5206 <https://github.com/python/mypy/issues/5206>`_. While the above works, the use of ``cast`` is **strongly discouraged**. Where applicable a refactor of the code to appease static analysis is preferable
201+
The limitation here is that while a human can reasonably understand that ``is_number`` would catch the ``int`` and ``float`` types mypy cannot make that same inference just yet (see `mypy #5206 <https://github.com/python/mypy/issues/5206>`_). While the above works, the use of ``cast`` is **strongly discouraged**. Where applicable a refactor of the code to appease static analysis is preferable
202202

203203
.. code-block:: python
204204

doc/source/whatsnew/v3.0.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -790,6 +790,7 @@ ExtensionArray
790790
^^^^^^^^^^^^^^
791791
- Bug in :class:`Categorical` when constructing with an :class:`Index` with :class:`ArrowDtype` (:issue:`60563`)
792792
- Bug in :meth:`.arrays.ArrowExtensionArray.__setitem__` which caused wrong behavior when using an integer array with repeated values as a key (:issue:`58530`)
793+
- Bug in :meth:`ArrowExtensionArray.factorize` where NA values were dropped when input was dictionary-encoded even when dropna was set to False(:issue:`60567`)
793794
- Bug in :meth:`api.types.is_datetime64_any_dtype` where a custom :class:`ExtensionDtype` would return ``False`` for array-likes (:issue:`57055`)
794795
- 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`)
795796
- 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`)

pandas/core/arrays/arrow/array.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1208,7 +1208,12 @@ def factorize(
12081208
data = data.cast(pa.int64())
12091209

12101210
if pa.types.is_dictionary(data.type):
1211-
encoded = data
1211+
if null_encoding == "encode":
1212+
# dictionary encode does nothing if an already encoded array is given
1213+
data = data.cast(data.type.value_type)
1214+
encoded = data.dictionary_encode(null_encoding=null_encoding)
1215+
else:
1216+
encoded = data
12121217
else:
12131218
encoded = data.dictionary_encode(null_encoding=null_encoding)
12141219
if encoded.length() == 0:

pandas/core/generic.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6267,6 +6267,11 @@ def astype(
62676267
"""
62686268
Cast a pandas object to a specified dtype ``dtype``.
62696269
6270+
This method allows the conversion of the data types of pandas objects,
6271+
including DataFrames and Series, to the specified dtype. It supports casting
6272+
entire objects to a single data type or applying different data types to
6273+
individual columns using a mapping.
6274+
62706275
Parameters
62716276
----------
62726277
dtype : str, data type, Series or Mapping of column name -> data type

pandas/tests/extension/test_arrow.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3329,6 +3329,18 @@ def test_factorize_chunked_dictionary():
33293329
tm.assert_index_equal(res_uniques, exp_uniques)
33303330

33313331

3332+
def test_factorize_dictionary_with_na():
3333+
# GH#60567
3334+
arr = pd.array(
3335+
["a1", pd.NA], dtype=ArrowDtype(pa.dictionary(pa.int32(), pa.utf8()))
3336+
)
3337+
indices, uniques = arr.factorize(use_na_sentinel=False)
3338+
expected_indices = np.array([0, 1], dtype=np.intp)
3339+
expected_uniques = pd.array(["a1", None], dtype=ArrowDtype(pa.string()))
3340+
tm.assert_numpy_array_equal(indices, expected_indices)
3341+
tm.assert_extension_array_equal(uniques, expected_uniques)
3342+
3343+
33323344
def test_dictionary_astype_categorical():
33333345
# GH#56672
33343346
arrs = [

0 commit comments

Comments
 (0)