Skip to content
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

Dont check for NaTType, just NaT #17564

Merged
merged 4 commits into from
Sep 23, 2017
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion pandas/core/indexes/timedeltas.py
Original file line number Diff line number Diff line change
Expand Up @@ -847,7 +847,7 @@ def insert(self, loc, item):
pass

freq = None
if isinstance(item, (Timedelta, libts.NaTType)):
if isinstance(item, Timedelta) or item is NaT:

# check freq can be preserved on edge cases
if self.freq is not None:
Expand Down
5 changes: 2 additions & 3 deletions pandas/io/packers.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@
Index, MultiIndex, Float64Index, Int64Index,
Panel, RangeIndex, PeriodIndex, DatetimeIndex, NaT,
Categorical, CategoricalIndex)
from pandas._libs.tslib import NaTType
from pandas.core.sparse.api import SparseSeries, SparseDataFrame
from pandas.core.sparse.array import BlockIndex, IntIndex
from pandas.core.generic import NDFrame
Expand Down Expand Up @@ -470,7 +469,7 @@ def encode(obj):
}

elif isinstance(obj, (datetime, date, np.datetime64, timedelta,
np.timedelta64, NaTType)):
Copy link
Contributor

Choose a reason for hiding this comment

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

FYI:

In [40]: np.timedelta64('nat') is pd.NaT
Out[40]: False

In [41]: pd.Timedelta(np.timedelta64('nat')) is pd.NaT
Out[41]: True

this is reason for the complex type checking around NaTs; numpy has a 'nat' like value embedded in np.timedelta64/np.datetime64 (however its not very useful).

np.timedelta64)) or obj is NaT:
if isinstance(obj, Timestamp):
tz = obj.tzinfo
if tz is not None:
Expand All @@ -482,7 +481,7 @@ def encode(obj):
u'value': obj.value,
u'freq': freq,
u'tz': tz}
if isinstance(obj, NaTType):
if obj is NaT:
return {u'typ': u'nat'}
elif isinstance(obj, np.timedelta64):
return {u'typ': u'timedelta64',
Expand Down
6 changes: 3 additions & 3 deletions pandas/tests/scalar/test_timedelta.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from pandas.core.tools.timedeltas import _coerce_scalar_to_timedelta_type as ct
from pandas import (Timedelta, TimedeltaIndex, timedelta_range, Series,
to_timedelta, compat)
from pandas._libs.tslib import iNaT, NaTType
from pandas._libs.tslib import iNaT


class TestTimedeltas(object):
Expand Down Expand Up @@ -572,7 +572,7 @@ def test_implementation_limits(self):
assert max_td.value == np.iinfo(np.int64).max

# Beyond lower limit, a NAT before the Overflow
assert isinstance(min_td - Timedelta(1, 'ns'), NaTType)
assert (min_td - Timedelta(1, 'ns')) is NaT

with pytest.raises(OverflowError):
min_td - Timedelta(2, 'ns')
Expand All @@ -582,7 +582,7 @@ def test_implementation_limits(self):

# Same tests using the internal nanosecond values
td = Timedelta(min_td.value - 1, 'ns')
assert isinstance(td, NaTType)
assert td is NaT

with pytest.raises(OverflowError):
Timedelta(min_td.value - 2, 'ns')
Expand Down