diff --git a/doc/source/whatsnew/v0.16.0.txt b/doc/source/whatsnew/v0.16.0.txt index 5ec0e90383f4c..1528747891c64 100644 --- a/doc/source/whatsnew/v0.16.0.txt +++ b/doc/source/whatsnew/v0.16.0.txt @@ -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 diff --git a/pandas/tseries/tests/test_tslib.py b/pandas/tseries/tests/test_tslib.py index 679fd2992855c..945458de22d2c 100644 --- a/pandas/tseries/tests/test_tslib.py +++ b/pandas/tseries/tests/test_tslib.py @@ -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() diff --git a/pandas/tslib.pyx b/pandas/tslib.pyx index 78a3a5d75cfd3..7cf7147a48d63 100644 --- a/pandas/tslib.pyx +++ b/pandas/tslib.pyx @@ -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