Skip to content

Commit f8e0ecb

Browse files
authored
CLN: standardize fixture usage in datetimelike array tests (#36902)
1 parent 880b361 commit f8e0ecb

File tree

2 files changed

+95
-98
lines changed

2 files changed

+95
-98
lines changed

pandas/tests/arrays/test_datetimelike.py

+63-69
Original file line numberDiff line numberDiff line change
@@ -17,37 +17,40 @@
1717

1818
# TODO: more freq variants
1919
@pytest.fixture(params=["D", "B", "W", "M", "Q", "Y"])
20-
def period_index(request):
20+
def freqstr(request):
21+
return request.param
22+
23+
24+
@pytest.fixture
25+
def period_index(freqstr):
2126
"""
2227
A fixture to provide PeriodIndex objects with different frequencies.
2328
2429
Most PeriodArray behavior is already tested in PeriodIndex tests,
2530
so here we just test that the PeriodArray behavior matches
2631
the PeriodIndex behavior.
2732
"""
28-
freqstr = request.param
2933
# TODO: non-monotone indexes; NaTs, different start dates
3034
pi = pd.period_range(start=pd.Timestamp("2000-01-01"), periods=100, freq=freqstr)
3135
return pi
3236

3337

34-
@pytest.fixture(params=["D", "B", "W", "M", "Q", "Y"])
35-
def datetime_index(request):
38+
@pytest.fixture
39+
def datetime_index(freqstr):
3640
"""
3741
A fixture to provide DatetimeIndex objects with different frequencies.
3842
3943
Most DatetimeArray behavior is already tested in DatetimeIndex tests,
4044
so here we just test that the DatetimeArray behavior matches
4145
the DatetimeIndex behavior.
4246
"""
43-
freqstr = request.param
4447
# TODO: non-monotone indexes; NaTs, different start dates, timezones
4548
dti = pd.date_range(start=pd.Timestamp("2000-01-01"), periods=100, freq=freqstr)
4649
return dti
4750

4851

4952
@pytest.fixture
50-
def timedelta_index(request):
53+
def timedelta_index():
5154
"""
5255
A fixture to provide TimedeltaIndex objects with different frequencies.
5356
Most TimedeltaArray behavior is already tested in TimedeltaIndex tests,
@@ -448,16 +451,15 @@ class TestDatetimeArray(SharedTests):
448451
dtype = pd.Timestamp
449452

450453
@pytest.fixture
451-
def arr1d(self, tz_naive_fixture):
454+
def arr1d(self, tz_naive_fixture, freqstr):
452455
tz = tz_naive_fixture
453-
dti = pd.date_range("2016-01-01 01:01:00", periods=3, freq="H", tz=tz)
456+
dti = pd.date_range("2016-01-01 01:01:00", periods=3, freq=freqstr, tz=tz)
454457
dta = dti._data
455458
return dta
456459

457-
def test_round(self, tz_naive_fixture):
460+
def test_round(self, arr1d):
458461
# GH#24064
459-
tz = tz_naive_fixture
460-
dti = pd.date_range("2016-01-01 01:01:00", periods=3, freq="H", tz=tz)
462+
dti = self.index_cls(arr1d)
461463

462464
result = dti.round(freq="2T")
463465
expected = dti - pd.Timedelta(minutes=1)
@@ -511,11 +513,10 @@ def test_array_interface(self, datetime_index):
511513
expected = np.asarray(arr).astype(dtype)
512514
tm.assert_numpy_array_equal(result, expected)
513515

514-
def test_array_object_dtype(self, tz_naive_fixture):
516+
def test_array_object_dtype(self, arr1d):
515517
# GH#23524
516-
tz = tz_naive_fixture
517-
dti = pd.date_range("2016-01-01", periods=3, tz=tz)
518-
arr = DatetimeArray(dti)
518+
arr = arr1d
519+
dti = self.index_cls(arr1d)
519520

520521
expected = np.array(list(dti))
521522

@@ -526,11 +527,10 @@ def test_array_object_dtype(self, tz_naive_fixture):
526527
result = np.array(dti, dtype=object)
527528
tm.assert_numpy_array_equal(result, expected)
528529

529-
def test_array_tz(self, tz_naive_fixture):
530+
def test_array_tz(self, arr1d):
530531
# GH#23524
531-
tz = tz_naive_fixture
532-
dti = pd.date_range("2016-01-01", periods=3, tz=tz)
533-
arr = DatetimeArray(dti)
532+
arr = arr1d
533+
dti = self.index_cls(arr1d)
534534

535535
expected = dti.asi8.view("M8[ns]")
536536
result = np.array(arr, dtype="M8[ns]")
@@ -547,10 +547,9 @@ def test_array_tz(self, tz_naive_fixture):
547547
assert result.base is expected.base
548548
assert result.base is not None
549549

550-
def test_array_i8_dtype(self, tz_naive_fixture):
551-
tz = tz_naive_fixture
552-
dti = pd.date_range("2016-01-01", periods=3, tz=tz)
553-
arr = DatetimeArray(dti)
550+
def test_array_i8_dtype(self, arr1d):
551+
arr = arr1d
552+
dti = self.index_cls(arr1d)
554553

555554
expected = dti.asi8
556555
result = np.array(arr, dtype="i8")
@@ -573,27 +572,25 @@ def test_from_array_keeps_base(self):
573572
dta = DatetimeArray(arr[:0])
574573
assert dta._data.base is arr
575574

576-
def test_from_dti(self, tz_naive_fixture):
577-
tz = tz_naive_fixture
578-
dti = pd.date_range("2016-01-01", periods=3, tz=tz)
579-
arr = DatetimeArray(dti)
575+
def test_from_dti(self, arr1d):
576+
arr = arr1d
577+
dti = self.index_cls(arr1d)
580578
assert list(dti) == list(arr)
581579

582580
# Check that Index.__new__ knows what to do with DatetimeArray
583581
dti2 = pd.Index(arr)
584582
assert isinstance(dti2, pd.DatetimeIndex)
585583
assert list(dti2) == list(arr)
586584

587-
def test_astype_object(self, tz_naive_fixture):
588-
tz = tz_naive_fixture
589-
dti = pd.date_range("2016-01-01", periods=3, tz=tz)
590-
arr = DatetimeArray(dti)
585+
def test_astype_object(self, arr1d):
586+
arr = arr1d
587+
dti = self.index_cls(arr1d)
588+
591589
asobj = arr.astype("O")
592590
assert isinstance(asobj, np.ndarray)
593591
assert asobj.dtype == "O"
594592
assert list(asobj) == list(dti)
595593

596-
@pytest.mark.parametrize("freqstr", ["D", "B", "W", "M", "Q", "Y"])
597594
def test_to_perioddelta(self, datetime_index, freqstr):
598595
# GH#23113
599596
dti = datetime_index
@@ -612,7 +609,6 @@ def test_to_perioddelta(self, datetime_index, freqstr):
612609
# an EA-specific tm.assert_ function
613610
tm.assert_index_equal(pd.Index(result), pd.Index(expected))
614611

615-
@pytest.mark.parametrize("freqstr", ["D", "B", "W", "M", "Q", "Y"])
616612
def test_to_period(self, datetime_index, freqstr):
617613
dti = datetime_index
618614
arr = DatetimeArray(dti)
@@ -626,10 +622,10 @@ def test_to_period(self, datetime_index, freqstr):
626622
tm.assert_index_equal(pd.Index(result), pd.Index(expected))
627623

628624
@pytest.mark.parametrize("propname", pd.DatetimeIndex._bool_ops)
629-
def test_bool_properties(self, datetime_index, propname):
625+
def test_bool_properties(self, arr1d, propname):
630626
# in this case _bool_ops is just `is_leap_year`
631-
dti = datetime_index
632-
arr = DatetimeArray(dti)
627+
dti = self.index_cls(arr1d)
628+
arr = arr1d
633629
assert dti.freq == arr.freq
634630

635631
result = getattr(arr, propname)
@@ -638,21 +634,21 @@ def test_bool_properties(self, datetime_index, propname):
638634
tm.assert_numpy_array_equal(result, expected)
639635

640636
@pytest.mark.parametrize("propname", pd.DatetimeIndex._field_ops)
641-
def test_int_properties(self, datetime_index, propname):
637+
def test_int_properties(self, arr1d, propname):
642638
if propname in ["week", "weekofyear"]:
643639
# GH#33595 Deprecate week and weekofyear
644640
return
645-
dti = datetime_index
646-
arr = DatetimeArray(dti)
641+
dti = self.index_cls(arr1d)
642+
arr = arr1d
647643

648644
result = getattr(arr, propname)
649645
expected = np.array(getattr(dti, propname), dtype=result.dtype)
650646

651647
tm.assert_numpy_array_equal(result, expected)
652648

653-
def test_take_fill_valid(self, datetime_index, tz_naive_fixture):
654-
dti = datetime_index.tz_localize(tz_naive_fixture)
655-
arr = DatetimeArray(dti)
649+
def test_take_fill_valid(self, arr1d):
650+
arr = arr1d
651+
dti = self.index_cls(arr1d)
656652

657653
now = pd.Timestamp.now().tz_localize(dti.tz)
658654
result = arr.take([-1, 1], allow_fill=True, fill_value=now)
@@ -687,10 +683,9 @@ def test_take_fill_valid(self, datetime_index, tz_naive_fixture):
687683
# require appropriate-dtype if we have a NA value
688684
arr.take([-1, 1], allow_fill=True, fill_value=value)
689685

690-
def test_concat_same_type_invalid(self, datetime_index):
686+
def test_concat_same_type_invalid(self, arr1d):
691687
# different timezones
692-
dti = datetime_index
693-
arr = DatetimeArray(dti)
688+
arr = arr1d
694689

695690
if arr.tz is None:
696691
other = arr.tz_localize("UTC")
@@ -718,8 +713,8 @@ def test_concat_same_type_different_freq(self):
718713

719714
tm.assert_datetime_array_equal(result, expected)
720715

721-
def test_strftime(self, datetime_index):
722-
arr = DatetimeArray(datetime_index)
716+
def test_strftime(self, arr1d):
717+
arr = arr1d
723718

724719
result = arr.strftime("%Y %b")
725720
expected = np.array([ts.strftime("%Y %b") for ts in arr], dtype=object)
@@ -864,27 +859,26 @@ class TestPeriodArray(SharedTests):
864859
def arr1d(self, period_index):
865860
return period_index._data
866861

867-
def test_from_pi(self, period_index):
868-
pi = period_index
869-
arr = PeriodArray(pi)
862+
def test_from_pi(self, arr1d):
863+
pi = self.index_cls(arr1d)
864+
arr = arr1d
870865
assert list(arr) == list(pi)
871866

872867
# Check that Index.__new__ knows what to do with PeriodArray
873868
pi2 = pd.Index(arr)
874869
assert isinstance(pi2, pd.PeriodIndex)
875870
assert list(pi2) == list(arr)
876871

877-
def test_astype_object(self, period_index):
878-
pi = period_index
879-
arr = PeriodArray(pi)
872+
def test_astype_object(self, arr1d):
873+
pi = self.index_cls(arr1d)
874+
arr = arr1d
880875
asobj = arr.astype("O")
881876
assert isinstance(asobj, np.ndarray)
882877
assert asobj.dtype == "O"
883878
assert list(asobj) == list(pi)
884879

885-
def test_take_fill_valid(self, period_index):
886-
pi = period_index
887-
arr = PeriodArray(pi)
880+
def test_take_fill_valid(self, arr1d):
881+
arr = arr1d
888882

889883
value = pd.NaT.value
890884
msg = f"'fill_value' should be a {self.dtype}. Got '{value}'."
@@ -899,9 +893,9 @@ def test_take_fill_valid(self, period_index):
899893
arr.take([-1, 1], allow_fill=True, fill_value=value)
900894

901895
@pytest.mark.parametrize("how", ["S", "E"])
902-
def test_to_timestamp(self, how, period_index):
903-
pi = period_index
904-
arr = PeriodArray(pi)
896+
def test_to_timestamp(self, how, arr1d):
897+
pi = self.index_cls(arr1d)
898+
arr = arr1d
905899

906900
expected = DatetimeArray(pi.to_timestamp(how=how))
907901
result = arr.to_timestamp(how=how)
@@ -922,28 +916,28 @@ def test_to_timestamp_out_of_bounds(self):
922916
pi._data.to_timestamp()
923917

924918
@pytest.mark.parametrize("propname", PeriodArray._bool_ops)
925-
def test_bool_properties(self, period_index, propname):
919+
def test_bool_properties(self, arr1d, propname):
926920
# in this case _bool_ops is just `is_leap_year`
927-
pi = period_index
928-
arr = PeriodArray(pi)
921+
pi = self.index_cls(arr1d)
922+
arr = arr1d
929923

930924
result = getattr(arr, propname)
931925
expected = np.array(getattr(pi, propname))
932926

933927
tm.assert_numpy_array_equal(result, expected)
934928

935929
@pytest.mark.parametrize("propname", PeriodArray._field_ops)
936-
def test_int_properties(self, period_index, propname):
937-
pi = period_index
938-
arr = PeriodArray(pi)
930+
def test_int_properties(self, arr1d, propname):
931+
pi = self.index_cls(arr1d)
932+
arr = arr1d
939933

940934
result = getattr(arr, propname)
941935
expected = np.array(getattr(pi, propname))
942936

943937
tm.assert_numpy_array_equal(result, expected)
944938

945-
def test_array_interface(self, period_index):
946-
arr = PeriodArray(period_index)
939+
def test_array_interface(self, arr1d):
940+
arr = arr1d
947941

948942
# default asarray gives objects
949943
result = np.asarray(arr)
@@ -966,8 +960,8 @@ def test_array_interface(self, period_index):
966960
expected = np.asarray(arr).astype("S20")
967961
tm.assert_numpy_array_equal(result, expected)
968962

969-
def test_strftime(self, period_index):
970-
arr = PeriodArray(period_index)
963+
def test_strftime(self, arr1d):
964+
arr = arr1d
971965

972966
result = arr.strftime("%Y")
973967
expected = np.array([per.strftime("%Y") for per in arr], dtype=object)

pandas/tests/arrays/test_timedeltas.py

+32-29
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ def test_copy(self):
6161

6262

6363
class TestTimedeltaArray:
64+
# TODO: de-duplicate with test_npsum below
6465
def test_np_sum(self):
6566
# GH#25282
6667
vals = np.arange(5, dtype=np.int64).view("m8[h]").astype("m8[ns]")
@@ -76,35 +77,6 @@ def test_from_sequence_dtype(self):
7677
with pytest.raises(ValueError, match=msg):
7778
TimedeltaArray._from_sequence([], dtype=object)
7879

79-
def test_abs(self):
80-
vals = np.array([-3600 * 10 ** 9, "NaT", 7200 * 10 ** 9], dtype="m8[ns]")
81-
arr = TimedeltaArray(vals)
82-
83-
evals = np.array([3600 * 10 ** 9, "NaT", 7200 * 10 ** 9], dtype="m8[ns]")
84-
expected = TimedeltaArray(evals)
85-
86-
result = abs(arr)
87-
tm.assert_timedelta_array_equal(result, expected)
88-
89-
def test_neg(self):
90-
vals = np.array([-3600 * 10 ** 9, "NaT", 7200 * 10 ** 9], dtype="m8[ns]")
91-
arr = TimedeltaArray(vals)
92-
93-
evals = np.array([3600 * 10 ** 9, "NaT", -7200 * 10 ** 9], dtype="m8[ns]")
94-
expected = TimedeltaArray(evals)
95-
96-
result = -arr
97-
tm.assert_timedelta_array_equal(result, expected)
98-
99-
def test_neg_freq(self):
100-
tdi = pd.timedelta_range("2 Days", periods=4, freq="H")
101-
arr = TimedeltaArray(tdi, freq=tdi.freq)
102-
103-
expected = TimedeltaArray(-tdi._data, freq=-tdi.freq)
104-
105-
result = -arr
106-
tm.assert_timedelta_array_equal(result, expected)
107-
10880
@pytest.mark.parametrize("dtype", [int, np.int32, np.int64, "uint32", "uint64"])
10981
def test_astype_int(self, dtype):
11082
arr = TimedeltaArray._from_sequence([pd.Timedelta("1H"), pd.Timedelta("2H")])
@@ -171,6 +143,37 @@ def test_searchsorted_invalid_types(self, other, index):
171143
arr.searchsorted(other)
172144

173145

146+
class TestUnaryOps:
147+
def test_abs(self):
148+
vals = np.array([-3600 * 10 ** 9, "NaT", 7200 * 10 ** 9], dtype="m8[ns]")
149+
arr = TimedeltaArray(vals)
150+
151+
evals = np.array([3600 * 10 ** 9, "NaT", 7200 * 10 ** 9], dtype="m8[ns]")
152+
expected = TimedeltaArray(evals)
153+
154+
result = abs(arr)
155+
tm.assert_timedelta_array_equal(result, expected)
156+
157+
def test_neg(self):
158+
vals = np.array([-3600 * 10 ** 9, "NaT", 7200 * 10 ** 9], dtype="m8[ns]")
159+
arr = TimedeltaArray(vals)
160+
161+
evals = np.array([3600 * 10 ** 9, "NaT", -7200 * 10 ** 9], dtype="m8[ns]")
162+
expected = TimedeltaArray(evals)
163+
164+
result = -arr
165+
tm.assert_timedelta_array_equal(result, expected)
166+
167+
def test_neg_freq(self):
168+
tdi = pd.timedelta_range("2 Days", periods=4, freq="H")
169+
arr = TimedeltaArray(tdi, freq=tdi.freq)
170+
171+
expected = TimedeltaArray(-tdi._data, freq=-tdi.freq)
172+
173+
result = -arr
174+
tm.assert_timedelta_array_equal(result, expected)
175+
176+
174177
class TestReductions:
175178
@pytest.mark.parametrize("name", ["sum", "std", "min", "max", "median"])
176179
@pytest.mark.parametrize("skipna", [True, False])

0 commit comments

Comments
 (0)