-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
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
Changes from 3 commits
83e7105
5a469d2
664c800
85ba78c
3d429df
a1df0f7
31e3581
8934f66
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. comment not needed There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will change, but sure we don't want the issue reference? |
||
raise ValueError( | ||
'Passed item and index have different timezone') | ||
# check freq can be preserved on edge cases | ||
|
Original file line number | Diff line number | Diff line change | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -46,6 +46,15 @@ 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): | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You could maybe parametrize over different NA values here, since this should work for inserting In [2]: pd.date_range('20170101', periods=2).insert(0, np.nan)
Out[2]: DatetimeIndex(['NaT', '2017-01-01', '2017-01-02'], dtype='datetime64[ns]', freq=None)
In [3]: pd.date_range('20170101', periods=2).insert(0, None)
Out[3]: DatetimeIndex(['NaT', '2017-01-01', '2017-01-02'], dtype='datetime64[ns]', freq=None) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you do that, I suppose you could delete these lines in pandas/pandas/tests/indexes/datetimes/test_indexing.py Lines 148 to 153 in ac8ac15
|
||||||||||||||
# GH#16537, GH#18295 (test missing) | ||||||||||||||
idx = pd.DatetimeIndex(['2017-01-01'], tz=tz) | ||||||||||||||
expected = pd.DatetimeIndex(['NaT', '2017-01-01'], tz=tz) | ||||||||||||||
for null in [None, np.nan, pd.NaT]: | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you pull this null into the paramaterization? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure, pushing momentarily. |
||||||||||||||
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') | ||||||||||||||
|
@@ -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') | ||||||||||||||
|
||||||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
notnull(item)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will change, but FYI they are equivalent here. notnull(item) was checked above in which case it was replaced with self._na_value.