Skip to content

Commit 93c2fc2

Browse files
authored
BUG: PeriodIndex fails to handle NA, rather than putting NaT in its place (pandas-dev#46673) (pandas-dev#47780)
* BUG: PeriodIndex fails to handle NA, rather than putting NaT in its place (pandas-dev#46673) * BUG: PeriodIndex fails to handle NA, rather than putting NaT in its place (pandas-dev#46673) * BUG: Series.astype is unable to handle pd.nan. * BUG: PeriodIndex fails to handle NA, rather than putting NaT in its place (pandas-dev#46673) * BUG: Series.astype is unable to handle pd.nan (pandas-dev#46377) * BUG: PeriodIndex fails to handle NA, rather than putting NaT in its place (pandas-dev#46673) * BUG: PeriodIndex fails to handle NA, rather than putting NaT in its place (pandas-dev#46673) * BUG: PeriodIndex fails to handle NA, rather than putting NaT in its place (pandas-dev#46673)
1 parent 8d16504 commit 93c2fc2

File tree

4 files changed

+19
-2
lines changed

4 files changed

+19
-2
lines changed

doc/source/whatsnew/v1.5.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -912,6 +912,7 @@ Indexing
912912
- Bug in :meth:`NDFrame.xs`, :meth:`DataFrame.iterrows`, :meth:`DataFrame.loc` and :meth:`DataFrame.iloc` not always propagating metadata (:issue:`28283`)
913913
- Bug in :meth:`DataFrame.sum` min_count changes dtype if input contains NaNs (:issue:`46947`)
914914
- Bug in :class:`IntervalTree` that lead to an infinite recursion. (:issue:`46658`)
915+
- Bug in :class:`PeriodIndex` raising ``AttributeError`` when indexing on ``NA``, rather than putting ``NaT`` in its place. (:issue:`46673`)
915916
-
916917

917918
Missing

pandas/_libs/tslibs/period.pyx

+2-1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ from libc.time cimport (
4343
import_datetime()
4444

4545
cimport pandas._libs.tslibs.util as util
46+
from pandas._libs.missing cimport C_NA
4647
from pandas._libs.tslibs.np_datetime cimport (
4748
NPY_DATETIMEUNIT,
4849
NPY_FR_D,
@@ -1470,7 +1471,7 @@ cdef inline int64_t _extract_ordinal(object item, str freqstr, freq) except? -1:
14701471
cdef:
14711472
int64_t ordinal
14721473

1473-
if checknull_with_nat(item):
1474+
if checknull_with_nat(item) or item is C_NA:
14741475
ordinal = NPY_NAT
14751476
elif util.is_integer_object(item):
14761477
if item == NPY_NAT:

pandas/tests/arrays/categorical/test_indexing.py

+15
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 result[2] is 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"])

pandas/tests/series/methods/test_astype.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,7 @@ def test_astype_string_to_extension_dtype_roundtrip(
446446
self, data, dtype, request, nullable_string_dtype
447447
):
448448
if dtype == "boolean" or (
449-
dtype in ("period[M]", "datetime64[ns]", "timedelta64[ns]") and NaT in data
449+
dtype in ("datetime64[ns]", "timedelta64[ns]") and NaT in data
450450
):
451451
mark = pytest.mark.xfail(
452452
reason="TODO StringArray.astype() with missing values #GH40566"

0 commit comments

Comments
 (0)