Skip to content

Commit 2ada054

Browse files
sinhrksjreback
authored andcommitted
BUG: NaTType can be unpickled properly
1 parent 1c438e8 commit 2ada054

File tree

3 files changed

+23
-0
lines changed

3 files changed

+23
-0
lines changed

doc/source/release.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ API Changes
152152

153153
- all offset operations now return ``Timestamp`` types (rather than datetime), Business/Week frequencies were incorrect (:issue:`4069`)
154154

155+
155156
Deprecations
156157
~~~~~~~~~~~~
157158

@@ -297,6 +298,7 @@ Bug Fixes
297298
- Bug in resample when ``how=None`` resample freq is the same as the axis frequency (:issue:`5955`)
298299
- Bug in downcasting inference with empty arrays (:issue:`6733`)
299300
- Bug in ``obj.blocks`` on sparse containers dropping all but the last items of same for dtype (:issue:`6748`)
301+
- Bug in unpickling ``NaT (NaTType)`` (:issue:`4606`)
300302

301303
pandas 0.13.1
302304
-------------

pandas/tseries/tests/test_timeseries.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1802,6 +1802,21 @@ def test_period_resample_with_local_timezone(self):
18021802
expected = pd.Series(1, index=expected_index)
18031803
assert_series_equal(result, expected)
18041804

1805+
def test_pickle(self):
1806+
#GH4606
1807+
from pandas.compat import cPickle
1808+
import pickle
1809+
1810+
for pick in [pickle, cPickle]:
1811+
p = pick.loads(pick.dumps(NaT))
1812+
self.assertTrue(p is NaT)
1813+
1814+
idx = pd.to_datetime(['2013-01-01', NaT, '2014-01-06'])
1815+
idx_p = pick.loads(pick.dumps(idx))
1816+
self.assertTrue(idx_p[0] == idx[0])
1817+
self.assertTrue(idx_p[1] is NaT)
1818+
self.assertTrue(idx_p[2] == idx[2])
1819+
18051820

18061821
def _simple_ts(start, end, freq='D'):
18071822
rng = date_range(start, end, freq=freq)

pandas/tslib.pyx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -444,13 +444,19 @@ class NaTType(_NaT):
444444
def toordinal(self):
445445
return -1
446446

447+
def __reduce__(self):
448+
return (__nat_unpickle, (None, ))
449+
447450
fields = ['year', 'quarter', 'month', 'day', 'hour',
448451
'minute', 'second', 'microsecond', 'nanosecond',
449452
'week', 'dayofyear']
450453
for field in fields:
451454
prop = property(fget=lambda self: -1)
452455
setattr(NaTType, field, prop)
453456

457+
def __nat_unpickle(*args):
458+
# return constant defined in the module
459+
return NaT
454460

455461
NaT = NaTType()
456462

0 commit comments

Comments
 (0)