Skip to content

ENH: add Timestamp.to_datetime64 #9255

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
Jan 15, 2015
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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.16.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ Enhancements
- Paths beginning with ~ will now be expanded to begin with the user's home directory (:issue:`9066`)
- Added time interval selection in get_data_yahoo (:issue:`9071`)
- Added ``Series.str.slice_replace()``, which previously raised NotImplementedError (:issue:`8888`)
- Added ``Timestamp.to_datetime64()`` to complement ``Timedelta.to_timedelta64()`` (:issue:`9255`)


Performance
Expand Down
15 changes: 15 additions & 0 deletions pandas/tseries/tests/test_tslib.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,21 @@ def test_constructor_with_stringoffset(self):
self.assertEqual(repr(result), expected_repr)
self.assertEqual(result, eval(repr(result)))

def test_conversion(self):
# GH 9255
ts = Timestamp('2000-01-01')

result = ts.to_pydatetime()
expected = datetime.datetime(2000, 1, 1)
self.assertEqual(result, expected)
self.assertEqual(type(result), type(expected))

result = ts.to_datetime64()
expected = np.datetime64(ts.value, 'ns')
self.assertEqual(result, expected)
self.assertEqual(type(result), type(expected))
self.assertEqual(result.dtype, expected.dtype)

def test_repr(self):
tm._skip_if_no_pytz()
tm._skip_if_no_dateutil()
Expand Down
4 changes: 4 additions & 0 deletions pandas/tslib.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -852,6 +852,10 @@ cdef class _Timestamp(datetime):
dts.hour, dts.min, dts.sec,
dts.us, ts.tzinfo)

cpdef to_datetime64(self):
""" Returns a numpy.datetime64 object with 'ns' precision """
return np.datetime64(self.value, 'ns')

def __add__(self, other):
cdef int64_t other_int

Expand Down