Skip to content

Commit fbfa4c1

Browse files
author
Kevin Sheppard
committed
Merge remote-tracking branch 'upstream/main' into pandas-scalars
2 parents f58c47c + 5cfc849 commit fbfa4c1

File tree

5 files changed

+155
-9
lines changed

5 files changed

+155
-9
lines changed

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_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)