Skip to content

Commit

Permalink
Fix insert NaT into tzaware datetime index
Browse files Browse the repository at this point in the history
  • Loading branch information
jbrockmendel committed Dec 20, 2017
1 parent 775099c commit 83e7105
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
3 changes: 2 additions & 1 deletion pandas/core/indexes/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -1776,7 +1776,8 @@ def insert(self, loc, item):

if isinstance(item, (datetime, np.datetime64)):
self._assert_can_do_op(item)
if not self._has_same_tz(item):
if not self._has_same_tz(item) and item is not self._na_value:
# GH#16537 allow pd.NaT through
raise ValueError(
'Passed item and index have different timezone')
# check freq can be preserved on edge cases
Expand Down
8 changes: 8 additions & 0 deletions pandas/tests/indexes/datetimes/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,14 @@ def test_where_tz(self):
expected = i2
tm.assert_index_equal(result, expected)

@pytest.mark.parametrize('tz', [None, 'UTC', 'US/Eastern'])
def test_insert_nat(self, tz):
# GH#16537
idx = pd.DatetimeIndex(['2017-01-01'], tz=tz)
res = idx.insert(0, pd.NaT)
expected = pd.DatetimeIndex(['NaT', '2017-01-01'], tz=tz)
tm.assert_index_equal(res, expected)

def test_insert(self):
idx = DatetimeIndex(
['2000-01-04', '2000-01-01', '2000-01-02'], name='idx')
Expand Down

0 comments on commit 83e7105

Please sign in to comment.