Skip to content

Commit 3256b57

Browse files
Joe Jevnikjreback
Joe Jevnik
authored andcommitted
BUG: fix NaT support for msgpack format.
closes #12307
1 parent 370f45f commit 3256b57

File tree

3 files changed

+13
-4
lines changed

3 files changed

+13
-4
lines changed

doc/source/whatsnew/v0.18.0.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -964,7 +964,7 @@ Bug Fixes
964964

965965
- Bug in ``.loc`` setitem indexer preventing the use of a TZ-aware DatetimeIndex (:issue:`12050`)
966966
- Bug in ``.style`` indexes and multi-indexes not appearing (:issue:`11655`)
967-
967+
- Bug in ``to_msgpack`` and ``from_msgpack`` which did not correctly serialize or deserialize ``NaT`` (:issue:`12307`).
968968
- Bug in ``.skew`` and ``.kurt`` due to roundoff error for highly similar values (:issue:`11974`)
969969

970970
- Bug in ``buffer_rd_bytes`` src->buffer could be freed more than once if reading failed, causing a segfault (:issue:`12098`)

pandas/io/packers.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@
4747
from pandas.compat import u
4848
from pandas import (Timestamp, Period, Series, DataFrame, # noqa
4949
Index, MultiIndex, Float64Index, Int64Index,
50-
Panel, RangeIndex, PeriodIndex, DatetimeIndex)
50+
Panel, RangeIndex, PeriodIndex, DatetimeIndex, NaT)
51+
from pandas.tslib import NaTType
5152
from pandas.sparse.api import SparseSeries, SparseDataFrame, SparsePanel
5253
from pandas.sparse.array import BlockIndex, IntIndex
5354
from pandas.core.generic import NDFrame
@@ -383,7 +384,7 @@ def encode(obj):
383384
} for b in data.blocks]}
384385

385386
elif isinstance(obj, (datetime, date, np.datetime64, timedelta,
386-
np.timedelta64)):
387+
np.timedelta64, NaTType)):
387388
if isinstance(obj, Timestamp):
388389
tz = obj.tzinfo
389390
if tz is not None:
@@ -395,6 +396,8 @@ def encode(obj):
395396
'value': obj.value,
396397
'offset': offset,
397398
'tz': tz}
399+
if isinstance(obj, NaTType):
400+
return {'typ': 'nat'}
398401
elif isinstance(obj, np.timedelta64):
399402
return {'typ': 'timedelta64',
400403
'data': obj.view('i8')}
@@ -462,6 +465,8 @@ def decode(obj):
462465
return obj
463466
elif typ == 'timestamp':
464467
return Timestamp(obj['value'], tz=obj['tz'], offset=obj['offset'])
468+
elif typ == 'nat':
469+
return NaT
465470
elif typ == 'period':
466471
return Period(ordinal=obj['ordinal'], freq=obj['freq'])
467472
elif typ == 'index':

pandas/io/tests/test_packers.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
from pandas.tests.test_panel import assert_panel_equal
1919

2020
import pandas
21-
from pandas import Timestamp, tslib
21+
from pandas import Timestamp, NaT, tslib
2222

2323
nan = np.nan
2424

@@ -225,6 +225,10 @@ def test_timestamp(self):
225225
i_rec = self.encode_decode(i)
226226
self.assertEqual(i, i_rec)
227227

228+
def test_nat(self):
229+
nat_rec = self.encode_decode(NaT)
230+
self.assertIs(NaT, nat_rec)
231+
228232
def test_datetimes(self):
229233

230234
# fails under 2.6/win32 (np.datetime64 seems broken)

0 commit comments

Comments
 (0)