Skip to content

BUG: PandasArray[uint].factorize #46295

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 3 commits into from
Mar 12, 2022
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
3 changes: 3 additions & 0 deletions pandas/core/arrays/_mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,9 @@ def _from_factorized(cls, values, original):
def _values_for_argsort(self) -> np.ndarray:
return self._ndarray

def _values_for_factorize(self):
return self._ndarray, self._internal_fill_value

# Signature of "argmin" incompatible with supertype "ExtensionArray"
def argmin(self, axis: int = 0, skipna: bool = True): # type: ignore[override]
# override base class by adding axis keyword
Expand Down
3 changes: 0 additions & 3 deletions pandas/core/arrays/categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -2298,9 +2298,6 @@ def unique(self):
unique_codes = unique1d(self.codes)
return self._from_backing_data(unique_codes)

def _values_for_factorize(self):
return self._ndarray, -1

def _cast_quantile_result(self, res_values: np.ndarray) -> np.ndarray:
# make sure we have correct itemsize for resulting codes
res_values = coerce_indexer_dtype(res_values, self.dtype.categories)
Expand Down
3 changes: 0 additions & 3 deletions pandas/core/arrays/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -549,9 +549,6 @@ def copy(self: DatetimeLikeArrayT, order="C") -> DatetimeLikeArrayT:
new_obj._freq = self.freq
return new_obj

def _values_for_factorize(self):
return self._ndarray, self._internal_fill_value

# ------------------------------------------------------------------
# Validation Methods
# TODO: try to de-duplicate these, ensure identical behavior
Expand Down
8 changes: 6 additions & 2 deletions pandas/core/arrays/numpy_.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,12 @@ def _validate_scalar(self, fill_value):
fill_value = self.dtype.na_value
return fill_value

def _values_for_factorize(self) -> tuple[np.ndarray, int]:
return self._ndarray, -1
def _values_for_factorize(self) -> tuple[np.ndarray, float | None]:
if self.dtype.kind in ["i", "u", "b"]:
fv = None
else:
fv = np.nan
return self._ndarray, fv

# ------------------------------------------------------------------------
# Reductions
Expand Down
13 changes: 13 additions & 0 deletions pandas/tests/arrays/numpy_/test_numpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,3 +309,16 @@ def test_quantile_empty(dtype):
result = arr._quantile(idx, interpolation="linear")
expected = PandasArray(np.array([np.nan, np.nan]))
tm.assert_extension_array_equal(result, expected)


def test_factorize_unsigned():
# don't raise when calling factorize on unsigned int PandasArray
arr = np.array([1, 2, 3], dtype=np.uint64)
obj = PandasArray(arr)

res_codes, res_unique = obj.factorize()
exp_codes, exp_unique = pd.factorize(arr)

tm.assert_numpy_array_equal(res_codes, exp_codes)

tm.assert_extension_array_equal(res_unique, PandasArray(exp_unique))