Skip to content

Commit 6cc36a4

Browse files
author
Chris Bertinato
committed
Added doc strings and to_numpy for NaT
1 parent b98f7ed commit 6cc36a4

File tree

10 files changed

+65
-25
lines changed

10 files changed

+65
-25
lines changed

doc/source/whatsnew/v0.24.2.rst

-6
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,6 @@ 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-
2418
.. _whatsnew_0242.regressions:
2519

2620
Fixed Regressions

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.
37+
3738
-
3839
-
3940

pandas/_libs/tslibs/nattype.pyx

+16
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,22 @@ cdef class _NaT(datetime):
186186
""" Returns a numpy.datetime64 object with 'ns' precision """
187187
return np.datetime64('NaT', 'ns')
188188

189+
def to_numpy(self, dtype=None, copy=False):
190+
"""
191+
Convert NaT to a NumPy datetime64 NaT.
192+
193+
.. versionadded:: 0.25.0
194+
195+
This is an alias method for `NaT.to_datetime64()`. The dtype and
196+
copy parameters are available here only for compatibility. Their values
197+
will not affect the return value.
198+
199+
Returns
200+
-------
201+
numpy.datetime64('NaT')
202+
"""
203+
return self.to_datetime64()
204+
189205
def __repr__(self):
190206
return 'NaT'
191207

pandas/_libs/tslibs/timedeltas.pyx

+17-1
Original file line numberDiff line numberDiff line change
@@ -825,7 +825,23 @@ cdef class _Timedelta(timedelta):
825825
return np.timedelta64(self.value, 'ns')
826826

827827
def to_numpy(self, dtype=None, copy=False):
828-
""" Alias to to_timedelta64 for Timedelta scalars """
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+
"""
829845
return self.to_timedelta64()
830846

831847
def total_seconds(self):

pandas/_libs/tslibs/timestamps.pyx

+17-1
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,23 @@ cdef class _Timestamp(datetime):
344344
return np.datetime64(self.value, 'ns')
345345

346346
def to_numpy(self, dtype=None, copy=False):
347-
""" Alias to to_datetime64 for Timestamp scalars """
347+
"""
348+
Convert the Timestamp to a NumPy datetime64.
349+
350+
.. versionadded:: 0.25.0
351+
352+
This is an alias method for `Timestamp.to_datetime64()`. The dtype and
353+
copy parameters are available here only for compatibility. Their values
354+
will not affect the return value.
355+
356+
Returns
357+
-------
358+
numpy.datetime64
359+
360+
See Also
361+
--------
362+
DatetimeIndex.to_numpy : Similar method for DatetimeIndex.
363+
"""
348364
return self.to_datetime64()
349365

350366
def __add__(self, other):

pandas/tests/arithmetic/test_datetime64.py

-6
Original file line numberDiff line numberDiff line change
@@ -2336,9 +2336,3 @@ 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()

pandas/tests/arithmetic/test_timedelta64.py

-6
Original file line numberDiff line numberDiff line change
@@ -1975,9 +1975,3 @@ 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()

pandas/tests/scalar/test_nat.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -141,10 +141,9 @@ def test_nat_iso_format(get_nat):
141141

142142

143143
@pytest.mark.parametrize("klass,expected", [
144-
(Timestamp, ["freqstr", "normalize", "to_julian_date", "to_numpy",
145-
"to_period", "tz"]),
146-
(Timedelta, ["components", "delta", "is_populated", "to_numpy",
147-
"to_pytimedelta", "to_timedelta64", "view"])
144+
(Timestamp, ["freqstr", "normalize", "to_julian_date", "to_period", "tz"]),
145+
(Timedelta, ["components", "delta", "is_populated", "to_pytimedelta",
146+
"to_timedelta64", "view"])
148147
])
149148
def test_missing_public_nat_methods(klass, expected):
150149
# see gh-17327

pandas/tests/scalar/timedelta/test_timedelta.py

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

397+
def test_to_numpy_alias(self):
398+
# GH 24653: alias .to_numpy() for scalars
399+
td = Timedelta('10m7s')
400+
assert td.to_timedelta64() == td.to_numpy()
401+
397402
def test_round(self):
398403

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

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)