Skip to content

BUG: Timestamp.__new__ doesnt preserve nanosecond #7610

New issue

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

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 30, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/source/v0.14.1.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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`).
Expand Down
36 changes: 35 additions & 1 deletion pandas/tseries/tests/test_tslib.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions pandas/tslib.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

__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):
Expand Down