Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
series
  • Loading branch information
crusaderky committed Jul 1, 2022
commit 48a08e174c7b6e5e2bda6566b16f89ae9fa971e1
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,9 @@ venv.bak/
# Rope project settings
.ropeproject

# PyCharm project settings
.idea/

# mkdocs documentation
/site

Expand Down
56 changes: 27 additions & 29 deletions pandas-stubs/core/series.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -467,18 +467,7 @@ class Series(IndexOpsMixin, NDFrame, Generic[S1]):
na_position: Union[_str, Literal["first", "last"]] = ...,
ignore_index: _bool = ...,
*,
inplace: Literal[False],
key: Optional[Callable] = ...,
) -> Series[S1]: ...
@overload
def sort_values(
self,
axis: AxisType = ...,
ascending: Union[_bool, Sequence[_bool]] = ...,
*,
kind: Union[_str, Literal["quicksort", "mergesort", "heapsort"]] = ...,
na_position: Union[_str, Literal["first", "last"]] = ...,
ignore_index: _bool = ...,
inplace: Literal[False] = ...,
key: Optional[Callable] = ...,
) -> Series[S1]: ...
@overload
Expand Down Expand Up @@ -517,20 +506,7 @@ class Series(IndexOpsMixin, NDFrame, Generic[S1]):
sort_remaining: _bool = ...,
ignore_index: _bool = ...,
*,
inplace: Literal[False],
key: Optional[Callable] = ...,
) -> Series: ...
@overload
def sort_index(
self,
axis: AxisType = ...,
level: Optional[Union[Level, List[int], List[_str]]] = ...,
ascending: Union[_bool, Sequence[_bool]] = ...,
*,
kind: Union[_str, Literal["quicksort", "mergesort", "heapsort"]] = ...,
na_position: Union[_str, Literal["first", "last"]] = ...,
sort_remaining: _bool = ...,
ignore_index: _bool = ...,
inplace: Literal[False] = ...,
key: Optional[Callable] = ...,
) -> Series: ...
@overload
Expand Down Expand Up @@ -747,15 +723,37 @@ class Series(IndexOpsMixin, NDFrame, Generic[S1]):
limit: Optional[int] = ...,
downcast: Optional[Dict] = ...,
) -> Union[Series[S1], None]: ...
@overload
def replace(
self,
to_replace: Optional[Union[_str, List, Dict, Series[S1], int, float]] = ...,
value: Optional[Union[Scalar, Dict, List, _str]] = ...,
inplace: _bool = ...,
inplace: Literal[False] = ...,
limit: Optional[int] = ...,
regex=...,
method: Optional[Union[_str, Literal["pad", "ffill", "bfill"]]] = ...,
) -> Series[S1]: ...
@overload
def replace(
self,
to_replace: Optional[Union[_str, List, Dict, Series[S1], int, float]] = ...,
value: Optional[Union[Scalar, Dict, List, _str]] = ...,
limit: Optional[int] = ...,
regex=...,
method: Optional[Union[_str, Literal["pad", "ffill", "bfill"]]] = ...,
*,
inplace: Literal[True],
) -> None: ...
@overload
def replace(
self,
to_replace: Optional[Union[_str, List, Dict, Series[S1], int, float]] = ...,
value: Optional[Union[Scalar, Dict, List, _str]] = ...,
inplace: _bool = ...,
limit: Optional[int] = ...,
regex=...,
method: Optional[Union[_str, Literal["pad", "ffill", "bfill"]]] = ...,
) -> Union[Series[S1], None]: ...
def shift(
self,
periods: int = ...,
Expand Down Expand Up @@ -878,7 +876,7 @@ class Series(IndexOpsMixin, NDFrame, Generic[S1]):
self,
axis: Optional[SeriesAxisType] = ...,
*,
inplace: Literal[False],
inplace: Literal[False] = ...,
limit: Optional[int] = ...,
downcast: Optional[Dict] = ...,
) -> Series[S1]: ...
Expand All @@ -896,7 +894,7 @@ class Series(IndexOpsMixin, NDFrame, Generic[S1]):
self,
axis: Optional[SeriesAxisType] = ...,
*,
inplace: Literal[False],
inplace: Literal[False] = ...,
limit: Optional[int] = ...,
downcast: Optional[Dict] = ...,
) -> Series[S1]: ...
Expand Down
38 changes: 25 additions & 13 deletions tests/test_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ def test_types_dropna() -> None:

