Skip to content

Commit 89eabba

Browse files
committed
BUG: explicity change nan -> NaT when assigning to datelike dtypes
1 parent 2f44c19 commit 89eabba

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

pandas/core/common.py

+9
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ class AmbiguousIndexError(PandasError, KeyError):
4242
_NS_DTYPE = np.dtype('M8[ns]')
4343
_TD_DTYPE = np.dtype('m8[ns]')
4444
_INT64_DTYPE = np.dtype(np.int64)
45+
_DATELIKE_DTYPES = set([ np.dtype(t) for t in ['M8[ns]','m8[ns]'] ])
4546

4647
def isnull(obj):
4748
"""Detect missing values (NaN in numeric arrays, None/NaN in object arrays)
@@ -718,6 +719,12 @@ def _infer_dtype_from_scalar(val):
718719
return dtype, val
719720

720721

722+
def _maybe_cast_scalar(dtype, value):
723+
""" if we a scalar value and are casting to a dtype that needs nan -> NaT conversion """
724+
if np.isscalar(value) and dtype in _DATELIKE_DTYPES and isnull(value):
725+
return tslib.iNaT
726+
return value
727+
721728
def _maybe_promote(dtype, fill_value=np.nan):
722729

723730
# if we passed an array here, determine the fill value by dtype
@@ -789,6 +796,7 @@ def _maybe_upcast_putmask(result, mask, other, dtype=None, change=None):
789796

790797
if mask.any():
791798

799+
other = _maybe_cast_scalar(result.dtype, other)
792800
def changeit():
793801

794802
# try to directly set by expanding our array to full
@@ -851,6 +859,7 @@ def _maybe_upcast_indexer(result, indexer, other, dtype=None):
851859
return the result and a changed flag
852860
"""
853861

862+
other = _maybe_cast_scalar(result.dtype, other)
854863
original_dtype = result.dtype
855864
def changeit():
856865
# our type is wrong here, need to upcast

pandas/tests/test_common.py

+19-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import nose
66
import unittest
77

8-
from pandas import Series, DataFrame, date_range, DatetimeIndex
8+
from pandas import Series, DataFrame, date_range, DatetimeIndex, Timestamp
99
from pandas.core.common import notnull, isnull
1010
import pandas.core.common as com
1111
import pandas.util.testing as tm
@@ -117,6 +117,24 @@ def test_datetimeindex_from_empty_datetime64_array():
117117
idx = DatetimeIndex(np.array([], dtype='datetime64[%s]' % unit))
118118
assert(len(idx) == 0)
119119

120+
def test_nan_to_nat_conversions():
121+
122+
df = DataFrame(dict({
123+
'A' : np.asarray(range(10),dtype='float64'),
124+
'B' : Timestamp('20010101') }))
125+
df.iloc[3:6,:] = np.nan
126+
result = df.loc[4,'B'].value
127+
assert(result == iNaT)
128+
129+
values = df['B'].values
130+
result, changed = com._maybe_upcast_indexer(values,tuple([slice(8,9)]),np.nan)
131+
assert(isnull(result[8]))
132+
133+
# numpy < 1.7.0 is wrong
134+
from distutils.version import LooseVersion
135+
if LooseVersion(np.__version__) >= '1.7.0':
136+
assert(result[8] == np.datetime64('NaT'))
137+
120138
def test_any_none():
121139
assert(com._any_none(1, 2, 3, None))
122140
assert(not com._any_none(1, 2, 3, 4))

0 commit comments

Comments
 (0)