Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

BUG: Timestamp.__new__ doesnt preserve nanosecond #7610

Merged
merged 1 commit into from Jun 30, 2014
Jump to file or symbol
Failed to load files and symbols.
+37 −2
Split
View
@@ -255,7 +255,7 @@ Bug Fixes
- Bug in ``Timestamp.tz_localize`` resets ``nanosecond`` info (:issue:`7534`)
- Bug in ``DatetimeIndex.asobject`` raises ``ValueError`` when it contains ``NaT`` (:issue:`7539`)
-
+- Bug in ``Timestamp.__new__`` doesn't preserve nanosecond properly (:issue:`7610`)
- Bug in ``Index.astype(float)`` where it would return an ``object`` dtype
``Index`` (:issue:`7464`).
@@ -283,14 +283,48 @@ def test_timedelta_ns_based_arithmetic(self):
def test_timedelta_us_arithmetic(self):
self.assert_ns_timedelta(self.timestamp + np.timedelta64(-123, 'us'), -123000)
- def test_timedelta_ns_arithmetic(self):
+ def test_timedelta_ms_arithmetic(self):
time = self.timestamp + np.timedelta64(-123, 'ms')
self.assert_ns_timedelta(time, -123000000)
def test_nanosecond_string_parsing(self):
self.timestamp = Timestamp('2013-05-01 07:15:45.123456789')
self.assertEqual(self.timestamp.value, 1367392545123456000)
+ def test_nanosecond_timestamp(self):
+ # GH 7610
+ expected = 1293840000000000005
+ t = Timestamp('2011-01-01') + offsets.Nano(5)
+ self.assertEqual(repr(t), "Timestamp('2011-01-01 00:00:00.000000005')")
+ self.assertEqual(t.value, expected)
+ self.assertEqual(t.nanosecond, 5)
+
+ t = Timestamp(t)
+ self.assertEqual(repr(t), "Timestamp('2011-01-01 00:00:00.000000005')")
+ self.assertEqual(t.value, expected)
+ self.assertEqual(t.nanosecond, 5)
+
+ t = Timestamp(np.datetime64('2011-01-01 00:00:00.000000005Z'))
+ self.assertEqual(repr(t), "Timestamp('2011-01-01 00:00:00.000000005')")
+ self.assertEqual(t.value, expected)
+ self.assertEqual(t.nanosecond, 5)
+
+ expected = 1293840000000000010
+ t = t + offsets.Nano(5)
+ self.assertEqual(repr(t), "Timestamp('2011-01-01 00:00:00.000000010')")
+ self.assertEqual(t.value, expected)
+ self.assertEqual(t.nanosecond, 10)
+
+ t = Timestamp(t)
+ self.assertEqual(repr(t), "Timestamp('2011-01-01 00:00:00.000000010')")
+ self.assertEqual(t.value, expected)
+ self.assertEqual(t.nanosecond, 10)
+
+ t = Timestamp(np.datetime64('2011-01-01 00:00:00.000000010Z'))
+ self.assertEqual(repr(t), "Timestamp('2011-01-01 00:00:00.000000010')")
+ self.assertEqual(t.value, expected)
+ self.assertEqual(t.nanosecond, 10)
+
def test_nat_arithmetic(self):
# GH 6873
nat = tslib.NaT
View
@@ -956,6 +956,7 @@ cdef convert_to_tsobject(object ts, object tz, object unit):
if is_timestamp(ts):
obj.value += ts.nanosecond
+ obj.dts.ps = ts.nanosecond * 1000
@jreback

jreback Jun 30, 2014

Contributor

I think this might be a repr issue (and not a calc issue). as these tests all pass (for .value), but only Timestamp(t) doesn't have the correcvt repr (but the right value)

@sinhrks

sinhrks Jun 30, 2014

Member

__repr__ refers to nanosecond, not value.
https://github.com/pydata/pandas/blob/master/pandas/tslib.pyx#L252
And nanosecond set from dst.ps, not value. Thus dst.ps must be set properly before setting nanosecond.
https://github.com/pydata/pandas/blob/master/pandas/tslib.pyx#L213

t = pd.Timestamp('2011-01-01') + pd.offsets.Nano(5)
t = pd.Timestamp(t)
t.nanosecond
# 0 (NG)
_check_dts_bounds(&obj.dts)
return obj
elif PyDate_Check(ts):