Skip to content

REF: remove ensure_datetime64ns, ensure_timedelta64ns #47219

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 0 additions & 10 deletions pandas/_libs/tslibs/conversion.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,7 @@ from pandas._typing import npt
DT64NS_DTYPE: np.dtype
TD64NS_DTYPE: np.dtype

class OutOfBoundsTimedelta(ValueError): ...

def precision_from_unit(
unit: str,
) -> tuple[int, int]: ... # (int64_t, _)
def ensure_datetime64ns(
arr: np.ndarray, # np.ndarray[datetime64[ANY]]
copy: bool = ...,
) -> np.ndarray: ... # np.ndarray[datetime64ns]
def ensure_timedelta64ns(
arr: np.ndarray, # np.ndarray[timedelta64[ANY]]
copy: bool = ...,
) -> np.ndarray: ... # np.ndarray[timedelta64ns]
def localize_pydatetime(dt: datetime, tz: tzinfo | None) -> datetime: ...
39 changes: 0 additions & 39 deletions pandas/_libs/tslibs/conversion.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -193,45 +193,6 @@ cdef inline int64_t get_datetime64_nanos(object val) except? -1:
return ival


def ensure_datetime64ns(arr: ndarray, copy: bool = True):
"""
Ensure a np.datetime64 array has dtype specifically 'datetime64[ns]'

Parameters
----------
arr : ndarray
copy : bool, default True

Returns
-------
ndarray with dtype datetime64[ns]
"""
if (<object>arr).dtype.byteorder == ">":
# GH#29684 we incorrectly get OutOfBoundsDatetime if we dont swap
dtype = arr.dtype
arr = arr.astype(dtype.newbyteorder("<"))

return astype_overflowsafe(arr, DT64NS_DTYPE, copy=copy)


def ensure_timedelta64ns(arr: ndarray, copy: bool = True):
"""
Ensure a np.timedelta64 array has dtype specifically 'timedelta64[ns]'

Parameters
----------
arr : ndarray
copy : bool, default True

Returns
-------
ndarray[timedelta64[ns]]
"""
assert arr.dtype.kind == "m", arr.dtype

return astype_overflowsafe(arr, dtype=TD64NS_DTYPE, copy=copy)


# ----------------------------------------------------------------------
# _TSObject Conversion

Expand Down
4 changes: 4 additions & 0 deletions pandas/_libs/tslibs/np_datetime.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,10 @@ cpdef ndarray astype_overflowsafe(
"datetime64/timedelta64 values and dtype must have a unit specified"
)

if (<object>values).dtype.byteorder == ">":
# GH#29684 we incorrectly get OutOfBoundsDatetime if we dont swap
values = values.astype(values.dtype.newbyteorder("<"))

