From 81b6a6387ce4a41625c6115833ca1497c4a98147 Mon Sep 17 00:00:00 2001 From: Chang She Date: Thu, 15 Nov 2012 15:57:44 -0500 Subject: [PATCH 1/2] BUG: loses nano precision when converting Timestamp objects #2252 --- pandas/src/datetime.pyx | 5 +++++ pandas/tseries/tests/test_timeseries.py | 8 ++++++++ 2 files changed, 13 insertions(+) diff --git a/pandas/src/datetime.pyx b/pandas/src/datetime.pyx index bdfa04eaca441..c3fdf82a76247 100644 --- a/pandas/src/datetime.pyx +++ b/pandas/src/datetime.pyx @@ -605,6 +605,9 @@ cpdef convert_to_tsobject(object ts, object tz=None): if obj.tzinfo is not None and not _is_utc(obj.tzinfo): offset = _get_utcoffset(obj.tzinfo, ts) obj.value -= _delta_to_nanoseconds(offset) + + if isinstance(ts, _Timestamp): + obj.value += ts.nanosecond _check_dts_bounds(obj.value, &obj.dts) return obj elif PyDate_Check(ts): @@ -810,6 +813,8 @@ def array_to_datetime(ndarray[object] values, raise_=False, dayfirst=False, 'utc=True') else: iresult[i] = _pydatetime_to_dts(val, &dts) + if isinstance(val, _Timestamp): + iresult[i] += val.nanosecond _check_dts_bounds(iresult[i], &dts) elif PyDate_Check(val): iresult[i] = _date_to_datetime64(val, &dts) diff --git a/pandas/tseries/tests/test_timeseries.py b/pandas/tseries/tests/test_timeseries.py index daaa86f681ee1..86feb68052f67 100644 --- a/pandas/tseries/tests/test_timeseries.py +++ b/pandas/tseries/tests/test_timeseries.py @@ -1333,6 +1333,14 @@ def test_to_period_nofreq(self): freq='infer') idx.to_period() + def test_000constructor_resolution(self): + #2252 + t1 = Timestamp((1352934390*1000000000)+1000000+1000+1) + idx = DatetimeIndex([t1]) + + self.assert_(idx.nanosecond[0] == t1.nanosecond) + + def test_constructor_coverage(self): rng = date_range('1/1/2000', periods=10.5) exp = date_range('1/1/2000', periods=10) From 3da43f72dd418001c5571fef2b7d3ddd90252cb0 Mon Sep 17 00:00:00 2001 From: Chang She Date: Thu, 15 Nov 2012 16:20:39 -0500 Subject: [PATCH 2/2] CLN: use is_timestamp instead of isinstance --- pandas/src/datetime.pyx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/src/datetime.pyx b/pandas/src/datetime.pyx index c3fdf82a76247..846c3f4d21380 100644 --- a/pandas/src/datetime.pyx +++ b/pandas/src/datetime.pyx @@ -606,7 +606,7 @@ cpdef convert_to_tsobject(object ts, object tz=None): offset = _get_utcoffset(obj.tzinfo, ts) obj.value -= _delta_to_nanoseconds(offset) - if isinstance(ts, _Timestamp): + if is_timestamp(ts): obj.value += ts.nanosecond _check_dts_bounds(obj.value, &obj.dts) return obj @@ -813,7 +813,7 @@ def array_to_datetime(ndarray[object] values, raise_=False, dayfirst=False, 'utc=True') else: iresult[i] = _pydatetime_to_dts(val, &dts) - if isinstance(val, _Timestamp): + if is_timestamp(val): iresult[i] += val.nanosecond _check_dts_bounds(iresult[i], &dts) elif PyDate_Check(val):