Skip to content

Commit 300adaf

Browse files
authored
Fixes mypy error when passing np.datetime64/timedelta64 input to pd.isna (#550)
Update `IndexIterScalar` to include `np.datetime64` and `np.timedelta64` (#549) * Fixes mypy error when passing np.datetime64/timedelta64 input to pd.isna * Update test_isna to include tests for np.datetime64/timedelta64
1 parent 949885e commit 300adaf

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

pandas-stubs/_typing.pyi

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,8 @@ IndexIterScalar: TypeAlias = (
163163
| datetime.date
164164
| datetime.datetime
165165
| datetime.timedelta
166+
| np.datetime64
167+
| np.timedelta64
166168
| bool
167169
| int
168170
| float

tests/test_pandas.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,27 @@ def test_isna() -> None:
266266
assert not check(assert_type(pd.isna(2.5), bool), bool)
267267
assert check(assert_type(pd.notna(2.5), bool), bool)
268268

269+
# Checks for datetime, timedelta, np.datetime64 and np.timedelta64
270+
py_dt = dt.datetime.now()
271+
assert check(assert_type(pd.notna(py_dt), bool), bool)
272+
assert not check(assert_type(pd.isna(py_dt), bool), bool)
273+
274+
py_td = dt.datetime.now() - py_dt
275+
assert check(assert_type(pd.notna(py_td), bool), bool)
276+
assert not check(assert_type(pd.isna(py_td), bool), bool)
277+
278+
np_dt = np.datetime64(py_dt)
279+
assert check(assert_type(pd.notna(np_dt), bool), bool)
280+
assert not check(assert_type(pd.isna(np_dt), bool), bool)
281+
282+
np_td = np.timedelta64(py_td)
283+
assert check(assert_type(pd.notna(np_td), bool), bool)
284+
assert not check(assert_type(pd.isna(np_td), bool), bool)
285+
286+
np_nat = np.timedelta64("NaT")
287+
assert check(assert_type(pd.isna(np_nat), bool), bool)
288+
assert not check(assert_type(pd.notna(np_nat), bool), bool)
289+
269290
# Check TypeGuard type narrowing functionality
270291
# TODO: Due to limitations in TypeGuard spec, the true annotations are not always viable
271292
# and as a result the type narrowing does not always work as it intuitively should

0 commit comments

Comments
 (0)