if from_unit == to_unit:
# Check this before allocating result for perf, might save some memory
if copy:
Expand Down
5 changes: 3 additions & 2 deletions pandas/_libs/tslibs/period.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ from pandas._libs.tslibs.np_datetime cimport (
NPY_DATETIMEUNIT,
NPY_FR_D,
NPY_FR_us,
astype_overflowsafe,
check_dts_bounds,
dt64_to_dtstruct,
dtstruct_to_dt64,
Expand All @@ -71,7 +72,7 @@ from pandas._libs.tslibs.timedeltas cimport (
is_any_td_scalar,
)

from pandas._libs.tslibs.conversion import ensure_datetime64ns
from pandas._libs.tslibs.conversion import DT64NS_DTYPE

from pandas._libs.tslibs.dtypes cimport (
FR_ANN,
Expand Down Expand Up @@ -981,7 +982,7 @@ def periodarr_to_dt64arr(const int64_t[:] periodarr, int freq):
dta = periodarr.base.view("M8[h]")
elif freq == FR_DAY:
dta = periodarr.base.view("M8[D]")
return ensure_datetime64ns(dta)
return astype_overflowsafe(dta, dtype=DT64NS_DTYPE)


cdef void get_asfreq_info(int from_freq, int to_freq,
Expand Down
4 changes: 2 additions & 2 deletions pandas/core/arrays/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
NaTType,
Resolution,
Timestamp,
conversion,
astype_overflowsafe,
fields,
get_resolution,
iNaT,
Expand Down Expand Up @@ -2169,7 +2169,7 @@ def _sequence_to_dt64ns(
# tz-naive DatetimeArray or ndarray[datetime64]
data = getattr(data, "_ndarray", data)
if data.dtype != DT64NS_DTYPE:
data = conversion.ensure_datetime64ns(data)
data = astype_overflowsafe(data, dtype=DT64NS_DTYPE)
copy = False

if tz is not None:
Expand Down
10 changes: 4 additions & 6 deletions pandas/core/arrays/timedeltas.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,11 @@
Tick,
Timedelta,
Timestamp,
astype_overflowsafe,
iNaT,
to_offset,
)
from pandas._libs.tslibs.conversion import (
ensure_timedelta64ns,
precision_from_unit,
)
from pandas._libs.tslibs.conversion import precision_from_unit
from pandas._libs.tslibs.fields import get_timedelta_field
from pandas._libs.tslibs.timedeltas import (
array_to_timedelta64,
Expand Down Expand Up @@ -1044,7 +1042,7 @@ def sequence_to_td64ns(
elif is_timedelta64_dtype(data.dtype):
if data.dtype != TD64NS_DTYPE:
# non-nano unit
data = ensure_timedelta64ns(data)
data = astype_overflowsafe(data, dtype=TD64NS_DTYPE)
copy = False

else:
Expand Down Expand Up @@ -1086,7 +1084,7 @@ def ints_to_td64ns(data, unit="ns"):
dtype_str = f"timedelta64[{unit}]"
data = data.view(dtype_str)

data = ensure_timedelta64ns(data)
data = astype_overflowsafe(data, dtype=TD64NS_DTYPE)

# the astype conversion makes a copy, so we can avoid re-copying later
copy_made = True
Expand Down
6 changes: 3 additions & 3 deletions pandas/core/dtypes/cast.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
OutOfBoundsTimedelta,
Timedelta,
Timestamp,
conversion,
astype_overflowsafe,
)
from pandas._libs.tslibs.timedeltas import array_to_timedelta64
from pandas._typing import (
Expand Down Expand Up @@ -1419,10 +1419,10 @@ def sanitize_to_nanoseconds(values: np.ndarray, copy: bool = False) -> np.ndarra
"""
dtype = values.dtype
if dtype.kind == "M" and dtype != DT64NS_DTYPE:
values = conversion.ensure_datetime64ns(values)
values = astype_overflowsafe(values, dtype=DT64NS_DTYPE)

elif dtype.kind == "m" and dtype != TD64NS_DTYPE:
values = conversion.ensure_timedelta64ns(values)
values = astype_overflowsafe(values, dtype=TD64NS_DTYPE)

elif copy:
values = values.copy()
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/arrays/categorical/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -751,7 +751,7 @@ def test_from_sequence_copy(self):

@pytest.mark.xfail(
not IS64 or is_platform_windows(),
reason="Incorrectly raising in ensure_datetime64ns",
reason="Incorrectly raising in astype_overflowsafe",
)
def test_constructor_datetime64_non_nano(self):
categories = np.arange(10).view("M8[D]")
Expand Down
4 changes: 2 additions & 2 deletions pandas/tests/indexes/datetimes/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

from pandas._libs.tslibs import (
OutOfBoundsDatetime,
conversion,
astype_overflowsafe,
)
from pandas.compat import PY39

Expand Down Expand Up @@ -975,7 +975,7 @@ def test_index_cast_datetime64_other_units(self):
arr = np.arange(0, 100, 10, dtype=np.int64).view("M8[D]")
idx = Index(arr)

assert (idx.values == conversion.ensure_datetime64ns(arr)).all()
assert (idx.values == astype_overflowsafe(arr, dtype=np.dtype("M8[ns]"))).all()

def test_constructor_int64_nocopy(self):
# GH#1624
Expand Down
7 changes: 4 additions & 3 deletions pandas/tests/tslibs/test_conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from pandas._libs.tslibs import (
OutOfBoundsTimedelta,
astype_overflowsafe,
conversion,
iNaT,
timezones,
Expand Down Expand Up @@ -106,7 +107,7 @@ def test_tz_convert_readonly():
@pytest.mark.parametrize("dtype", ["M8[ns]", "M8[s]"])
def test_length_zero_copy(dtype, copy):
arr = np.array([], dtype=dtype)
result = conversion.ensure_datetime64ns(arr, copy=copy)
result = astype_overflowsafe(arr, copy=copy, dtype=np.dtype("M8[ns]"))
if copy:
assert not np.shares_memory(result, arr)
else:
Expand All @@ -119,7 +120,7 @@ def test_length_zero_copy(dtype, copy):
def test_ensure_datetime64ns_bigendian():
# GH#29684
arr = np.array([np.datetime64(1, "ms")], dtype=">M8[ms]")
result = conversion.ensure_datetime64ns(arr)
result = astype_overflowsafe(arr, dtype=np.dtype("M8[ns]"))

expected = np.array([np.datetime64(1, "ms")], dtype="M8[ns]")
tm.assert_numpy_array_equal(result, expected)
Expand All @@ -129,7 +130,7 @@ def test_ensure_timedelta64ns_overflows():
arr = np.arange(10).astype("m8[Y]") * 100
msg = r"Cannot convert 300 years to timedelta64\[ns\] without overflow"
with pytest.raises(OutOfBoundsTimedelta, match=msg):
conversion.ensure_timedelta64ns(arr)
astype_overflowsafe(arr, dtype=np.dtype("m8[ns]"))


class SubDatetime(datetime):
Expand Down