Skip to content

Fix DatetimeIndex.insert(pd.NaT) for tz-aware index #18883

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Dec 29, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.23.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,7 @@ Indexing
- :func:`Index.to_series` now accepts ``index`` and ``name`` kwargs (:issue:`18699`)
- :func:`DatetimeIndex.to_series` now accepts ``index`` and ``name`` kwargs (:issue:`18699`)
- Bug in indexing non-scalar value from ``Series`` having non-unique ``Index`` will return value flattened (:issue:`17610`)
- Bug in :func:`DatetimeIndex.insert` where inserting ``NaT`` into a timezone-aware index incorrectly raised (:issue:`16357`)


I/O
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/indexes/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -1775,7 +1775,7 @@ 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 not isna(item):
raise ValueError(
'Passed item and index have different timezone')
# check freq can be preserved on edge cases
Expand Down
16 changes: 9 additions & 7 deletions pandas/tests/indexes/datetimes/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,15 @@ def test_where_tz(self):
expected = i2
tm.assert_index_equal(result, expected)

@pytest.mark.parametrize('null', [None, np.nan, pd.NaT])
@pytest.mark.parametrize('tz', [None, 'UTC', 'US/Eastern'])
def test_insert_nat(self, tz, null):
# GH#16537, GH#18295 (test missing)
idx = pd.DatetimeIndex(['2017-01-01'], tz=tz)
expected = pd.DatetimeIndex(['NaT', '2017-01-01'], tz=tz)
res = idx.insert(0, null)
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 Expand Up @@ -145,13 +154,6 @@ def test_insert(self):
assert result.tz == expected.tz
assert result.freq is None

# GH 18295 (test missing)
expected = DatetimeIndex(
['20170101', pd.NaT, '20170102', '20170103', '20170104'])
for na in (np.nan, pd.NaT, None):
result = date_range('20170101', periods=4).insert(1, na)
tm.assert_index_equal(result, expected)

def test_delete(self):
idx = date_range(start='2000-01-01', periods=5, freq='M', name='idx')

Expand Down