Skip to content

ENH: Overload to_datetime when errors="coerce" #115

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
18 changes: 17 additions & 1 deletion pandas-stubs/core/tools/datetimes.pyi
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from datetime import datetime as datetime
from typing import (
List,
Literal,
Optional,
Tuple,
TypedDict,
Expand All @@ -18,6 +19,7 @@ from pandas.core.series import (
TimestampSeries,
)

from pandas._libs.tslibs import NaTType
from pandas._typing import (
AnyArrayLike as AnyArrayLike,
ArrayLike as ArrayLike,
Expand Down Expand Up @@ -61,7 +63,7 @@ def should_cache(
@overload
def to_datetime(
arg: DatetimeScalar,
errors: DateTimeErrorChoices = ...,
errors: Literal["ignore", "raise"] = ...,
dayfirst: bool = ...,
yearfirst: bool = ...,
utc: bool | None = ...,
Expand All @@ -73,6 +75,20 @@ def to_datetime(
cache: bool = ...,
) -> Timestamp: ...
@overload
def to_datetime(
arg: DatetimeScalar,
errors: Literal["coerce"],
dayfirst: bool = ...,
yearfirst: bool = ...,
utc: bool | None = ...,
format: str | None = ...,
exact: bool = ...,
unit: str | None = ...,
infer_datetime_format: bool = ...,
origin=...,
cache: bool = ...,
) -> Timestamp | NaTType: ...
@overload
def to_datetime(
arg: Series | DictConvertible,
errors: DateTimeErrorChoices = ...,
Expand Down
29 changes: 29 additions & 0 deletions tests/test_timefuncs.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@
from typing import (
TYPE_CHECKING,
Optional,
Union,
)

import numpy as np
from numpy import typing as npt
import pandas as pd
from typing_extensions import assert_type

from pandas._libs import NaTType

if TYPE_CHECKING:
from pandas.core.series import (
TimedeltaSeries,
Expand Down Expand Up @@ -209,3 +212,29 @@ def test_comparisons_datetimeindex() -> None:
assert_type((dti <= ts), np_ndarray_bool)
assert_type((dti == ts), np_ndarray_bool)
assert_type((dti != ts), np_ndarray_bool)


def test_to_datetime_nat() -> None:
# GH 88
assert isinstance(
assert_type(pd.to_datetime("2021-03-01", errors="ignore"), "pd.Timestamp"),
pd.Timestamp,
)
assert isinstance(
assert_type(pd.to_datetime("2021-03-01", errors="raise"), "pd.Timestamp"),
pd.Timestamp,
)
assert isinstance(
assert_type(
pd.to_datetime("2021-03-01", errors="coerce"),
"Union[pd.Timestamp, NaTType]",
),
pd.Timestamp,
)
assert isinstance(
assert_type(
pd.to_datetime("not a date", errors="coerce"),
"Union[pd.Timestamp, NaTType]",
),
NaTType,
)