Skip to content

NaT.isoformat() returns 'NaT'. #12300

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

Closed
wants to merge 1 commit into from
Closed
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
7 changes: 7 additions & 0 deletions doc/source/whatsnew/v0.18.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,8 @@ Backwards incompatible API changes
- ``Series.head(0)`` and ``Series.tail(0)`` return empty series, rather than ``self``. (:issue:`11937`)
- ``to_msgpack`` and ``read_msgpack`` encoding now defaults to ``'utf-8'``. (:issue:`12170`)
- the order of keyword arguments to text file parsing functions (``.read_csv()``, ``.read_table()``, ``.read_fwf()``) changed to group related arguments. (:issue:`11555`)
- ``NaTType.isoformat`` now returns the string ``'NaT`` to allow the result to
be passed to the constructor of ``Timestamp``. (:issue:`12300`)

NaT and Timedelta operations
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand Down Expand Up @@ -507,6 +509,11 @@ Subtraction by ``Timedelta`` in a ``Series`` by a ``Timestamp`` works (:issue:`1
pd.Timestamp('2012-01-01') - ser


``NaT.isoformat()`` now returns ``'NaT'``. This change allows allows
``pd.Timestamp`` to rehydrate any timestamp like object from its isoformat
(:issue:`12300`).


Signature change for .rank
^^^^^^^^^^^^^^^^^^^^^^^^^^

Expand Down
5 changes: 5 additions & 0 deletions pandas/tests/test_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from pandas.compat import range, zip, lrange, StringIO, PY3, lzip, u
import pandas.compat as compat
import itertools
from operator import methodcaller
import os
import sys
from textwrap import dedent
Expand Down Expand Up @@ -4083,6 +4084,10 @@ def test_tz_dateutil(self):
dt_datetime_us = datetime(2013, 1, 2, 12, 1, 3, 45, tzinfo=utc)
self.assertEqual(str(dt_datetime_us), str(Timestamp(dt_datetime_us)))

def test_nat_representations(self):
for f in (str, repr, methodcaller('isoformat')):
self.assertEqual(f(pd.NaT), 'NaT')


if __name__ == '__main__':
nose.runmodule(argv=[__file__, '-vvs', '-x', '--pdb', '--pdb-failure'],
Expand Down
9 changes: 6 additions & 3 deletions pandas/tseries/tests/test_timeseries.py
Original file line number Diff line number Diff line change
Expand Up @@ -973,9 +973,9 @@ def test_NaT_methods(self):
# GH 9513
raise_methods = ['astimezone', 'combine', 'ctime', 'dst',
'fromordinal', 'fromtimestamp', 'isocalendar',
'isoformat', 'strftime', 'strptime', 'time',
'timestamp', 'timetuple', 'timetz', 'toordinal',
'tzname', 'utcfromtimestamp', 'utcnow', 'utcoffset',
'strftime', 'strptime', 'time', 'timestamp',
'timetuple', 'timetz', 'toordinal', 'tzname',
'utcfromtimestamp', 'utcnow', 'utcoffset',
'utctimetuple']
nat_methods = ['date', 'now', 'replace', 'to_datetime', 'today']
nan_methods = ['weekday', 'isoweekday']
Expand All @@ -992,6 +992,9 @@ def test_NaT_methods(self):
if hasattr(NaT, method):
self.assertIs(getattr(NaT, method)(), NaT)

# GH 12300
self.assertEqual(NaT.isoformat(), 'NaT')

def test_to_datetime_types(self):

# empty string
Expand Down
6 changes: 5 additions & 1 deletion pandas/tslib.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -673,6 +673,10 @@ class NaTType(_NaT):
def __str__(self):
return 'NaT'

def isoformat(self, sep='T'):
# This allows Timestamp(ts.isoformat()) to always correctly roundtrip.
return 'NaT'

def __hash__(self):
return NPY_NAT

Expand Down Expand Up @@ -736,7 +740,7 @@ _nat_methods = ['date', 'now', 'replace', 'to_datetime', 'today']

_nan_methods = ['weekday', 'isoweekday', 'total_seconds']

_implemented_methods = ['to_datetime64']
_implemented_methods = ['to_datetime64', 'isoformat']
_implemented_methods.extend(_nat_methods)
_implemented_methods.extend(_nan_methods)

Expand Down