Skip to content
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

TST: dont test private constructors #55654

Merged
merged 2 commits into from
Oct 24, 2023
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
1 change: 1 addition & 0 deletions pandas/core/arrays/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
18 changes: 2 additions & 16 deletions pandas/tests/arrays/datetimes/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -44,7 +43,6 @@ def test_freq_validation(self):
"meth",
[
DatetimeArray._from_sequence,
_sequence_to_dt64ns,
pd.to_datetime,
pd.DatetimeIndex,
],
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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"])
Expand All @@ -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)
Expand Down
19 changes: 7 additions & 12 deletions pandas/tests/arrays/datetimes/test_cumulative.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
7 changes: 0 additions & 7 deletions pandas/tests/arrays/test_datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions pandas/tests/arrays/timedeltas/test_cumulative.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
9 changes: 3 additions & 6 deletions pandas/tests/indexes/datetimes/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,20 +74,17 @@ 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 = (
"Inferred frequency None from passed values does not conform "
"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(
Expand Down
8 changes: 1 addition & 7 deletions pandas/tests/indexes/timedeltas/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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)

Expand Down
7 changes: 0 additions & 7 deletions pandas/tests/test_downstream.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Loading