Skip to content

Commit bb321a4

Browse files
author
Chris Bertinato
committed
COMPAT: alias .to_numpy() for timestamp and timedelta scalars
1 parent 2448e52 commit bb321a4

File tree

7 files changed

+84
-4
lines changed

7 files changed

+84
-4
lines changed

Diff for: doc/source/whatsnew/v0.25.0.rst

+2-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ Backwards incompatible API changes
3333
Other API Changes
3434
^^^^^^^^^^^^^^^^^
3535

36-
-
36+
- ``Timestamp`` and ``Timedelta`` scalars now implement the :meth:`to_numpy` method as aliases to :meth:`Timestamp.to_datetime64` and :meth:`Timedelta.to_timedelta64`, respectively. (:issue:`24653`)
37+
3738
-
3839
-
3940

Diff for: pandas/_libs/tslibs/nattype.pyx

+20
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,26 @@ cdef class _NaT(datetime):
188188
"""
189189
return np.datetime64('NaT', 'ns')
190190

191+
def to_numpy(self, dtype=None, copy=False):
192+
"""
193+
Convert the Timestamp to a NumPy datetime64.
194+
195+
.. versionadded:: 0.25.0
196+
197+
This is an alias method for `Timestamp.to_datetime64()`. The dtype and
198+
copy parameters are available here only for compatibility. Their values
199+
will not affect the return value.
200+
201+
Returns
202+
-------
203+
numpy.datetime64
204+
205+
See Also
206+
--------
207+
DatetimeIndex.to_numpy : Similar method for DatetimeIndex.
208+
"""
209+
return self.to_datetime64()
210+
191211
def __repr__(self):
192212
return 'NaT'
193213

Diff for: pandas/_libs/tslibs/timedeltas.pyx

+20
Original file line numberDiff line numberDiff line change
@@ -824,6 +824,26 @@ cdef class _Timedelta(timedelta):
824824
""" Returns a numpy.timedelta64 object with 'ns' precision """
825825
return np.timedelta64(self.value, 'ns')
826826

827+
def to_numpy(self, dtype=None, copy=False):
828+
"""
829+
Convert the Timestamp to a NumPy timedelta64.
830+
831+
.. versionadded:: 0.25.0
832+
833+
This is an alias method for `Timedelta.to_timedelta64()`. The dtype and
834+
copy parameters are available here only for compatibility. Their values
835+
will not affect the return value.
836+
837+
Returns
838+
-------
839+
numpy.timedelta64
840+
841+
See Also
842+
--------
843+
Series.to_numpy : Similar method for Series.
844+
"""
845+
return self.to_timedelta64()
846+
827847
def total_seconds(self):
828848
"""
829849
Total duration of timedelta in seconds (to ns precision)

Diff for: pandas/_libs/tslibs/timestamps.pyx

+20
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,26 @@ cdef class _Timestamp(datetime):
345345
"""
346346
return np.datetime64(self.value, 'ns')
347347

348+
def to_numpy(self, dtype=None, copy=False):
349+
"""
350+
Convert the Timestamp to a NumPy datetime64.
351+
352+
.. versionadded:: 0.25.0
353+
354+
This is an alias method for `Timestamp.to_datetime64()`. The dtype and
355+
copy parameters are available here only for compatibility. Their values
356+
will not affect the return value.
357+
358+
Returns
359+
-------
360+
numpy.datetime64
361+
362+
See Also
363+
--------
364+
DatetimeIndex.to_numpy : Similar method for DatetimeIndex.
365+
"""
366+
return self.to_datetime64()
367+
348368
def __add__(self, other):
349369
cdef:
350370
int64_t other_int, nanos

Diff for: pandas/tests/scalar/test_nat.py

+12-3
Original file line numberDiff line numberDiff line change
@@ -201,9 +201,10 @@ def _get_overlap_public_nat_methods(klass, as_tuple=False):
201201
"fromtimestamp", "isocalendar", "isoformat", "isoweekday",
202202
"month_name", "now", "replace", "round", "strftime",
203203
"strptime", "time", "timestamp", "timetuple", "timetz",
204-
"to_datetime64", "to_pydatetime", "today", "toordinal",
205-
"tz_convert", "tz_localize", "tzname", "utcfromtimestamp",
206-
"utcnow", "utcoffset", "utctimetuple", "weekday"]),
204+
"to_datetime64", "to_numpy", "to_pydatetime", "today",
205+
"toordinal", "tz_convert", "tz_localize", "tzname",
206+
"utcfromtimestamp", "utcnow", "utcoffset", "utctimetuple",
207+
"weekday"]),
207208
(Timedelta, ["total_seconds"])
208209
])
209210
def test_overlap_public_nat_methods(klass, expected):
@@ -339,3 +340,11 @@ def test_nat_arithmetic_td64_vector(op_name, box):
339340
def test_nat_pinned_docstrings():
340341
# see gh-17327
341342
assert NaT.ctime.__doc__ == datetime.ctime.__doc__
343+
344+
345+
def test_to_numpy_alias():
346+
# GH 24653: alias .to_numpy() for scalars
347+
expected = NaT.to_datetime64()
348+
result = NaT.to_numpy()
349+
350+
assert np.isnat(expected) and np.isnat(result)

Diff for: pandas/tests/scalar/timedelta/test_timedelta.py

+5
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,11 @@ def test_timedelta_conversions(self):
414414
assert (Timedelta(timedelta(days=1)) ==
415415
np.timedelta64(1, 'D').astype('m8[ns]'))
416416

417+
def test_to_numpy_alias(self):
418+
# GH 24653: alias .to_numpy() for scalars
419+
td = Timedelta('10m7s')
420+
assert td.to_timedelta64() == td.to_numpy()
421+
417422
def test_round(self):
418423

419424
t1 = Timedelta('1 days 02:34:56.789123456')

Diff for: pandas/tests/scalar/timestamp/test_timestamp.py

+5
Original file line numberDiff line numberDiff line change
@@ -962,3 +962,8 @@ def test_to_period_tz_warning(self):
962962
with tm.assert_produces_warning(UserWarning):
963963
# warning that timezone info will be lost
964964
ts.to_period('D')
965+
966+
def test_to_numpy_alias(self):
967+
# GH 24653: alias .to_numpy() for scalars
968+
ts = Timestamp(datetime.now())
969+
assert ts.to_datetime64() == ts.to_numpy()

0 commit comments

Comments
 (0)