Skip to content

Commit 25aa8c9

Browse files
committed
Merge branch 'pandas-scalars' of github.com:bashtage/pandas-stubs into pandas-scalars
2 parents c044a8c + ccbde88 commit 25aa8c9

File tree

7 files changed

+278
-15
lines changed

7 files changed

+278
-15
lines changed

pandas-stubs/_libs/interval.pyi

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,10 @@ class Interval(IntervalMixin, Generic[_OrderableT]):
6363
def right(self: Interval[_OrderableT]) -> _OrderableT: ...
6464
@property
6565
def closed(self) -> IntervalClosedType: ...
66-
mid: _MidDescriptor
67-
length: _LengthDescriptor
66+
@property
67+
def mid(self) -> _MidDescriptor: ...
68+
@property
69+
def length(self) -> _LengthDescriptor: ...
6870
def __init__(
6971
self,
7072
left: _OrderableT,
@@ -142,10 +144,6 @@ class Interval(IntervalMixin, Generic[_OrderableT]):
142144
def __floordiv__(self: Interval[float], y: float) -> Interval[float]: ...
143145
def overlaps(self: Interval[_OrderableT], other: Interval[_OrderableT]) -> bool: ...
144146

145-
def intervals_to_interval_bounds(
146-
intervals: np.ndarray, validate_closed: bool = ...
147-
) -> tuple[np.ndarray, np.ndarray, str]: ...
148-
149147
class IntervalTree(IntervalMixin):
150148
def __init__(
151149
self,

pandas-stubs/_typing.pyi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ IndexT = TypeVar("IndexT", bound=Index)
223223

224224
IntervalClosedType: TypeAlias = Literal["left", "right", "both", "neither"]
225225

226-
DateTimeErrorChoices: TypeAlias = Literal["ignore", "raise", "coerce"]
226+
IgnoreRaiseCoerce: TypeAlias = Literal["ignore", "raise", "coerce"]
227227

228228
# Shared by functions such as drop and astype
229229
IgnoreRaise: TypeAlias = Literal["ignore", "raise"]

pandas-stubs/core/tools/datetimes.pyi

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ from typing_extensions import TypeAlias
2626
from pandas._libs.tslibs import NaTType
2727
from pandas._typing import (
2828
AnyArrayLike,
29-
DateTimeErrorChoices,
3029
DictConvertible,
3130
IgnoreRaise,
31+
IgnoreRaiseCoerce,
3232
TimestampConvertibleTypes,
3333
npt,
3434
)
@@ -70,7 +70,7 @@ def to_datetime(
7070
@overload
7171
def to_datetime(
7272
arg: Series | DictConvertible,
73-
errors: DateTimeErrorChoices = ...,
73+
errors: IgnoreRaiseCoerce = ...,
7474
dayfirst: bool = ...,
7575
yearfirst: bool = ...,
7676
utc: bool | None = ...,
@@ -91,7 +91,7 @@ def to_datetime(
9191
| npt.NDArray[np.int_]
9292
| Index
9393
| ExtensionArray,
94-
errors: DateTimeErrorChoices = ...,
94+
errors: IgnoreRaiseCoerce = ...,
9595
dayfirst: bool = ...,
9696
yearfirst: bool = ...,
9797
utc: bool | None = ...,

pandas-stubs/core/tools/numeric.pyi

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,42 @@
1-
def to_numeric(arg, errors: str = ..., downcast=...): ...
1+
from typing import (
2+
Literal,
3+
Union,
4+
overload,
5+
)
6+
7+
import numpy as np
8+
import pandas as pd
9+
from typing_extensions import TypeAlias
10+
11+
from pandas._typing import (
12+
IgnoreRaiseCoerce,
13+
Scalar,
14+
npt,
15+
)
16+
17+
_Downcast: TypeAlias = Union[Literal["integer", "signed", "unsigned", "float"], None]
18+
19+
@overload
20+
def to_numeric(
21+
arg: Scalar,
22+
errors: Literal["raise", "coerce"] = ...,
23+
downcast: _Downcast = ...,
24+
) -> float: ...
25+
@overload
26+
def to_numeric(
27+
arg: Scalar,
28+
errors: Literal["ignore"],
29+
downcast: _Downcast = ...,
30+
) -> Scalar: ...
31+
@overload
32+
def to_numeric(
33+
arg: list | tuple | np.ndarray,
34+
errors: IgnoreRaiseCoerce = ...,
35+
downcast: _Downcast = ...,
36+
) -> npt.NDArray: ...
37+
@overload
38+
def to_numeric(
39+
arg: pd.Series,
40+
errors: IgnoreRaiseCoerce = ...,
41+
downcast: _Downcast = ...,
42+
) -> pd.Series: ...

pandas-stubs/core/tools/timedeltas.pyi

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,20 @@ from pandas._libs.tslibs import Timedelta
1616
from pandas._libs.tslibs.timedeltas import TimeDeltaUnitChoices
1717
from pandas._typing import (
1818
ArrayLike,
19-
DateTimeErrorChoices,
19+
IgnoreRaiseCoerce,
2020
)
2121

2222
@overload
2323
def to_timedelta(
2424
arg: str | float | timedelta,
2525
unit: TimeDeltaUnitChoices | None = ...,
26-
errors: DateTimeErrorChoices = ...,
26+
errors: IgnoreRaiseCoerce = ...,
2727
) -> Timedelta: ...
2828
@overload
2929
def to_timedelta(
3030
arg: Series,
3131
unit: TimeDeltaUnitChoices | None = ...,
32-
errors: DateTimeErrorChoices = ...,
32+
errors: IgnoreRaiseCoerce = ...,
3333
) -> TimedeltaSeries: ...
3434
@overload
3535
def to_timedelta(
@@ -40,5 +40,5 @@ def to_timedelta(
4040
| ArrayLike
4141
| Index,
4242
unit: TimeDeltaUnitChoices | None = ...,
43-
errors: DateTimeErrorChoices = ...,
43+
errors: IgnoreRaiseCoerce = ...,
4444
) -> TimedeltaIndex: ...

tests/test_dtypes.py

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
from datetime import (
2+
timedelta,
3+
timezone,
4+
)
5+
6+
import numpy as np
7+
import pandas as pd
8+
from typing_extensions import assert_type
9+
10+
from tests import check
11+
12+
from pandas.tseries.offsets import (
13+
BusinessDay,
14+
CustomBusinessDay,
15+
Day,
16+
)
17+
18+
19+
def test_datetimetz_dtype() -> None:
20+
check(
21+
assert_type(pd.DatetimeTZDtype(unit="ns", tz="UTC"), pd.DatetimeTZDtype),
22+
pd.DatetimeTZDtype,
23+
)
24+
check(
25+
assert_type(
26+
pd.DatetimeTZDtype(unit="ns", tz=timezone(timedelta(hours=1))),
27+
pd.DatetimeTZDtype,
28+
),
29+
pd.DatetimeTZDtype,
30+
)
31+
32+
33+
def test_period_dtype() -> None:
34+
check(assert_type(pd.PeriodDtype(freq="D"), pd.PeriodDtype), pd.PeriodDtype)
35+
check(assert_type(pd.PeriodDtype(freq=Day()), pd.PeriodDtype), pd.PeriodDtype)
36+
check(
37+
assert_type(pd.PeriodDtype(freq=BusinessDay()), pd.PeriodDtype), pd.PeriodDtype
38+
)
39+
check(
40+
assert_type(pd.PeriodDtype(freq=CustomBusinessDay()), pd.PeriodDtype),
41+
pd.PeriodDtype,
42+
)
43+
44+
45+
def test_interval_dtype() -> None:
46+
check(
47+
assert_type(
48+
pd.Interval(pd.Timestamp("2017-01-01"), pd.Timestamp("2017-01-02")),
49+
"pd.Interval[pd.Timestamp]",
50+
),
51+
pd.Interval,
52+
)
53+
check(
54+
assert_type(pd.Interval(1, 2, closed="left"), "pd.Interval[int]"), pd.Interval
55+
)
56+
check(
57+
assert_type(pd.Interval(1.0, 2.5, closed="right"), "pd.Interval[float]"),
58+
pd.Interval,
59+
)
60+
check(
61+
assert_type(pd.Interval(1.0, 2.5, closed="both"), "pd.Interval[float]"),
62+
pd.Interval,
63+
)
64+
check(
65+
assert_type(
66+
pd.Interval(
67+
pd.Timedelta("1 day"), pd.Timedelta("2 days"), closed="neither"
68+
),
69+
"pd.Interval[pd.Timedelta]",
70+
),
71+
pd.Interval,
72+
)
73+
74+
75+
def test_int64_dtype() -> None:
76+
check(assert_type(pd.Int64Dtype(), pd.Int64Dtype), pd.Int64Dtype)
77+
78+
79+
def test_categorical_dtype() -> None:
80+
check(
81+
assert_type(
82+
pd.CategoricalDtype(categories=["a", "b", "c"], ordered=True),
83+
pd.CategoricalDtype,
84+
),
85+
pd.CategoricalDtype,
86+
)
87+
check(
88+
assert_type(pd.CategoricalDtype(categories=[1, 2, 3]), pd.CategoricalDtype),
89+
pd.CategoricalDtype,
90+
)
91+
92+
93+
def test_sparse_dtype() -> None:
94+
check(assert_type(pd.SparseDtype(str), pd.SparseDtype), pd.SparseDtype)
95+
check(assert_type(pd.SparseDtype(complex), pd.SparseDtype), pd.SparseDtype)
96+
check(assert_type(pd.SparseDtype(bool), pd.SparseDtype), pd.SparseDtype)
97+
check(assert_type(pd.SparseDtype(int), pd.SparseDtype), pd.SparseDtype)
98+
check(assert_type(pd.SparseDtype(np.int64), pd.SparseDtype), pd.SparseDtype)
99+
check(assert_type(pd.SparseDtype(str), pd.SparseDtype), pd.SparseDtype)
100+
check(assert_type(pd.SparseDtype(float), pd.SparseDtype), pd.SparseDtype)
101+
check(assert_type(pd.SparseDtype(np.datetime64), pd.SparseDtype), pd.SparseDtype)
102+
check(assert_type(pd.SparseDtype(np.timedelta64), pd.SparseDtype), pd.SparseDtype)
103+
check(assert_type(pd.SparseDtype("datetime64"), pd.SparseDtype), pd.SparseDtype)
104+
check(assert_type(pd.SparseDtype(), pd.SparseDtype), pd.SparseDtype)
105+
106+
107+
def test_string_dtype() -> None:
108+
check(assert_type(pd.StringDtype("pyarrow"), pd.StringDtype), pd.StringDtype)
109+
check(assert_type(pd.StringDtype("python"), pd.StringDtype), pd.StringDtype)
110+
111+
112+
def test_boolean_dtype() -> None:
113+
check(assert_type(pd.BooleanDtype(), pd.BooleanDtype), pd.BooleanDtype)
114+
115+
116+
def test_arrow_dtype() -> None:
117+
import pyarrow as pa
118+
119+
check(assert_type(pd.ArrowDtype(pa.int64()), pd.ArrowDtype), pd.ArrowDtype)

tests/test_pandas.py

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,111 @@ def test_eval():
457457
)
458458

459459

460+
def test_to_numeric_scalar() -> None:
461+
check(assert_type(pd.to_numeric(1), float), int)
462+
check(assert_type(pd.to_numeric("1.2"), float), float)
463+
check(assert_type(pd.to_numeric("blerg", errors="coerce"), float), float)
464+
check(assert_type(pd.to_numeric("blerg", errors="ignore"), Scalar), str)
465+
check(assert_type(pd.to_numeric(1, downcast="signed"), float), int)
466+
check(assert_type(pd.to_numeric(1, downcast="unsigned"), float), int)
467+
check(assert_type(pd.to_numeric(1, downcast="float"), float), int)
468+
check(assert_type(pd.to_numeric(1, downcast="integer"), float), int)
469+
470+
471+
def test_to_numeric_array_like() -> None:
472+
check(
473+
assert_type(
474+
pd.to_numeric([1, 2, 3]),
475+
npt.NDArray,
476+
),
477+
np.ndarray,
478+
)
479+
check(
480+
assert_type(
481+
pd.to_numeric([1.0, 2.0, 3.0]),
482+
npt.NDArray,
483+
),
484+
np.ndarray,
485+
)
486+
check(
487+
assert_type(
488+
pd.to_numeric([1.0, 2.0, "3.0"]),
489+
npt.NDArray,
490+
),
491+
np.ndarray,
492+
)
493+
check(
494+
assert_type(
495+
pd.to_numeric(np.array([1.0, 2.0, "3.0"], dtype=object)),
496+
npt.NDArray,
497+
),
498+
np.ndarray,
499+
)
500+
check(
501+
assert_type(
502+
pd.to_numeric([1.0, 2.0, "blerg"], errors="coerce"),
503+
npt.NDArray,
504+
),
505+
np.ndarray,
506+
)
507+
check(
508+
assert_type(pd.to_numeric([1.0, 2.0, "blerg"], errors="ignore"), npt.NDArray),
509+
np.ndarray,
510+
)
511+
check(
512+
assert_type(
513+
pd.to_numeric((1.0, 2.0, 3.0)),
514+
npt.NDArray,
515+
),
516+
np.ndarray,
517+
)
518+
check(
519+
assert_type(pd.to_numeric([1, 2, 3], downcast="unsigned"), npt.NDArray),
520+
np.ndarray,
521+
)
522+
523+
524+
def test_to_numeric_array_series() -> None:
525+
check(
526+
assert_type(
527+
pd.to_numeric(pd.Series([1, 2, 3])),
528+
pd.Series,
529+
),
530+
pd.Series,
531+
)
532+
check(
533+
assert_type(
534+
pd.to_numeric(pd.Series([1, 2, "blerg"]), errors="coerce"),
535+
pd.Series,
536+
),
537+
pd.Series,
538+
)
539+
check(
540+
assert_type(
541+
pd.to_numeric(pd.Series([1, 2, "blerg"]), errors="ignore"), pd.Series
542+
),
543+
pd.Series,
544+
)
545+
check(
546+
assert_type(pd.to_numeric(pd.Series([1, 2, 3]), downcast="signed"), pd.Series),
547+
pd.Series,
548+
)
549+
check(
550+
assert_type(
551+
pd.to_numeric(pd.Series([1, 2, 3]), downcast="unsigned"), pd.Series
552+
),
553+
pd.Series,
554+
)
555+
check(
556+
assert_type(pd.to_numeric(pd.Series([1, 2, 3]), downcast="integer"), pd.Series),
557+
pd.Series,
558+
)
559+
check(
560+
assert_type(pd.to_numeric(pd.Series([1, 2, 3]), downcast="float"), pd.Series),
561+
pd.Series,
562+
)
563+
564+
460565
def test_wide_to_long():
461566
df = pd.DataFrame(
462567
{

0 commit comments

Comments
 (0)