Skip to content

Commit 2b8d566

Browse files
authored
(TST) Replacing Timestamp.now () in tests. (#44501)
* Replacing Timestamp.now () in tests. * Fixes for PEP8 * Error correction * Fixes for black * Moved the fixed_now_ts() fixture in 'pandas/conftest.py' * Applied the fixed_now_ts () fixture in tests. * Fixes for black * Fixes for black * Fixes BAG * Added non-zero minutes, seconds and microsecond
1 parent 34565ef commit 2b8d566

23 files changed

+91
-84
lines changed

pandas/conftest.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1268,6 +1268,16 @@ def timedelta64_dtype(request):
12681268
return request.param
12691269

12701270

1271+
@pytest.fixture
1272+
def fixed_now_ts():
1273+
"""
1274+
Fixture emits fixed Timestamp.now()
1275+
"""
1276+
return Timestamp(
1277+
year=2021, month=1, day=1, hour=12, minute=4, second=13, microsecond=22
1278+
)
1279+
1280+
12711281
@pytest.fixture(params=tm.FLOAT_NUMPY_DTYPES)
12721282
def float_numpy_dtype(request):
12731283
"""

pandas/tests/arithmetic/test_datetime64.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ def test_dt64arr_nat_comparison(self, tz_naive_fixture, box_with_array):
150150
tz = tz_naive_fixture
151151
box = box_with_array
152152

153-
ts = Timestamp.now(tz)
153+
ts = Timestamp("2021-01-01", tz=tz)
154154
ser = Series([ts, NaT])
155155

156156
obj = tm.box_expected(ser, box)

pandas/tests/arithmetic/test_numeric.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,9 @@ def test_operator_series_comparison_zerorank(self):
8585
expected = 0.0 > Series([1, 2, 3])
8686
tm.assert_series_equal(result, expected)
8787

88-
def test_df_numeric_cmp_dt64_raises(self, box_with_array):
88+
def test_df_numeric_cmp_dt64_raises(self, box_with_array, fixed_now_ts):
8989
# GH#8932, GH#22163
90-
ts = pd.Timestamp.now()
90+
ts = fixed_now_ts
9191
obj = np.array(range(5))
9292
obj = tm.box_expected(obj, box_with_array)
9393

@@ -281,9 +281,9 @@ def test_add_sub_timedeltalike_invalid(self, numeric_idx, other, box_with_array)
281281
@pytest.mark.parametrize(
282282
"other",
283283
[
284-
pd.Timestamp.now().to_pydatetime(),
285-
pd.Timestamp.now(tz="UTC").to_pydatetime(),
286-
pd.Timestamp.now().to_datetime64(),
284+
pd.Timestamp("2021-01-01").to_pydatetime(),
285+
pd.Timestamp("2021-01-01", tz="UTC").to_pydatetime(),
286+
pd.Timestamp("2021-01-01").to_datetime64(),
287287
pd.NaT,
288288
],
289289
)
@@ -873,7 +873,7 @@ def test_add_frames(self, first, second, expected):
873873
tm.assert_frame_equal(second + first, expected)
874874

875875
# TODO: This came from series.test.test_operators, needs cleanup
876-
def test_series_frame_radd_bug(self):
876+
def test_series_frame_radd_bug(self, fixed_now_ts):
877877
# GH#353
878878
vals = Series(tm.rands_array(5, 10))
879879
result = "foo_" + vals
@@ -889,7 +889,7 @@ def test_series_frame_radd_bug(self):
889889
ts.name = "ts"
890890

891891
# really raise this time
892-
now = pd.Timestamp.now().to_pydatetime()
892+
fix_now = fixed_now_ts.to_pydatetime()
893893
msg = "|".join(
894894
[
895895
"unsupported operand type",
@@ -898,10 +898,10 @@ def test_series_frame_radd_bug(self):
898898
]
899899
)
900900
with pytest.raises(TypeError, match=msg):
901-
now + ts
901+
fix_now + ts
902902

903903
with pytest.raises(TypeError, match=msg):
904-
ts + now
904+
ts + fix_now
905905

906906
# TODO: This came from series.test.test_operators, needs cleanup
907907
def test_datetime64_with_index(self):

pandas/tests/arithmetic/test_object.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ def test_sub_object(self):
315315
with pytest.raises(TypeError, match=msg):
316316
index - np.array([2, "foo"], dtype=object)
317317

318-
def test_rsub_object(self):
318+
def test_rsub_object(self, fixed_now_ts):
319319
# GH#19369
320320
index = pd.Index([Decimal(1), Decimal(2)])
321321
expected = pd.Index([Decimal(1), Decimal(0)])
@@ -331,7 +331,7 @@ def test_rsub_object(self):
331331
"foo" - index
332332

333333
with pytest.raises(TypeError, match=msg):
334-
np.array([True, Timestamp.now()]) - index
334+
np.array([True, fixed_now_ts]) - index
335335

336336

337337
class MyIndex(pd.Index):

pandas/tests/arithmetic/test_period.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ def test_compare_zerodim(self, box_with_array):
7272
"scalar",
7373
[
7474
"foo",
75-
Timestamp.now(),
75+
Timestamp("2021-01-01"),
7676
Timedelta(days=4),
7777
9,
7878
9.5,
@@ -693,9 +693,9 @@ def test_sub_n_gt_1_offsets(self, offset, kwd_name, n):
693693
"other",
694694
[
695695
# datetime scalars
696-
Timestamp.now(),
697-
Timestamp.now().to_pydatetime(),
698-
Timestamp.now().to_datetime64(),
696+
Timestamp("2016-01-01"),
697+
Timestamp("2016-01-01").to_pydatetime(),
698+
Timestamp("2016-01-01").to_datetime64(),
699699
# datetime-like arrays
700700
pd.date_range("2016-01-01", periods=3, freq="H"),
701701
pd.date_range("2016-01-01", periods=3, tz="Europe/Brussels"),

pandas/tests/arithmetic/test_timedelta64.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -110,11 +110,11 @@ def test_compare_timedeltalike_scalar(self, box_with_array, td_scalar):
110110
[
111111
345600000000000,
112112
"a",
113-
Timestamp.now(),
114-
Timestamp.now("UTC"),
115-
Timestamp.now().to_datetime64(),
116-
Timestamp.now().to_pydatetime(),
117-
Timestamp.now().date(),
113+
Timestamp("2021-01-01"),
114+
Timestamp("2021-01-01").now("UTC"),
115+
Timestamp("2021-01-01").now().to_datetime64(),
116+
Timestamp("2021-01-01").now().to_pydatetime(),
117+
Timestamp("2021-01-01").date(),
118118
np.array(4), # zero-dim mismatched dtype
119119
],
120120
)
@@ -152,7 +152,7 @@ def test_td64arr_cmp_arraylike_invalid(self, other, box_with_array):
152152

153153
def test_td64arr_cmp_mixed_invalid(self):
154154
rng = timedelta_range("1 days", periods=5)._data
155-
other = np.array([0, 1, 2, rng[3], Timestamp.now()])
155+
other = np.array([0, 1, 2, rng[3], Timestamp("2021-01-01")])
156156

157157
result = rng == other
158158
expected = np.array([False, False, False, True, False])
@@ -2174,7 +2174,7 @@ def test_td64arr_pow_invalid(self, scalar_td, box_with_array):
21742174

21752175
def test_add_timestamp_to_timedelta():
21762176
# GH: 35897
2177-
timestamp = Timestamp.now()
2177+
timestamp = Timestamp("2021-01-01")
21782178
result = timestamp + timedelta_range("0s", "1s", periods=31)
21792179
expected = DatetimeIndex(
21802180
[

pandas/tests/arrays/string_/test_string.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -539,7 +539,7 @@ def test_to_numpy_na_value(dtype, nulls_fixture):
539539
tm.assert_numpy_array_equal(result, expected)
540540

541541

542-
def test_isin(dtype, request):
542+
def test_isin(dtype, request, fixed_now_ts):
543543
s = pd.Series(["a", "b", None], dtype=dtype)
544544

545545
result = s.isin(["a", "c"])
@@ -554,6 +554,6 @@ def test_isin(dtype, request):
554554
expected = pd.Series([False, False, False])
555555
tm.assert_series_equal(result, expected)
556556

557-
result = s.isin(["a", pd.Timestamp.now()])
557+
result = s.isin(["a", fixed_now_ts])
558558
expected = pd.Series([True, False, False])
559559
tm.assert_series_equal(result, expected)

pandas/tests/arrays/test_datetimelike.py

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -839,30 +839,29 @@ def test_int_properties(self, arr1d, propname):
839839

840840
tm.assert_numpy_array_equal(result, expected)
841841

842-
def test_take_fill_valid(self, arr1d):
842+
def test_take_fill_valid(self, arr1d, fixed_now_ts):
843843
arr = arr1d
844844
dti = self.index_cls(arr1d)
845-
dt_ind = Timestamp(2021, 1, 1, 12)
846-
dt_ind_tz = dt_ind.tz_localize(dti.tz)
847845

848-
result = arr.take([-1, 1], allow_fill=True, fill_value=dt_ind_tz)
849-
assert result[0] == dt_ind_tz
846+
now = fixed_now_ts.tz_localize(dti.tz)
847+
result = arr.take([-1, 1], allow_fill=True, fill_value=now)
848+
assert result[0] == now
850849

851850
msg = f"value should be a '{arr1d._scalar_type.__name__}' or 'NaT'. Got"
852851
with pytest.raises(TypeError, match=msg):
853852
# fill_value Timedelta invalid
854-
arr.take([-1, 1], allow_fill=True, fill_value=dt_ind_tz - dt_ind_tz)
853+
arr.take([-1, 1], allow_fill=True, fill_value=now - now)
855854

856855
with pytest.raises(TypeError, match=msg):
857856
# fill_value Period invalid
858857
arr.take([-1, 1], allow_fill=True, fill_value=Period("2014Q1"))
859858

860859
tz = None if dti.tz is not None else "US/Eastern"
861-
dt_ind_tz = dt_ind.tz_localize(tz)
860+
now = fixed_now_ts.tz_localize(tz)
862861
msg = "Cannot compare tz-naive and tz-aware datetime-like objects"
863862
with pytest.raises(TypeError, match=msg):
864863
# Timestamp with mismatched tz-awareness
865-
arr.take([-1, 1], allow_fill=True, fill_value=dt_ind_tz)
864+
arr.take([-1, 1], allow_fill=True, fill_value=now)
866865

867866
value = NaT.value
868867
msg = f"value should be a '{arr1d._scalar_type.__name__}' or 'NaT'. Got"
@@ -878,7 +877,7 @@ def test_take_fill_valid(self, arr1d):
878877
if arr.tz is not None:
879878
# GH#37356
880879
# Assuming here that arr1d fixture does not include Australia/Melbourne
881-
value = dt_ind.tz_localize("Australia/Melbourne")
880+
value = fixed_now_ts.tz_localize("Australia/Melbourne")
882881
msg = "Timezones don't match. .* != 'Australia/Melbourne'"
883882
with pytest.raises(ValueError, match=msg):
884883
# require tz match, not just tzawareness match
@@ -1032,22 +1031,21 @@ def test_array_interface(self, timedelta_index):
10321031
expected = np.asarray(arr).astype(dtype)
10331032
tm.assert_numpy_array_equal(result, expected)
10341033

1035-
def test_take_fill_valid(self, timedelta_index):
1034+
def test_take_fill_valid(self, timedelta_index, fixed_now_ts):
10361035
tdi = timedelta_index
10371036
arr = TimedeltaArray(tdi)
10381037

10391038
td1 = pd.Timedelta(days=1)
10401039
result = arr.take([-1, 1], allow_fill=True, fill_value=td1)
10411040
assert result[0] == td1
10421041

1043-
dt_ind = Timestamp(2021, 1, 1, 12)
1044-
value = dt_ind
1042+
value = fixed_now_ts
10451043
msg = f"value should be a '{arr._scalar_type.__name__}' or 'NaT'. Got"
10461044
with pytest.raises(TypeError, match=msg):
10471045
# fill_value Timestamp invalid
10481046
arr.take([0, 1], allow_fill=True, fill_value=value)
10491047

1050-
value = dt_ind.to_period("D")
1048+
value = fixed_now_ts.to_period("D")
10511049
with pytest.raises(TypeError, match=msg):
10521050
# fill_value Period invalid
10531051
arr.take([0, 1], allow_fill=True, fill_value=value)

pandas/tests/arrays/test_datetimes.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -146,9 +146,9 @@ def test_setitem_clears_freq(self):
146146
@pytest.mark.parametrize(
147147
"obj",
148148
[
149-
pd.Timestamp.now(),
150-
pd.Timestamp.now().to_datetime64(),
151-
pd.Timestamp.now().to_pydatetime(),
149+
pd.Timestamp("2021-01-01"),
150+
pd.Timestamp("2021-01-01").to_datetime64(),
151+
pd.Timestamp("2021-01-01").to_pydatetime(),
152152
],
153153
)
154154
def test_setitem_objects(self, obj):
@@ -329,7 +329,7 @@ def test_searchsorted_tzawareness_compat(self, index):
329329
"invalid",
330330
np.arange(10, dtype="i8") * 24 * 3600 * 10 ** 9,
331331
np.arange(10).view("timedelta64[ns]") * 24 * 3600 * 10 ** 9,
332-
pd.Timestamp.now().to_period("D"),
332+
pd.Timestamp("2021-01-01").to_period("D"),
333333
],
334334
)
335335
@pytest.mark.parametrize("index", [True, False])

pandas/tests/arrays/test_timedeltas.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,11 @@ def test_setitem_objects(self, obj):
5555
np.int64(1),
5656
1.0,
5757
np.datetime64("NaT"),
58-
pd.Timestamp.now(),
58+
pd.Timestamp("2021-01-01"),
5959
"invalid",
6060
np.arange(10, dtype="i8") * 24 * 3600 * 10 ** 9,
6161
(np.arange(10) * 24 * 3600 * 10 ** 9).view("datetime64[ns]"),
62-
pd.Timestamp.now().to_period("D"),
62+
pd.Timestamp("2021-01-01").to_period("D"),
6363
],
6464
)
6565
@pytest.mark.parametrize("index", [True, False])

pandas/tests/arrays/timedeltas/test_reductions.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,9 +127,9 @@ def test_sum_2d_skipna_false(self):
127127
"add",
128128
[
129129
Timedelta(0),
130-
pd.Timestamp.now(),
131-
pd.Timestamp.now("UTC"),
132-
pd.Timestamp.now("Asia/Tokyo"),
130+
pd.Timestamp("2021-01-01"),
131+
pd.Timestamp("2021-01-01", tz="UTC"),
132+
pd.Timestamp("2021-01-01", tz="Asia/Tokyo"),
133133
],
134134
)
135135
def test_std(self, add):

pandas/tests/dtypes/cast/test_construct_from_scalar.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
from pandas import (
88
Categorical,
99
Timedelta,
10-
Timestamp,
1110
)
1211
import pandas._testing as tm
1312

@@ -25,9 +24,9 @@ def test_cast_1d_array_like_from_scalar_categorical():
2524
tm.assert_categorical_equal(result, expected)
2625

2726

28-
def test_cast_1d_array_like_from_timestamp():
27+
def test_cast_1d_array_like_from_timestamp(fixed_now_ts):
2928
# check we dont lose nanoseconds
30-
ts = Timestamp.now() + Timedelta(1)
29+
ts = fixed_now_ts + Timedelta(1)
3130
res = construct_1d_arraylike_from_scalar(ts, 2, np.dtype("M8[ns]"))
3231
assert res[0] == ts
3332

pandas/tests/dtypes/test_missing.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@
4444
import pandas._testing as tm
4545
from pandas.core.api import Float64Index
4646

47-
now = pd.Timestamp.now()
48-
utcnow = pd.Timestamp.now("UTC")
47+
fix_now = pd.Timestamp("2021-01-01")
48+
fix_utcnow = pd.Timestamp("2021-01-01", tz="UTC")
4949

5050

5151
@pytest.mark.parametrize("notna_f", [notna, notnull])
@@ -467,12 +467,12 @@ def test_array_equivalent_different_dtype_but_equal():
467467
# There are 3 variants for each of lvalue and rvalue. We include all
468468
# three for the tz-naive `now` and exclude the datetim64 variant
469469
# for utcnow because it drops tzinfo.
470-
(now, utcnow),
471-
(now.to_datetime64(), utcnow),
472-
(now.to_pydatetime(), utcnow),
473-
(now, utcnow),
474-
(now.to_datetime64(), utcnow.to_pydatetime()),
475-
(now.to_pydatetime(), utcnow.to_pydatetime()),
470+
(fix_now, fix_utcnow),
471+
(fix_now.to_datetime64(), fix_utcnow),
472+
(fix_now.to_pydatetime(), fix_utcnow),
473+
(fix_now, fix_utcnow),
474+
(fix_now.to_datetime64(), fix_utcnow.to_pydatetime()),
475+
(fix_now.to_pydatetime(), fix_utcnow.to_pydatetime()),
476476
],
477477
)
478478
def test_array_equivalent_tzawareness(lvalue, rvalue):

pandas/tests/frame/test_constructors.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2888,8 +2888,8 @@ def test_from_timedelta_scalar_preserves_nanos(self, constructor):
28882888
obj = constructor(td, dtype="m8[ns]")
28892889
assert get1(obj) == td
28902890

2891-
def test_from_timestamp_scalar_preserves_nanos(self, constructor):
2892-
ts = Timestamp.now() + Timedelta(1)
2891+
def test_from_timestamp_scalar_preserves_nanos(self, constructor, fixed_now_ts):
2892+
ts = fixed_now_ts + Timedelta(1)
28932893

28942894
obj = constructor(ts, dtype="M8[ns]")
28952895
assert get1(obj) == ts

pandas/tests/indexes/timedeltas/test_indexing.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ def test_where_doesnt_retain_freq(self):
149149
result = tdi.where(cond, tdi[::-1])
150150
tm.assert_index_equal(result, expected)
151151

152-
def test_where_invalid_dtypes(self):
152+
def test_where_invalid_dtypes(self, fixed_now_ts):
153153
tdi = timedelta_range("1 day", periods=3, freq="D", name="idx")
154154

155155
tail = tdi[2:].tolist()
@@ -161,17 +161,17 @@ def test_where_invalid_dtypes(self):
161161
result = tdi.where(mask, i2.asi8)
162162
tm.assert_index_equal(result, expected)
163163

164-
ts = i2 + Timestamp.now()
164+
ts = i2 + fixed_now_ts
165165
expected = Index([ts[0], ts[1]] + tail, dtype=object, name="idx")
166166
result = tdi.where(mask, ts)
167167
tm.assert_index_equal(result, expected)
168168

169-
per = (i2 + Timestamp.now()).to_period("D")
169+
per = (i2 + fixed_now_ts).to_period("D")
170170
expected = Index([per[0], per[1]] + tail, dtype=object, name="idx")
171171
result = tdi.where(mask, per)
172172
tm.assert_index_equal(result, expected)
173173

174-
ts = Timestamp.now()
174+
ts = fixed_now_ts
175175
expected = Index([ts, ts] + tail, dtype=object, name="idx")
176176
result = tdi.where(mask, ts)
177177
tm.assert_index_equal(result, expected)

0 commit comments

Comments
 (0)