Skip to content

Commit e7a5e15

Browse files
author
Chris Bertinato
committed
Added doc strings and to_numpy for NaT
1 parent c23bbc4 commit e7a5e15

File tree

10 files changed

+65
-25
lines changed

10 files changed

+65
-25
lines changed

doc/source/whatsnew/v0.24.2.rst

Lines changed: 0 additions & 6 deletions
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

Lines changed: 2 additions & 1 deletion
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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,22 @@ cdef class _NaT(datetime):
188188
"""
189189
return np.datetime64('NaT', 'ns')
190190

191+
def to_numpy(self, dtype=None, copy=False):
192+
"""
193+
Convert NaT to a NumPy datetime64 NaT.
194+
195+
.. versionadded:: 0.25.0
196+
197+
This is an alias method for `NaT.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('NaT')
204+
"""
205+
return self.to_datetime64()
206+
191207
def __repr__(self):
192208
return 'NaT'
193209

pandas/_libs/tslibs/timedeltas.pyx

Lines changed: 17 additions & 1 deletion
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

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,23 @@ cdef class _Timestamp(datetime):
346346
return np.datetime64(self.value, 'ns')
347347

348348
def to_numpy(self, dtype=None, copy=False):
349-
""" Alias to to_datetime64 for Timestamp scalars """
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+
"""
350366
return self.to_datetime64()
351367

352368
def __add__(self, other):

pandas/tests/arithmetic/test_datetime64.py

Lines changed: 0 additions & 6 deletions
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

Lines changed: 0 additions & 6 deletions
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

Lines changed: 3 additions & 4 deletions
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

Lines changed: 5 additions & 0 deletions
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')

pandas/tests/scalar/timestamp/test_timestamp.py

Lines changed: 5 additions & 0 deletions
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)