Skip to content

Commit 288059a

Browse files
Joe Jevnikjreback
Joe Jevnik
authored andcommitted
NaT.isoformat() returns 'NaT'.
NaT.isoformat() returning 'NaT' allows users to use Timestamp to rehydrate any _Timestamp object from its isoformat. This means that it is safe to use _Timestamp.isoformat() as a serialization format for timestamps where NaT is a valid value. Author: Joe Jevnik <[email protected]> Closes #12300 from llllllllll/nat-isoformat and squashes the following commits: eb34f07 [Joe Jevnik] ENH: NaT.isoformat() returns 'NaT'.
1 parent cac5f8b commit 288059a

File tree

4 files changed

+23
-4
lines changed

4 files changed

+23
-4
lines changed

doc/source/whatsnew/v0.18.0.txt

+7
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,8 @@ Backwards incompatible API changes
437437
- ``Series.head(0)`` and ``Series.tail(0)`` return empty series, rather than ``self``. (:issue:`11937`)
438438
- ``to_msgpack`` and ``read_msgpack`` encoding now defaults to ``'utf-8'``. (:issue:`12170`)
439439
- the order of keyword arguments to text file parsing functions (``.read_csv()``, ``.read_table()``, ``.read_fwf()``) changed to group related arguments. (:issue:`11555`)
440+
- ``NaTType.isoformat`` now returns the string ``'NaT`` to allow the result to
441+
be passed to the constructor of ``Timestamp``. (:issue:`12300`)
440442

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

509511

512+
``NaT.isoformat()`` now returns ``'NaT'``. This change allows allows
513+
``pd.Timestamp`` to rehydrate any timestamp like object from its isoformat
514+
(:issue:`12300`).
515+
516+
510517
Signature change for .rank
511518
^^^^^^^^^^^^^^^^^^^^^^^^^^
512519

pandas/tests/test_format.py

+5
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from pandas.compat import range, zip, lrange, StringIO, PY3, lzip, u
1111
import pandas.compat as compat
1212
import itertools
13+
from operator import methodcaller
1314
import os
1415
import sys
1516
from textwrap import dedent
@@ -4083,6 +4084,10 @@ def test_tz_dateutil(self):
40834084
dt_datetime_us = datetime(2013, 1, 2, 12, 1, 3, 45, tzinfo=utc)
40844085
self.assertEqual(str(dt_datetime_us), str(Timestamp(dt_datetime_us)))
40854086

4087+
def test_nat_representations(self):
4088+
for f in (str, repr, methodcaller('isoformat')):
4089+
self.assertEqual(f(pd.NaT), 'NaT')
4090+
40864091

40874092
if __name__ == '__main__':
40884093
nose.runmodule(argv=[__file__, '-vvs', '-x', '--pdb', '--pdb-failure'],

pandas/tseries/tests/test_timeseries.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -973,9 +973,9 @@ def test_NaT_methods(self):
973973
# GH 9513
974974
raise_methods = ['astimezone', 'combine', 'ctime', 'dst',
975975
'fromordinal', 'fromtimestamp', 'isocalendar',
976-
'isoformat', 'strftime', 'strptime', 'time',
977-
'timestamp', 'timetuple', 'timetz', 'toordinal',
978-
'tzname', 'utcfromtimestamp', 'utcnow', 'utcoffset',
976+
'strftime', 'strptime', 'time', 'timestamp',
977+
'timetuple', 'timetz', 'toordinal', 'tzname',
978+
'utcfromtimestamp', 'utcnow', 'utcoffset',
979979
'utctimetuple']
980980
nat_methods = ['date', 'now', 'replace', 'to_datetime', 'today']
981981
nan_methods = ['weekday', 'isoweekday']
@@ -992,6 +992,9 @@ def test_NaT_methods(self):
992992
if hasattr(NaT, method):
993993
self.assertIs(getattr(NaT, method)(), NaT)
994994

995+
# GH 12300
996+
self.assertEqual(NaT.isoformat(), 'NaT')
997+
995998
def test_to_datetime_types(self):
996999

9971000
# empty string

pandas/tslib.pyx

+5-1
Original file line numberDiff line numberDiff line change
@@ -673,6 +673,10 @@ class NaTType(_NaT):
673673
def __str__(self):
674674
return 'NaT'
675675

676+
def isoformat(self, sep='T'):
677+
# This allows Timestamp(ts.isoformat()) to always correctly roundtrip.
678+
return 'NaT'
679+
676680
def __hash__(self):
677681
return NPY_NAT
678682

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

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

739-
_implemented_methods = ['to_datetime64']
743+
_implemented_methods = ['to_datetime64', 'isoformat']
740744
_implemented_methods.extend(_nat_methods)
741745
_implemented_methods.extend(_nan_methods)
742746

0 commit comments

Comments
 (0)