diff --git a/pandas/core/arrays/datetimes.py b/pandas/core/arrays/datetimes.py index 3cf4dde3015c9..23ff5e7491ada 100644 --- a/pandas/core/arrays/datetimes.py +++ b/pandas/core/arrays/datetimes.py @@ -2308,6 +2308,7 @@ def _sequence_to_dt64ns( # assume this data are epoch timestamps if data.dtype != INT64_DTYPE: data = data.astype(np.int64, copy=False) + copy = False result = data.view(out_dtype) if copy: diff --git a/pandas/tests/arrays/datetimes/test_constructors.py b/pandas/tests/arrays/datetimes/test_constructors.py index e513457819eb5..b0a15d7ef4935 100644 --- a/pandas/tests/arrays/datetimes/test_constructors.py +++ b/pandas/tests/arrays/datetimes/test_constructors.py @@ -8,7 +8,6 @@ import pandas as pd import pandas._testing as tm from pandas.core.arrays import DatetimeArray -from pandas.core.arrays.datetimes import _sequence_to_dt64ns class TestDatetimeArrayConstructor: @@ -44,7 +43,6 @@ def test_freq_validation(self): "meth", [ DatetimeArray._from_sequence, - _sequence_to_dt64ns, pd.to_datetime, pd.DatetimeIndex, ], @@ -104,9 +102,6 @@ def test_bool_dtype_raises(self): with pytest.raises(TypeError, match=msg): DatetimeArray._from_sequence(arr) - with pytest.raises(TypeError, match=msg): - _sequence_to_dt64ns(arr) - with pytest.raises(TypeError, match=msg): pd.DatetimeIndex(arr) @@ -143,14 +138,12 @@ def test_tz_dtype_mismatch_raises(self): ["2000"], dtype=DatetimeTZDtype(tz="US/Central") ) with pytest.raises(TypeError, match="data is already tz-aware"): - DatetimeArray._from_sequence_not_strict( - arr, dtype=DatetimeTZDtype(tz="UTC") - ) + DatetimeArray._from_sequence(arr, dtype=DatetimeTZDtype(tz="UTC")) def test_tz_dtype_matches(self): dtype = DatetimeTZDtype(tz="US/Central") arr = DatetimeArray._from_sequence(["2000"], dtype=dtype) - result = DatetimeArray._from_sequence_not_strict(arr, dtype=dtype) + result = DatetimeArray._from_sequence(arr, dtype=dtype) tm.assert_equal(arr, result) @pytest.mark.parametrize("order", ["F", "C"]) @@ -160,13 +153,6 @@ def test_2d(self, order): if order == "F": arr = arr.T - res = _sequence_to_dt64ns(arr) - expected = _sequence_to_dt64ns(arr.ravel()) - - tm.assert_numpy_array_equal(res[0].ravel(), expected[0]) - assert res[1] == expected[1] - assert res[2] == expected[2] - res = DatetimeArray._from_sequence(arr) expected = DatetimeArray._from_sequence(arr.ravel()).reshape(arr.shape) tm.assert_datetime_array_equal(res, expected) diff --git a/pandas/tests/arrays/datetimes/test_cumulative.py b/pandas/tests/arrays/datetimes/test_cumulative.py index ca9760d58770a..2d61dba212064 100644 --- a/pandas/tests/arrays/datetimes/test_cumulative.py +++ b/pandas/tests/arrays/datetimes/test_cumulative.py @@ -7,40 +7,35 @@ class TestAccumulator: def test_accumulators_freq(self): # GH#50297 - arr = DatetimeArray._from_sequence_not_strict( + arr = DatetimeArray._from_sequence( [ "2000-01-01", "2000-01-02", "2000-01-03", - ], - freq="D", - ) + ] + )._with_freq("infer") result = arr._accumulate("cummin") - expected = DatetimeArray._from_sequence_not_strict( - ["2000-01-01"] * 3, freq=None - ) + expected = DatetimeArray._from_sequence(["2000-01-01"] * 3) tm.assert_datetime_array_equal(result, expected) result = arr._accumulate("cummax") - expected = DatetimeArray._from_sequence_not_strict( + expected = DatetimeArray._from_sequence( [ "2000-01-01", "2000-01-02", "2000-01-03", ], - freq=None, ) tm.assert_datetime_array_equal(result, expected) @pytest.mark.parametrize("func", ["cumsum", "cumprod"]) def test_accumulators_disallowed(self, func): # GH#50297 - arr = DatetimeArray._from_sequence_not_strict( + arr = DatetimeArray._from_sequence( [ "2000-01-01", "2000-01-02", ], - freq="D", - ) + )._with_freq("infer") with pytest.raises(TypeError, match=f"Accumulation {func}"): arr._accumulate(func) diff --git a/pandas/tests/arrays/test_datetimelike.py b/pandas/tests/arrays/test_datetimelike.py index 3f91b9b03e1de..8f49a924a97df 100644 --- a/pandas/tests/arrays/test_datetimelike.py +++ b/pandas/tests/arrays/test_datetimelike.py @@ -27,8 +27,6 @@ PeriodArray, TimedeltaArray, ) -from pandas.core.arrays.datetimes import _sequence_to_dt64ns -from pandas.core.arrays.timedeltas import sequence_to_td64ns # TODO: more freq variants @@ -1314,11 +1312,6 @@ def test_from_pandas_array(dtype): expected = cls._from_sequence(data) tm.assert_extension_array_equal(result, expected) - func = {"M8[ns]": _sequence_to_dt64ns, "m8[ns]": sequence_to_td64ns}[dtype] - result = func(arr)[0] - expected = func(data)[0] - tm.assert_equal(result, expected) - func = {"M8[ns]": pd.to_datetime, "m8[ns]": pd.to_timedelta}[dtype] result = func(arr).array expected = func(data).array diff --git a/pandas/tests/arrays/timedeltas/test_cumulative.py b/pandas/tests/arrays/timedeltas/test_cumulative.py index b321dc05bef27..2668ea673fa40 100644 --- a/pandas/tests/arrays/timedeltas/test_cumulative.py +++ b/pandas/tests/arrays/timedeltas/test_cumulative.py @@ -7,13 +7,13 @@ class TestAccumulator: def test_accumulators_disallowed(self): # GH#50297 - arr = TimedeltaArray._from_sequence_not_strict(["1D", "2D"]) + arr = TimedeltaArray._from_sequence(["1D", "2D"]) with pytest.raises(TypeError, match="cumprod not supported"): arr._accumulate("cumprod") def test_cumsum(self): # GH#50297 - arr = TimedeltaArray._from_sequence_not_strict(["1D", "2D"]) + arr = TimedeltaArray._from_sequence(["1D", "2D"]) result = arr._accumulate("cumsum") - expected = TimedeltaArray._from_sequence_not_strict(["1D", "3D"]) + expected = TimedeltaArray._from_sequence(["1D", "3D"]) tm.assert_timedelta_array_equal(result, expected) diff --git a/pandas/tests/indexes/datetimes/test_constructors.py b/pandas/tests/indexes/datetimes/test_constructors.py index bb6d68b39e451..035d74756c2e8 100644 --- a/pandas/tests/indexes/datetimes/test_constructors.py +++ b/pandas/tests/indexes/datetimes/test_constructors.py @@ -74,10 +74,7 @@ def test_explicit_tz_none(self): with pytest.raises(ValueError, match=msg): DatetimeIndex([], dtype="M8[ns, UTC]", tz=None) - @pytest.mark.parametrize( - "dt_cls", [DatetimeIndex, DatetimeArray._from_sequence_not_strict] - ) - def test_freq_validation_with_nat(self, dt_cls): + def test_freq_validation_with_nat(self): # GH#11587 make sure we get a useful error message when generate_range # raises msg = ( @@ -85,9 +82,9 @@ def test_freq_validation_with_nat(self, dt_cls): "to passed frequency D" ) with pytest.raises(ValueError, match=msg): - dt_cls([pd.NaT, Timestamp("2011-01-01")], freq="D") + DatetimeIndex([pd.NaT, Timestamp("2011-01-01")], freq="D") with pytest.raises(ValueError, match=msg): - dt_cls([pd.NaT, Timestamp("2011-01-01")._value], freq="D") + DatetimeIndex([pd.NaT, Timestamp("2011-01-01")._value], freq="D") # TODO: better place for tests shared by DTI/TDI? @pytest.mark.parametrize( diff --git a/pandas/tests/indexes/timedeltas/test_constructors.py b/pandas/tests/indexes/timedeltas/test_constructors.py index a3de699a9f58d..c173864b1a8b3 100644 --- a/pandas/tests/indexes/timedeltas/test_constructors.py +++ b/pandas/tests/indexes/timedeltas/test_constructors.py @@ -11,10 +11,7 @@ to_timedelta, ) import pandas._testing as tm -from pandas.core.arrays.timedeltas import ( - TimedeltaArray, - sequence_to_td64ns, -) +from pandas.core.arrays.timedeltas import TimedeltaArray class TestTimedeltaIndex: @@ -36,9 +33,6 @@ def test_array_of_dt64_nat_raises(self): with pytest.raises(TypeError, match=msg): TimedeltaArray._from_sequence(arr) - with pytest.raises(TypeError, match=msg): - sequence_to_td64ns(arr) - with pytest.raises(TypeError, match=msg): to_timedelta(arr) diff --git a/pandas/tests/test_downstream.py b/pandas/tests/test_downstream.py index 735c6131ba319..08a1c8e3aebb2 100644 --- a/pandas/tests/test_downstream.py +++ b/pandas/tests/test_downstream.py @@ -23,8 +23,6 @@ DatetimeArray, TimedeltaArray, ) -from pandas.core.arrays.datetimes import _sequence_to_dt64ns -from pandas.core.arrays.timedeltas import sequence_to_td64ns @pytest.fixture @@ -316,11 +314,6 @@ def test_from_obscure_array(dtype, array_likes): result = cls._from_sequence(data) tm.assert_extension_array_equal(result, expected) - func = {"M8[ns]": _sequence_to_dt64ns, "m8[ns]": sequence_to_td64ns}[dtype] - result = func(arr)[0] - expected = func(data)[0] - tm.assert_equal(result, expected) - if not isinstance(data, memoryview): # FIXME(GH#44431) these raise on memoryview and attempted fix # fails on py3.10