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

Conversation

jbrockmendel
Copy link
Member

@@ -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):
Copy link
Member

@jschendel jschendel Dec 21, 2017

Choose a reason for hiding this comment

The 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 np.nan and None as well (shouldn't require any additional code changes). For example, on a tz-naive DTI:

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)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you do that, I suppose you could delete these lines in test_insert below, as to not duplicate things:

# 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)

@@ -294,6 +294,7 @@ Indexing
- Bug in tz-aware :class:`DatetimeIndex` where addition/subtraction with a :class:`TimedeltaIndex` or array with ``dtype='timedelta64[ns]'`` was incorrect (:issue:`17558`)
- :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 :class:`DatetimeIndex.insert` where inserting ``pd.NaT`` into a timezone-aware index incorrectly raised (:issue:`16357`)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think :class: should be :func: or :meth: instead, and it looks like previous convention has been to just use ``NaT`` with the pd. omitted.

@jreback jreback changed the title Fix DatetimeIndex.insert(pd.NaT) for tz-aware index Fix DatetimeIndex.insert(pd.NaT) for tz-aware index Dec 21, 2017
@jreback jreback added Bug Missing-data np.nan, pd.NaT, pd.NA, dropna, isnull, interpolate Timezones Timezone data dtype labels Dec 21, 2017
@@ -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:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

notnull(item)

Copy link
Member Author

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.

@@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

comment not needed

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will change, but sure we don't want the issue reference?

@codecov
Copy link

codecov bot commented Dec 24, 2017

Codecov Report

Merging #18883 into master will decrease coverage by 0.02%.
The diff coverage is n/a.

Impacted file tree graph

@@            Coverage Diff             @@
##           master   #18883      +/-   ##
==========================================
- Coverage   91.59%   91.56%   -0.03%     
==========================================
  Files         150      150              
  Lines       48959    48967       +8     
==========================================
- Hits        44843    44836       -7     
- Misses       4116     4131      +15
Flag Coverage Δ
#multiple 89.92% <ø> (-0.03%) ⬇️
#single 41.73% <ø> (+0.59%) ⬆️
Impacted Files Coverage Δ
pandas/core/indexes/datetimes.py 95.45% <ø> (-0.17%) ⬇️
pandas/plotting/_converter.py 65.22% <0%> (-1.74%) ⬇️
pandas/core/indexes/interval.py 92.61% <0%> (-1.28%) ⬇️
pandas/core/sparse/array.py 91.82% <0%> (-0.47%) ⬇️
pandas/core/indexes/timedeltas.py 91.08% <0%> (-0.18%) ⬇️
pandas/core/dtypes/cast.py 88.42% <0%> (-0.18%) ⬇️
pandas/core/indexes/datetimelike.py 97.04% <0%> (-0.09%) ⬇️
pandas/core/indexes/numeric.py 97.28% <0%> (-0.09%) ⬇️
pandas/core/series.py 94.61% <0%> (-0.05%) ⬇️
... and 14 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update cdebcf3...8934f66. Read the comment docs.

@jbrockmendel
Copy link
Member Author

Easy win without whatsnew conflicts.

Copy link
Contributor

@jreback jreback left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

small change, otherwise lgtm. ping on green.

# 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]:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you pull this null into the paramaterization?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, pushing momentarily.

@jreback jreback added this to the 0.23.0 milestone Dec 29, 2017
@jbrockmendel
Copy link
Member Author

ping.

@jreback jreback merged commit 10edfd0 into pandas-dev:master Dec 29, 2017
@jreback
Copy link
Contributor

jreback commented Dec 29, 2017

thanks!

@jbrockmendel jbrockmendel deleted the insert_nat branch December 29, 2017 16:29
hexgnu pushed a commit to hexgnu/pandas that referenced this pull request Jan 1, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Missing-data np.nan, pd.NaT, pd.NA, dropna, isnull, interpolate Timezones Timezone data dtype
Projects
None yet
Development

Successfully merging this pull request may close these issues.

BUG: insert NaT into tz-aware DTI.
3 participants