Skip to content

Commit e27ad0f

Browse files
author
Chris Bertinato
committed
COMPAT: alias .to_numpy() for timestamp and timedelta scalars
1 parent 2e38d55 commit e27ad0f

File tree

5 files changed

+27
-0
lines changed

5 files changed

+27
-0
lines changed

doc/source/whatsnew/v0.24.2.rst

+6
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@ Whats New in 0.24.2 (February XX, 2019)
1515
These are the changes in pandas 0.24.2. See :ref:`release` for a full changelog
1616
including other versions of pandas.
1717

18+
.. _whatsnew_0242.api:
19+
20+
API Changes
21+
~~~~~~~~~~~
22+
- ``Timestamp`` and ``Timedelta`` scalars now implement the :meth:`to_numpy` method as aliases to :meth:`Timestamp.to_datetime64` and :meth:`Timedelta.to_timedelta64`, respectively.
23+
1824
.. _whatsnew_0242.regressions:
1925

2026
Fixed Regressions

pandas/_libs/tslibs/timedeltas.pyx

+4
Original file line numberDiff line numberDiff line change
@@ -824,6 +824,10 @@ 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+
""" Alias to to_timedelta64 for Timedelta scalars """
829+
return self.to_timedelta64()
830+
827831
def total_seconds(self):
828832
"""
829833
Total duration of timedelta in seconds (to ns precision)

pandas/_libs/tslibs/timestamps.pyx

+4
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,10 @@ cdef class _Timestamp(datetime):
343343
""" Returns a numpy.datetime64 object with 'ns' precision """
344344
return np.datetime64(self.value, 'ns')
345345

346+
def to_numpy(self, dtype=None, copy=False):
347+
""" Alias to to_datetime64 for Timestamp scalars """
348+
return self.to_datetime64()
349+
346350
def __add__(self, other):
347351
cdef:
348352
int64_t other_int, nanos

pandas/tests/arithmetic/test_datetime64.py

+7
Original file line numberDiff line numberDiff line change
@@ -2336,3 +2336,10 @@ def test_shift_months(years, months):
23362336
for x in dti]
23372337
expected = DatetimeIndex(raw)
23382338
tm.assert_index_equal(actual, expected)
2339+
2340+
2341+
def test_to_numpy_alias():
2342+
# GH 24653: alias .to_numpy() for scalars
2343+
ts = pd.Timestamp(datetime.now())
2344+
assert ts.to_datetime64() == ts.to_numpy()
2345+

pandas/tests/arithmetic/test_timedelta64.py

+6
Original file line numberDiff line numberDiff line change
@@ -1975,3 +1975,9 @@ def test_td64arr_pow_invalid(self, scalar_td, box_with_array):
19751975

19761976
with pytest.raises(TypeError, match=pattern):
19771977
td1 ** scalar_td
1978+
1979+
1980+
def test_to_numpy_alias():
1981+
# GH 24653: alias .to_numpy() for scalars
1982+
td = Timedelta('10m7s')
1983+
assert td.to_timedelta64() == td.to_numpy()

0 commit comments

Comments
 (0)