@@ -566,17 +566,6 @@ def factorize_array(
566
566
567
567
hash_klass , values = _get_hashtable_algo (values )
568
568
569
- # factorize can now handle differentiating various types of null values.
570
- # However, for backwards compatibility we only use the null for the
571
- # provided dtype. This may be revisited in the future, see GH#48476.
572
- null_mask = isna (values )
573
- if null_mask .any ():
574
- na_value = na_value_for_dtype (values .dtype , compat = False )
575
- # Don't modify (potentially user-provided) array
576
- # error: No overload variant of "where" matches argument types "Any", "object",
577
- # "ndarray[Any, Any]"
578
- values = np .where (null_mask , na_value , values ) # type: ignore[call-overload]
579
-
580
569
table = hash_klass (size_hint or len (values ))
581
570
uniques , codes = table .factorize (
582
571
values ,
@@ -810,6 +799,18 @@ def factorize(
810
799
na_sentinel_arg = None
811
800
else :
812
801
na_sentinel_arg = na_sentinel
802
+
803
+ if not dropna and not sort and is_object_dtype (values ):
804
+ # factorize can now handle differentiating various types of null values.
805
+ # These can only occur when the array has object dtype.
806
+ # However, for backwards compatibility we only use the null for the
807
+ # provided dtype. This may be revisited in the future, see GH#48476.
808
+ null_mask = isna (values )
809
+ if null_mask .any ():
810
+ na_value = na_value_for_dtype (values .dtype , compat = False )
811
+ # Don't modify (potentially user-provided) array
812
+ values = np .where (null_mask , na_value , values )
813
+
813
814
codes , uniques = factorize_array (
814
815
values ,
815
816
na_sentinel = na_sentinel_arg ,
0 commit comments