Skip to content

Commit

Permalink
NaT.isoformat() returns 'NaT'.
Browse files Browse the repository at this point in the history
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 <joe@quantopian.com>

Closes #12300 from llllllllll/nat-isoformat and squashes the following commits:

eb34f07 [Joe Jevnik] ENH: NaT.isoformat() returns 'NaT'.
  • Loading branch information
Joe Jevnik authored and jreback committed Feb 15, 2016
1 parent cac5f8b commit 288059a
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 4 deletions.
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

0 comments on commit 288059a

Please sign in to comment.