Skip to content

Commit 426e31d

Browse files
amotzopAmotz
and
Amotz
authored
__neg__ methods and bugfix (#253)
* Added __neg__ to Series, DataFrame and Index. Fixed bug in Index.difference * Moved to NDFrameT and IndexT * sorted imports Co-authored-by: Amotz <amotz@nonaya.bis>
1 parent 0822888 commit 426e31d

File tree

6 files changed

+37
-5
lines changed

6 files changed

+37
-5
lines changed

pandas-stubs/_typing.pyi

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,8 @@ IndexingInt = Union[
169169
# passed in, a DataFrame is always returned.
170170
NDFrameT = TypeVar("NDFrameT", bound=NDFrame)
171171

172+
IndexT = TypeVar("IndexT", bound=Index)
173+
172174
# Interval closed type
173175

174176
IntervalClosedType = Literal["left", "right", "both", "neither"]

pandas-stubs/core/generic.pyi

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ from pandas._typing import (
2929
HDFCompLib,
3030
IgnoreRaise,
3131
Level,
32+
NDFrameT,
3233
ReplaceMethod,
3334
Scalar,
3435
SeriesAxisType,
@@ -69,8 +70,8 @@ class NDFrame(PandasObject, indexing.IndexingMixin):
6970
def pop(self, item: _str) -> NDFrame: ...
7071
def squeeze(self, axis=...): ...
7172
def equals(self, other: Series[S1]) -> _bool: ...
72-
def __neg__(self) -> None: ...
73-
def __pos__(self) -> None: ...
73+
def __neg__(self: NDFrameT) -> NDFrameT: ...
74+
def __pos__(self: NDFrameT) -> NDFrameT: ...
7475
def __nonzero__(self) -> None: ...
7576
def bool(self) -> _bool: ...
7677
def __abs__(self) -> NDFrame: ...

pandas-stubs/core/indexes/base.pyi

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ from pandas._typing import (
2727
Dtype,
2828
DtypeArg,
2929
DtypeObj,
30+
IndexT,
3031
Label,
3132
Level,
3233
NaPosition,
@@ -142,11 +143,12 @@ class Index(IndexOpsMixin, PandasObject):
142143
def __and__(self, other) -> Index: ...
143144
def __or__(self, other) -> Index: ...
144145
def __xor__(self, other) -> Index: ...
146+
def __neg__(self: IndexT) -> IndexT: ...
145147
def __nonzero__(self) -> None: ...
146148
__bool__ = ...
147149
def union(self, other: list[T1] | Index, sort=...) -> Index: ...
148150
def intersection(self, other: list[T1] | Index, sort: bool = ...) -> Index: ...
149-
def difference(self, other: list[T1 | None] | Index) -> Index: ...
151+
def difference(self, other: list | Index) -> Index: ...
150152
def symmetric_difference(
151153
self, other: list[T1] | Index, result_name=..., sort=...
152154
) -> Index: ...

tests/test_frame.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1685,3 +1685,15 @@ def func() -> MyDataFrame[int]:
16851685
return MyDataFrame[int]({"foo": [1, 2, 3]})
16861686

16871687
func()
1688+
1689+
1690+
def test_neg() -> None:
1691+
# GH 253
1692+
df = pd.DataFrame(data={"col1": [1, 2], "col2": [3, 4]})
1693+
check(assert_type(-df, pd.DataFrame), pd.DataFrame)
1694+
1695+
1696+
def test_pos() -> None:
1697+
# GH 253
1698+
df = pd.DataFrame(data={"col1": [1, 2], "col2": [3, 4]})
1699+
check(assert_type(+df, pd.DataFrame), pd.DataFrame)

tests/test_indexes.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010

1111
def test_index_unique() -> None:
12-
1312
df = pd.DataFrame({"x": [1, 2, 3, 4]}, index=pd.Index([1, 2, 3, 2]))
1413
ind = df.index
1514
check(assert_type(ind, pd.Index), pd.Index)
@@ -67,7 +66,9 @@ def test_column_contains() -> None:
6766
def test_difference_none() -> None:
6867
# https://github.com/pandas-dev/pandas-stubs/issues/17
6968
ind = pd.Index([1, 2, 3])
70-
check(assert_type(ind.difference([1, None]), "pd.Index"), pd.Index, int)
69+
check(assert_type(ind.difference([1, None]), pd.Index), pd.Index)
70+
# GH 253
71+
check(assert_type(ind.difference([1]), pd.Index), pd.Index)
7172

7273

7374
def test_str_split() -> None:
@@ -87,3 +88,9 @@ def test_index_dropna():
8788

8889
check(assert_type(midx.dropna(how="all"), pd.MultiIndex), pd.MultiIndex)
8990
check(assert_type(midx.dropna(how="any"), pd.MultiIndex), pd.MultiIndex)
91+
92+
93+
def test_index_neg():
94+
# GH 253
95+
idx = pd.Index([1, 2])
96+
check(assert_type(-idx, pd.Index), pd.Index)

tests/test_series.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1124,3 +1124,11 @@ def test_resample() -> None:
11241124
check(assert_type(df.resample("2T").sem(), pd.Series), pd.Series)
11251125
check(assert_type(df.resample("2T").median(), pd.Series), pd.Series)
11261126
check(assert_type(df.resample("2T").ohlc(), pd.DataFrame), pd.DataFrame)
1127+
1128+
1129+
def test_neg() -> None:
1130+
# GH 253
1131+
sr = pd.Series([1, 2, 3])
1132+
sr_int = pd.Series([1, 2, 3], dtype=int)
1133+
check(assert_type(-sr, pd.Series), pd.Series)
1134+
check(assert_type(-sr_int, "pd.Series[int]"), pd.Series, int)

0 commit comments

Comments
 (0)