Skip to content

Commit

Permalink
GH1037 Remove na_sentinel from factorize methods (#1038)
Browse files Browse the repository at this point in the history
* GH1037 Remove na_sentinel from factorize methods

* GH1037 PR feedback

* GH1037 PR feedback

* GH1037 PR feedback
  • Loading branch information
loicdiridollou authored Nov 22, 2024
1 parent 70ee340 commit 47fc9b6
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 7 deletions.
4 changes: 0 additions & 4 deletions pandas-stubs/core/algorithms.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -52,17 +52,13 @@ def factorize(
def factorize(
values: Index | Series,
sort: bool = ...,
# Not actually positional-only, used to handle deprecations in 1.5.0
*,
use_na_sentinel: bool = ...,
size_hint: int | None = ...,
) -> tuple[np.ndarray, Index]: ...
@overload
def factorize(
values: Categorical,
sort: bool = ...,
# Not actually positional-only, used to handle deprecations in 1.5.0
*,
use_na_sentinel: bool = ...,
size_hint: int | None = ...,
) -> tuple[np.ndarray, Categorical]: ...
Expand Down
3 changes: 1 addition & 2 deletions pandas-stubs/core/arrays/base.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ class ExtensionArray:
def shift(self, periods: int = ..., fill_value: object = ...) -> Self: ...
def unique(self): ...
def searchsorted(self, value, side: str = ..., sorter=...): ...
# TODO: remove keyword-only when pandas removed na_sentinel
def factorize(self, *, use_na_sentinel: bool = ...) -> tuple[np.ndarray, Self]: ...
def factorize(self, use_na_sentinel: bool = ...) -> tuple[np.ndarray, Self]: ...
def repeat(self, repeats, axis=...): ...
def take(
self,
Expand Down
2 changes: 1 addition & 1 deletion pandas-stubs/core/base.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ class IndexOpsMixin(OpsMixin, Generic[S1]):
@property
def is_monotonic_increasing(self) -> bool: ...
def factorize(
self, sort: bool = ...
self, sort: bool = ..., use_na_sentinel: bool = ...
) -> tuple[np.ndarray, np.ndarray | Index | Categorical]: ...
def searchsorted(
self, value, side: Literal["left", "right"] = ..., sorter=...
Expand Down
15 changes: 15 additions & 0 deletions tests/test_indexes.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import numpy as np
from numpy import typing as npt
import pandas as pd
from pandas.core.arrays.categorical import Categorical
from pandas.core.indexes.base import Index
from typing_extensions import (
Never,
assert_type,
Expand Down Expand Up @@ -1160,3 +1162,16 @@ def test_value_counts() -> None:
pd.Series,
float,
)


def test_index_factorize() -> None:
"""Test Index.factorize method."""
codes, idx_uniques = pd.Index(["b", "b", "a", "c", "b"]).factorize()
check(assert_type(codes, np.ndarray), np.ndarray)
check(assert_type(idx_uniques, np.ndarray | Index | Categorical), pd.Index)

codes, idx_uniques = pd.Index(["b", "b", "a", "c", "b"]).factorize(
use_na_sentinel=False
)
check(assert_type(codes, np.ndarray), np.ndarray)
check(assert_type(idx_uniques, np.ndarray | Index | Categorical), pd.Index)

0 comments on commit 47fc9b6

Please sign in to comment.