def test_types_fillna() -> None:
s = pd.Series([1, np.nan, np.nan, 3])
res: pd.Series = s.fillna(0)
res1: pd.Series = s.fillna(0)
res2: pd.Series = s.fillna(0, axis="index")
res3: pd.Series = s.fillna(method="backfill", axis=0)
res4: None = s.fillna(method="bfill", inplace=True)
Expand All @@ -180,9 +180,10 @@ def test_types_fillna() -> None:

def test_types_sort_index() -> None:
s = pd.Series([1, 2, 3], index=[2, 3, 1])
res: pd.Series = s.sort_index()
res2: None = s.sort_index(ascending=False, inplace=True)
res3: pd.Series = s.sort_index(kind="mergesort")
res1: pd.Series = s.sort_index()
res2: pd.Series = s.sort_index(ascending=False)
res3: None = s.sort_index(ascending=False, inplace=True)
res4: pd.Series = s.sort_index(kind="mergesort")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know most tests do not yet use typing_extensions.assert_type. If you want, you could re-write these new tests to use assert_type(s.sort_index(), pd.Series).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done



# This was added in 1.1.0 https://pandas.pydata.org/docs/whatsnew/v1.1.0.html
Expand All @@ -193,11 +194,12 @@ def test_types_sort_index_with_key() -> None:

def test_types_sort_values() -> None:
s = pd.Series([4, 2, 1, 3])
res: pd.Series = s.sort_values(0)
res2: pd.Series = s.sort_values(ascending=False)
res3: None = s.sort_values(inplace=True, kind="quicksort")
res4: pd.Series = s.sort_values(na_position="last")
res5: pd.Series = s.sort_values(ignore_index=True)
res1: pd.Series = s.sort_values()
res2: pd.Series = s.sort_values(0)
res3: pd.Series = s.sort_values(ascending=False)
res4: None = s.sort_values(inplace=True, kind="quicksort")
res5: pd.Series = s.sort_values(na_position="last")
res6: pd.Series = s.sort_values(ignore_index=True)


# This was added in 1.1.0 https://pandas.pydata.org/docs/whatsnew/v1.1.0.html
Expand Down Expand Up @@ -547,8 +549,9 @@ def test_types_ne() -> None:

def test_types_bfill() -> None:
s1 = pd.Series([1, 2, 3])
s2: pd.Series = s1.bfill(inplace=False)
s3: None = s1.bfill(inplace=True)
s2: pd.Series = s1.bfill()
s3: pd.Series = s1.bfill(inplace=False)
s4: None = s1.bfill(inplace=True)


def test_types_ewm() -> None:
Expand All @@ -563,8 +566,9 @@ def test_types_ewm() -> None:

def test_types_ffill() -> None:
s1 = pd.Series([1, 2, 3])
s2: pd.Series = s1.ffill(inplace=False)
s3: None = s1.ffill(inplace=True)
s2: pd.Series = s1.ffill()
s3: pd.Series = s1.ffill(inplace=False)
s4: None = s1.ffill(inplace=True)


def test_types_as_type() -> None:
Expand Down Expand Up @@ -671,3 +675,11 @@ def test_series_add_str() -> None:
def test_series_dtype() -> None:
s = pd.Series(["abc", "def"], dtype=str)
assert_type(s, "pd.Series[str]")


def test_types_replace() -> None:
s = pd.Series([1, 2, 3])
res1: pd.Series = s.replace(1, 2)
res2: pd.Series = s.replace(1, 2, inplace=False)
res3: None = s.replace(1, 2, inplace=True)