Skip to content

Commit 3b589b1

Browse files
committed
BUG: PeriodIndex fails to handle NA, rather than putting NaT in its place (pandas-dev#46673)
1 parent 0a8b45f commit 3b589b1

File tree

3 files changed

+18
-1
lines changed

3 files changed

+18
-1
lines changed

doc/source/whatsnew/v1.5.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -906,6 +906,7 @@ Indexing
906906
- Bug in :meth:`NDFrame.xs`, :meth:`DataFrame.iterrows`, :meth:`DataFrame.loc` and :meth:`DataFrame.iloc` not always propagating metadata (:issue:`28283`)
907907
- Bug in :meth:`DataFrame.sum` min_count changes dtype if input contains NaNs (:issue:`46947`)
908908
- Bug in :class:`IntervalTree` that lead to an infinite recursion. (:issue:`46658`)
909+
- Bug in :class:`PeriodIndex` raising ``AttributeError`` when indexing on ``NA``, rather than putting ``NaT`` in its place. (:issue:`46673`)
909910
-
910911

911912
Missing

pandas/_libs/tslibs/nattype.pyx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ from numpy cimport int64_t
2828
cnp.import_array()
2929

3030
cimport pandas._libs.tslibs.util as util
31+
from pandas._libs.missing cimport C_NA
3132
from pandas._libs.tslibs.np_datetime cimport (
3233
get_datetime64_value,
3334
get_timedelta64_value,
@@ -1217,7 +1218,7 @@ cdef inline bint checknull_with_nat(object val):
12171218
"""
12181219
Utility to check if a value is a nat or not.
12191220
"""
1220-
return val is None or util.is_nan(val) or val is c_NaT
1221+
return val is None or util.is_nan(val) or val is c_NaT or val is C_NA
12211222

12221223

12231224
cdef inline bint is_dt64nat(object val):

pandas/tests/arrays/categorical/test_indexing.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
1+
import math
2+
13
import numpy as np
24
import pytest
35

46
from pandas import (
7+
NA,
58
Categorical,
69
CategoricalIndex,
710
Index,
811
Interval,
912
IntervalIndex,
13+
NaT,
1014
PeriodIndex,
1115
Series,
1216
Timedelta,
@@ -194,6 +198,17 @@ def test_categories_assignments(self):
194198
tm.assert_numpy_array_equal(cat.__array__(), exp)
195199
tm.assert_index_equal(cat.categories, Index([1, 2, 3]))
196200

201+
@pytest.mark.parametrize(
202+
"null_val",
203+
[None, np.nan, NaT, NA, math.nan, "NaT", "nat", "NAT", "nan", "NaN", "NAN"],
204+
)
205+
def test_periodindex_on_null_types(self, null_val):
206+
# GH 46673
207+
result = PeriodIndex(["2022-04-06", "2022-04-07", null_val], freq="D")
208+
expected = PeriodIndex(["2022-04-06", "2022-04-07", "NaT"], dtype="period[D]")
209+
assert type(result[2]) == type(NaT)
210+
tm.assert_index_equal(result, expected)
211+
197212
@pytest.mark.parametrize("new_categories", [[1, 2, 3, 4], [1, 2]])
198213
def test_categories_assignments_wrong_length_raises(self, new_categories):
199214
cat = Categorical(["a", "b", "c", "a"])

0 commit comments

Comments
 (0)