Skip to content

Commit 83e7105

Browse files
committed
Fix insert NaT into tzaware datetime index
closes pandas-dev#16357
1 parent 775099c commit 83e7105

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

pandas/core/indexes/datetimes.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1776,7 +1776,8 @@ def insert(self, loc, item):
17761776

17771777
if isinstance(item, (datetime, np.datetime64)):
17781778
self._assert_can_do_op(item)
1779-
if not self._has_same_tz(item):
1779+
if not self._has_same_tz(item) and item is not self._na_value:
1780+
# GH#16537 allow pd.NaT through
17801781
raise ValueError(
17811782
'Passed item and index have different timezone')
17821783
# check freq can be preserved on edge cases

pandas/tests/indexes/datetimes/test_indexing.py

+8
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,14 @@ def test_where_tz(self):
4646
expected = i2
4747
tm.assert_index_equal(result, expected)
4848

49+
@pytest.mark.parametrize('tz', [None, 'UTC', 'US/Eastern'])
50+
def test_insert_nat(self, tz):
51+
# GH#16537
52+
idx = pd.DatetimeIndex(['2017-01-01'], tz=tz)
53+
res = idx.insert(0, pd.NaT)
54+
expected = pd.DatetimeIndex(['NaT', '2017-01-01'], tz=tz)
55+
tm.assert_index_equal(res, expected)
56+
4957
def test_insert(self):
5058
idx = DatetimeIndex(
5159
['2000-01-04', '2000-01-01', '2000-01-02'], name='idx')

0 commit comments

Comments
 (0)