Skip to content

Commit

Permalink
Type tolerance in reindex_like, remove unused function (#1107)
Browse files Browse the repository at this point in the history
* Type `tolerance` in `reindex_like`, remove unused function

* revert unrelated, use assert_type

* another assert_type

* one more

* 🔥 remove generic reindex_like
  • Loading branch information
MarcoGorelli authored Feb 7, 2025
1 parent 54b15c3 commit a32c467
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 32 deletions.
16 changes: 0 additions & 16 deletions pandas-stubs/core/construction.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,6 @@ from collections.abc import Sequence

import numpy as np
from pandas.core.arrays.base import ExtensionArray
from pandas.core.indexes.api import Index
from pandas.core.series import Series

from pandas._typing import (
ArrayLike,
Dtype,
)

from pandas.core.dtypes.dtypes import ExtensionDtype

Expand All @@ -22,12 +15,3 @@ def sanitize_array(
data, index, dtype=..., copy: bool = ..., raise_cast_failure: bool = ...
): ...
def is_empty_data(data) -> bool: ...
def create_series_with_explicit_dtype(
data=...,
index: ArrayLike | Index | None = ...,
dtype: Dtype | None = ...,
name: str | None = ...,
copy: bool = ...,
fastpath: bool = ...,
dtype_if_empty: Dtype = ...,
) -> Series: ...
2 changes: 1 addition & 1 deletion pandas-stubs/core/frame.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -2127,7 +2127,7 @@ class DataFrame(NDFrame, OpsMixin, _GetItemHack):
method: _str | FillnaOptions | Literal["nearest"] | None = ...,
copy: _bool = ...,
limit: int | None = ...,
tolerance=...,
tolerance: Scalar | AnyArrayLike | Sequence[Scalar] = ...,
) -> Self: ...
# Rename axis with `mapper`, `axis`, and `inplace=True`
@overload
Expand Down
8 changes: 0 additions & 8 deletions pandas-stubs/core/generic.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -294,14 +294,6 @@ class NDFrame(indexing.IndexingMixin):
) -> _str: ...
def take(self, indices, axis=..., **kwargs) -> Self: ...
def __delitem__(self, idx: Hashable) -> None: ...
def reindex_like(
self,
other,
method: _str | None = ...,
copy: _bool = ...,
limit=...,
tolerance=...,
) -> Self: ...
@overload
def drop(
self,
Expand Down
9 changes: 3 additions & 6 deletions pandas-stubs/core/reshape/encoding.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,17 @@ from collections.abc import (
Iterable,
)

from pandas import (
DataFrame,
Series,
)
from pandas import DataFrame

from pandas._typing import (
ArrayLike,
AnyArrayLike,
Dtype,
HashableT1,
HashableT2,
)

def get_dummies(
data: ArrayLike | DataFrame | Series,
data: AnyArrayLike | DataFrame,
prefix: str | Iterable[str] | dict[HashableT1, str] | None = ...,
prefix_sep: str = ...,
dummy_na: bool = ...,
Expand Down
2 changes: 1 addition & 1 deletion pandas-stubs/core/series.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -1151,7 +1151,7 @@ class Series(IndexOpsMixin[S1], NDFrame):
method: _str | FillnaOptions | Literal["nearest"] | None = ...,
copy: _bool = ...,
limit: int | None = ...,
tolerance: float | None = ...,
tolerance: Scalar | AnyArrayLike | Sequence[Scalar] = ...,
) -> Self: ...
@overload
def fillna(
Expand Down
12 changes: 12 additions & 0 deletions tests/test_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -2663,6 +2663,18 @@ def test_frame_reindex() -> None:
df.reindex([2, 1, 0])


def test_frame_reindex_like() -> None:
# GH 84
df = pd.DataFrame({"a": [1, 2, 3]}, index=[0, 1, 2])
other = pd.DataFrame({"a": [1, 2]}, index=[1, 0])
check(
assert_type(
df.reindex_like(other, method="nearest", tolerance=[0.5, 0.2]), pd.DataFrame
),
pd.DataFrame,
)


def test_frame_ndarray_assignmment() -> None:
# GH 100
df_a = pd.DataFrame({"a": [0.0] * 10})
Expand Down
18 changes: 18 additions & 0 deletions tests/test_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -3552,3 +3552,21 @@ def test_series_int_float() -> None:
check(
assert_type(pd.Series([1, 2.0, 3]), "pd.Series[float]"), pd.Series, np.float64
)


def test_series_reindex() -> None:
s = pd.Series([1, 2, 3], index=[0, 1, 2])
check(assert_type(s.reindex([2, 1, 0]), "pd.Series[int]"), pd.Series, np.integer)


def test_series_reindex_like() -> None:
s = pd.Series([1, 2, 3], index=[0, 1, 2])
other = pd.Series([1, 2], index=[1, 0])
check(
assert_type(
s.reindex_like(other, method="nearest", tolerance=[0.5, 0.2]),
"pd.Series[int]",
),
pd.Series,
np.integer,
)

0 comments on commit a32c467

Please sign in to comment.