Skip to content
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

GH1037 Remove na_sentinel from factorize methods #1038

Merged
merged 5 commits into from
Nov 22, 2024
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
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)
Loading