Skip to content

Commit bbddca4

Browse files
committed
Merge pull request #9255 from shoyer/to_datetime64
ENH: add Timestamp.to_datetime64
2 parents 779fbaa + 593d239 commit bbddca4

File tree

3 files changed

+20
-0
lines changed

3 files changed

+20
-0
lines changed

doc/source/whatsnew/v0.16.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ Enhancements
6363
- Paths beginning with ~ will now be expanded to begin with the user's home directory (:issue:`9066`)
6464
- Added time interval selection in get_data_yahoo (:issue:`9071`)
6565
- Added ``Series.str.slice_replace()``, which previously raised NotImplementedError (:issue:`8888`)
66+
- Added ``Timestamp.to_datetime64()`` to complement ``Timedelta.to_timedelta64()`` (:issue:`9255`)
6667

6768

6869
Performance

pandas/tseries/tests/test_tslib.py

+15
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,21 @@ def test_constructor_with_stringoffset(self):
136136
self.assertEqual(repr(result), expected_repr)
137137
self.assertEqual(result, eval(repr(result)))
138138

139+
def test_conversion(self):
140+
# GH 9255
141+
ts = Timestamp('2000-01-01')
142+
143+
result = ts.to_pydatetime()
144+
expected = datetime.datetime(2000, 1, 1)
145+
self.assertEqual(result, expected)
146+
self.assertEqual(type(result), type(expected))
147+
148+
result = ts.to_datetime64()
149+
expected = np.datetime64(ts.value, 'ns')
150+
self.assertEqual(result, expected)
151+
self.assertEqual(type(result), type(expected))
152+
self.assertEqual(result.dtype, expected.dtype)
153+
139154
def test_repr(self):
140155
tm._skip_if_no_pytz()
141156
tm._skip_if_no_dateutil()

pandas/tslib.pyx

+4
Original file line numberDiff line numberDiff line change
@@ -852,6 +852,10 @@ cdef class _Timestamp(datetime):
852852
dts.hour, dts.min, dts.sec,
853853
dts.us, ts.tzinfo)
854854

855+
cpdef to_datetime64(self):
856+
""" Returns a numpy.datetime64 object with 'ns' precision """
857+
return np.datetime64(self.value, 'ns')
858+
855859
def __add__(self, other):
856860
cdef int64_t other_int
857861

0 commit comments

Comments
 (0)