Skip to content

Commit e79d253

Browse files
committed
Add DatetimeNaTType type
Change test for NaT in io/formats/format.py
1 parent d980ebe commit e79d253

File tree

4 files changed

+12
-10
lines changed

4 files changed

+12
-10
lines changed

pandas/_typing.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import numpy.typing as npt
3636

3737
from pandas._libs import (
38+
NaTType,
3839
Period,
3940
Timedelta,
4041
Timestamp,
@@ -308,3 +309,7 @@ def closed(self) -> bool:
308309
# Interval closed type
309310

310311
IntervalClosedType = Literal["left", "right", "both", "neither"]
312+
313+
# datetime and NaTType
314+
315+
DatetimeNaTType = Union[datetime, "NaTType"]

pandas/io/formats/format.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1767,7 +1767,7 @@ def _format_datetime64_dateonly(
17671767
nat_rep: str = "NaT",
17681768
date_format: str | None = None,
17691769
) -> str:
1770-
if x is NaT or isinstance(x, NaTType):
1770+
if isinstance(x, NaTType):
17711771
return nat_rep
17721772

17731773
if date_format:

pandas/io/sas/sas_xport.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616

1717
import numpy as np
1818

19-
from pandas._libs.tslibs import NaTType
2019
from pandas._typing import (
20+
DatetimeNaTType,
2121
FilePath,
2222
ReadBuffer,
2323
)
@@ -140,7 +140,7 @@
140140
"""
141141

142142

143-
def _parse_date(datestr: str) -> datetime | NaTType:
143+
def _parse_date(datestr: str) -> DatetimeNaTType:
144144
"""Given a date in xport format, return Python date."""
145145
try:
146146
# e.g. "16FEB11:10:07:55"

pandas/tests/resample/test_datetime_index.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
11
from datetime import datetime
22
from functools import partial
33
from io import StringIO
4-
from typing import (
5-
List,
6-
Union,
7-
)
4+
from typing import List
85

96
import numpy as np
107
import pytest
118
import pytz
129

1310
from pandas._libs import lib
14-
from pandas._libs.tslibs import NaTType
11+
from pandas._typing import DatetimeNaTType
1512
from pandas.errors import UnsupportedFunctionCall
1613

1714
import pandas as pd
@@ -1291,7 +1288,7 @@ def test_resample_consistency():
12911288
tm.assert_series_equal(s10_2, rl)
12921289

12931290

1294-
dates1: List[Union[datetime, NaTType]] = [
1291+
dates1: List[DatetimeNaTType] = [
12951292
datetime(2014, 10, 1),
12961293
datetime(2014, 9, 3),
12971294
datetime(2014, 11, 5),
@@ -1300,7 +1297,7 @@ def test_resample_consistency():
13001297
datetime(2014, 7, 15),
13011298
]
13021299

1303-
dates2: List[Union[datetime, NaTType]] = (
1300+
dates2: List[DatetimeNaTType] = (
13041301
dates1[:2] + [pd.NaT] + dates1[2:4] + [pd.NaT] + dates1[4:]
13051302
)
13061303
dates3 = [pd.NaT] + dates1 + [pd.NaT] # type: ignore[operator]

0 commit comments

Comments
 (0)