Skip to content

Commit ab144c4

Browse files
authored
TST: parametrize over dt64 unit (#56052)
1 parent 2b67593 commit ab144c4

26 files changed

+218
-185
lines changed

pandas/core/arrays/datetimes.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -2244,13 +2244,11 @@ def _sequence_to_dt64(
22442244
if tz and inferred_tz:
22452245
# two timezones: convert to intended from base UTC repr
22462246
# GH#42505 by convention, these are _already_ UTC
2247-
assert converted.dtype == out_dtype, converted.dtype
2248-
result = converted.view(out_dtype)
2247+
result = converted
22492248

22502249
elif inferred_tz:
22512250
tz = inferred_tz
2252-
assert converted.dtype == out_dtype, converted.dtype
2253-
result = converted.view(out_dtype)
2251+
result = converted
22542252

22552253
else:
22562254
result, _ = _construct_from_dt64_naive(

pandas/tests/apply/test_frame_apply.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1272,7 +1272,7 @@ def test_nuiscance_columns():
12721272

12731273
result = df.agg(["min"])
12741274
expected = DataFrame(
1275-
[[1, 1.0, "bar", Timestamp("20130101")]],
1275+
[[1, 1.0, "bar", Timestamp("20130101").as_unit("ns")]],
12761276
index=["min"],
12771277
columns=df.columns,
12781278
)

pandas/tests/apply/test_series_apply.py

+19-13
Original file line numberDiff line numberDiff line change
@@ -150,39 +150,45 @@ def func(x):
150150
tm.assert_series_equal(result, expected)
151151

152152

153-
def test_apply_box():
153+
def test_apply_box_dt64():
154154
# ufunc will not be boxed. Same test cases as the test_map_box
155155
vals = [pd.Timestamp("2011-01-01"), pd.Timestamp("2011-01-02")]
156-
s = Series(vals)
157-
assert s.dtype == "datetime64[ns]"
156+
ser = Series(vals, dtype="M8[ns]")
157+
assert ser.dtype == "datetime64[ns]"
158158
# boxed value must be Timestamp instance
159-
res = s.apply(lambda x: f"{type(x).__name__}_{x.day}_{x.tz}", by_row="compat")
159+
res = ser.apply(lambda x: f"{type(x).__name__}_{x.day}_{x.tz}", by_row="compat")
160160
exp = Series(["Timestamp_1_None", "Timestamp_2_None"])
161161
tm.assert_series_equal(res, exp)
162162

163+
164+
def test_apply_box_dt64tz():
163165
vals = [
164166
pd.Timestamp("2011-01-01", tz="US/Eastern"),
165167
pd.Timestamp("2011-01-02", tz="US/Eastern"),
166168
]
167-
s = Series(vals)
168-
assert s.dtype == "datetime64[ns, US/Eastern]"
169-
res = s.apply(lambda x: f"{type(x).__name__}_{x.day}_{x.tz}", by_row="compat")
169+
ser = Series(vals, dtype="M8[ns, US/Eastern]")
170+
assert ser.dtype == "datetime64[ns, US/Eastern]"
171+
res = ser.apply(lambda x: f"{type(x).__name__}_{x.day}_{x.tz}", by_row="compat")
170172
exp = Series(["Timestamp_1_US/Eastern", "Timestamp_2_US/Eastern"])
171173
tm.assert_series_equal(res, exp)
172174

175+
176+
def test_apply_box_td64():
173177
# timedelta
174178
vals = [pd.Timedelta("1 days"), pd.Timedelta("2 days")]
175-
s = Series(vals)
176-
assert s.dtype == "timedelta64[ns]"
177-
res = s.apply(lambda x: f"{type(x).__name__}_{x.days}", by_row="compat")
179+
ser = Series(vals)
180+
assert ser.dtype == "timedelta64[ns]"
181+
res = ser.apply(lambda x: f"{type(x).__name__}_{x.days}", by_row="compat")
178182
exp = Series(["Timedelta_1", "Timedelta_2"])
179183
tm.assert_series_equal(res, exp)
180184

185+
186+
def test_apply_box_period():
181187
# period
182188
vals = [pd.Period("2011-01-01", freq="M"), pd.Period("2011-01-02", freq="M")]
183-
s = Series(vals)
184-
assert s.dtype == "Period[M]"
185-
res = s.apply(lambda x: f"{type(x).__name__}_{x.freqstr}", by_row="compat")
189+
ser = Series(vals)
190+
assert ser.dtype == "Period[M]"
191+
res = ser.apply(lambda x: f"{type(x).__name__}_{x.freqstr}", by_row="compat")
186192
exp = Series(["Period_M", "Period_M"])
187193
tm.assert_series_equal(res, exp)
188194

pandas/tests/arithmetic/test_datetime64.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -1286,7 +1286,7 @@ def test_dti_add_tick_tzaware(self, tz_aware_fixture, box_with_array):
12861286
["2010-11-01 05:00", "2010-11-01 06:00", "2010-11-01 07:00"],
12871287
freq="h",
12881288
tz=tz,
1289-
)
1289+
).as_unit("ns")
12901290

12911291
dates = tm.box_expected(dates, box_with_array)
12921292
expected = tm.box_expected(expected, box_with_array)
@@ -1580,7 +1580,7 @@ def test_dti_add_sub_nonzero_mth_offset(
15801580
mth = getattr(date, op)
15811581
result = mth(offset)
15821582

1583-
expected = DatetimeIndex(exp, tz=tz)
1583+
expected = DatetimeIndex(exp, tz=tz).as_unit("ns")
15841584
expected = tm.box_expected(expected, box_with_array, False)
15851585
tm.assert_equal(result, expected)
15861586

@@ -2286,7 +2286,7 @@ def test_dti_add_series(self, tz_naive_fixture, names):
22862286
tz = tz_naive_fixture
22872287
index = DatetimeIndex(
22882288
["2016-06-28 05:30", "2016-06-28 05:31"], tz=tz, name=names[0]
2289-
)
2289+
).as_unit("ns")
22902290
ser = Series([Timedelta(seconds=5)] * 2, index=index, name=names[1])
22912291
expected = Series(index + Timedelta(seconds=5), index=index, name=names[2])
22922292

pandas/tests/arithmetic/test_timedelta64.py

+17-9
Original file line numberDiff line numberDiff line change
@@ -344,12 +344,14 @@ def test_subtraction_ops(self):
344344

345345
result = dti - td
346346
expected = DatetimeIndex(
347-
["20121231", "20130101", "20130102"], freq="D", name="bar"
347+
["20121231", "20130101", "20130102"], dtype="M8[ns]", freq="D", name="bar"
348348
)
349349
tm.assert_index_equal(result, expected)
350350

351351
result = dt - tdi
352-
expected = DatetimeIndex(["20121231", NaT, "20121230"], name="foo")
352+
expected = DatetimeIndex(
353+
["20121231", NaT, "20121230"], dtype="M8[ns]", name="foo"
354+
)
353355
tm.assert_index_equal(result, expected)
354356

355357
def test_subtraction_ops_with_tz(self, box_with_array):
@@ -432,7 +434,9 @@ def _check(result, expected):
432434
_check(result, expected)
433435

434436
result = dti_tz - td
435-
expected = DatetimeIndex(["20121231", "20130101", "20130102"], tz="US/Eastern")
437+
expected = DatetimeIndex(
438+
["20121231", "20130101", "20130102"], tz="US/Eastern"
439+
).as_unit("ns")
436440
expected = tm.box_expected(expected, box_with_array)
437441
tm.assert_equal(result, expected)
438442

@@ -450,7 +454,7 @@ def test_dti_tdi_numeric_ops(self):
450454
tm.assert_index_equal(result, expected)
451455

452456
result = dti - tdi # name will be reset
453-
expected = DatetimeIndex(["20121231", NaT, "20130101"])
457+
expected = DatetimeIndex(["20121231", NaT, "20130101"], dtype="M8[ns]")
454458
tm.assert_index_equal(result, expected)
455459

456460
def test_addition_ops(self):
@@ -461,11 +465,15 @@ def test_addition_ops(self):
461465
dt = Timestamp("20130101")
462466

463467
result = tdi + dt
464-
expected = DatetimeIndex(["20130102", NaT, "20130103"], name="foo")
468+
expected = DatetimeIndex(
469+
["20130102", NaT, "20130103"], dtype="M8[ns]", name="foo"
470+
)
465471
tm.assert_index_equal(result, expected)
466472

467473
result = dt + tdi
468-
expected = DatetimeIndex(["20130102", NaT, "20130103"], name="foo")
474+
expected = DatetimeIndex(
475+
["20130102", NaT, "20130103"], dtype="M8[ns]", name="foo"
476+
)
469477
tm.assert_index_equal(result, expected)
470478

471479
result = td + tdi
@@ -492,11 +500,11 @@ def test_addition_ops(self):
492500
# pytest.raises(TypeError, lambda : Index([1,2,3]) + tdi)
493501

494502
result = tdi + dti # name will be reset
495-
expected = DatetimeIndex(["20130102", NaT, "20130105"])
503+
expected = DatetimeIndex(["20130102", NaT, "20130105"], dtype="M8[ns]")
496504
tm.assert_index_equal(result, expected)
497505

498506
result = dti + tdi # name will be reset
499-
expected = DatetimeIndex(["20130102", NaT, "20130105"])
507+
expected = DatetimeIndex(["20130102", NaT, "20130105"], dtype="M8[ns]")
500508
tm.assert_index_equal(result, expected)
501509

502510
result = dt + td
@@ -869,7 +877,7 @@ def test_operators_timedelta64(self):
869877
# timestamp on lhs
870878
result = resultb + df["A"]
871879
values = [Timestamp("20111230"), Timestamp("20120101"), Timestamp("20120103")]
872-
expected = Series(values, name="A")
880+
expected = Series(values, dtype="M8[ns]", name="A")
873881
tm.assert_series_equal(result, expected)
874882

875883
# datetimes on rhs

pandas/tests/arrays/period/test_astype.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,5 +63,5 @@ def test_astype_datetime(dtype):
6363
else:
6464
# GH#45038 allow period->dt64 because we allow dt64->period
6565
result = arr.astype(dtype)
66-
expected = pd.DatetimeIndex(["2000", "2001", pd.NaT])._data
66+
expected = pd.DatetimeIndex(["2000", "2001", pd.NaT], dtype=dtype)._data
6767
tm.assert_datetime_array_equal(result, expected)

pandas/tests/arrays/test_array.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -113,17 +113,17 @@ def test_dt64_array(dtype_unit):
113113
(
114114
pd.DatetimeIndex(["2000", "2001"]),
115115
np.dtype("datetime64[ns]"),
116-
DatetimeArray._from_sequence(["2000", "2001"]),
116+
DatetimeArray._from_sequence(["2000", "2001"], dtype="M8[ns]"),
117117
),
118118
(
119119
pd.DatetimeIndex(["2000", "2001"]),
120120
None,
121-
DatetimeArray._from_sequence(["2000", "2001"]),
121+
DatetimeArray._from_sequence(["2000", "2001"], dtype="M8[ns]"),
122122
),
123123
(
124124
["2000", "2001"],
125125
np.dtype("datetime64[ns]"),
126-
DatetimeArray._from_sequence(["2000", "2001"]),
126+
DatetimeArray._from_sequence(["2000", "2001"], dtype="M8[ns]"),
127127
),
128128
# Datetime (tz-aware)
129129
(

pandas/tests/frame/methods/test_asfreq.py

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ def test_asfreq2(self, frame_or_series):
3232
datetime(2009, 11, 30),
3333
datetime(2009, 12, 31),
3434
],
35+
dtype="M8[ns]",
3536
freq="BME",
3637
),
3738
)

pandas/tests/frame/test_arithmetic.py

+1
Original file line numberDiff line numberDiff line change
@@ -1012,6 +1012,7 @@ def test_frame_with_frame_reindex(self):
10121012
"bar": [pd.Timestamp("2018"), pd.Timestamp("2021")],
10131013
},
10141014
columns=["foo", "bar"],
1015+
dtype="M8[ns]",
10151016
)
10161017
df2 = df[["foo"]]
10171018

pandas/tests/indexes/datetimes/methods/test_astype.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@ def test_dti_astype_asobject_around_dst_transition(self, tzstr):
3939

4040
def test_astype(self):
4141
# GH 13149, GH 13209
42-
idx = DatetimeIndex(["2016-05-16", "NaT", NaT, np.nan], name="idx")
42+
idx = DatetimeIndex(
43+
["2016-05-16", "NaT", NaT, np.nan], dtype="M8[ns]", name="idx"
44+
)
4345

4446
result = idx.astype(object)
4547
expected = Index(
@@ -55,6 +57,7 @@ def test_astype(self):
5557
)
5658
tm.assert_index_equal(result, expected)
5759

60+
def test_astype2(self):
5861
rng = date_range("1/1/2000", periods=10, name="idx")
5962
result = rng.astype("i8")
6063
tm.assert_index_equal(result, Index(rng.asi8, name="idx"))
@@ -160,7 +163,9 @@ def test_astype_str_freq_and_tz(self):
160163

161164
def test_astype_datetime64(self):
162165
# GH 13149, GH 13209
163-
idx = DatetimeIndex(["2016-05-16", "NaT", NaT, np.nan], name="idx")
166+
idx = DatetimeIndex(
167+
["2016-05-16", "NaT", NaT, np.nan], dtype="M8[ns]", name="idx"
168+
)
164169

165170
result = idx.astype("datetime64[ns]")
166171
tm.assert_index_equal(result, idx)

pandas/tests/indexes/datetimes/methods/test_tz_localize.py

+1-4
Original file line numberDiff line numberDiff line change
@@ -294,10 +294,7 @@ def test_dti_tz_localize_ambiguous_flags(self, tz, unit):
294294
tm.assert_index_equal(expected, localized)
295295

296296
result = DatetimeIndex(times, tz=tz, ambiguous=is_dst).as_unit(unit)
297-
tm.assert_index_equal(
298-
result,
299-
expected,
300-
)
297+
tm.assert_index_equal(result, expected)
301298

302299
localized = di.tz_localize(tz, ambiguous=np.array(is_dst))
303300
tm.assert_index_equal(dr, localized)

pandas/tests/indexes/datetimes/methods/test_unique.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ def test_index_unique(rand_series_with_duplicate_datetimeindex):
3434
datetime(2000, 1, 4),
3535
datetime(2000, 1, 5),
3636
],
37-
dtype="M8[ns]",
37+
dtype=index.dtype,
3838
)
39-
assert uniques.dtype == "M8[ns]" # sanity
39+
assert uniques.dtype == index.dtype # sanity
4040
tm.assert_index_equal(uniques, expected)
4141
assert index.nunique() == 4
4242

pandas/tests/resample/test_period_index.py

+10-8
Original file line numberDiff line numberDiff line change
@@ -608,8 +608,10 @@ def test_resample_with_dst_time_change(self):
608608
"2016-03-15 01:00:00-05:00",
609609
"2016-03-15 13:00:00-05:00",
610610
]
611-
index = pd.to_datetime(expected_index_values, utc=True).tz_convert(
612-
"America/Chicago"
611+
index = (
612+
pd.to_datetime(expected_index_values, utc=True)
613+
.tz_convert("America/Chicago")
614+
.as_unit(index.unit)
613615
)
614616
index = pd.DatetimeIndex(index, freq="12h")
615617
expected = DataFrame(
@@ -668,15 +670,15 @@ def test_default_left_closed_label(self, from_freq, to_freq):
668670
)
669671

670672
def test_all_values_single_bin(self):
671-
# 2070
673+
# GH#2070
672674
index = period_range(start="2012-01-01", end="2012-12-31", freq="M")
673-
s = Series(np.random.default_rng(2).standard_normal(len(index)), index=index)
675+
ser = Series(np.random.default_rng(2).standard_normal(len(index)), index=index)
674676

675-
result = s.resample("Y").mean()
676-
tm.assert_almost_equal(result.iloc[0], s.mean())
677+
result = ser.resample("Y").mean()
678+
tm.assert_almost_equal(result.iloc[0], ser.mean())
677679

678680
def test_evenly_divisible_with_no_extra_bins(self):
679-
# 4076
681+
# GH#4076
680682
# when the frequency is evenly divisible, sometimes extra bins
681683

682684
df = DataFrame(
@@ -686,7 +688,7 @@ def test_evenly_divisible_with_no_extra_bins(self):
686688
result = df.resample("5D").mean()
687689
expected = pd.concat([df.iloc[0:5].mean(), df.iloc[5:].mean()], axis=1).T
688690
expected.index = pd.DatetimeIndex(
689-
[Timestamp("2000-1-1"), Timestamp("2000-1-6")], freq="5D"
691+
[Timestamp("2000-1-1"), Timestamp("2000-1-6")], dtype="M8[ns]", freq="5D"
690692
)
691693
tm.assert_frame_equal(result, expected)
692694

pandas/tests/resample/test_resample_api.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,10 @@ def test_resample_group_keys():
116116

117117
# group_keys=True
118118
expected.index = pd.MultiIndex.from_arrays(
119-
[pd.to_datetime(["2000-01-01", "2000-01-06"]).repeat(5), expected.index]
119+
[
120+
pd.to_datetime(["2000-01-01", "2000-01-06"]).as_unit("ns").repeat(5),
121+
expected.index,
122+
]
120123
)
121124
g = df.resample("5D", group_keys=True)
122125
result = g.apply(lambda x: x)

pandas/tests/resample/test_time_grouper.py

+17-7
Original file line numberDiff line numberDiff line change
@@ -193,11 +193,11 @@ def test_aggregate_nth():
193193
],
194194
)
195195
def test_resample_entirely_nat_window(method, method_args, unit):
196-
s = Series([0] * 2 + [np.nan] * 2, index=date_range("2017", periods=4))
197-
result = methodcaller(method, **method_args)(s.resample("2d"))
198-
expected = Series(
199-
[0.0, unit], index=pd.DatetimeIndex(["2017-01-01", "2017-01-03"], freq="2D")
200-
)
196+
ser = Series([0] * 2 + [np.nan] * 2, index=date_range("2017", periods=4))
197+
result = methodcaller(method, **method_args)(ser.resample("2d"))
198+
199+
exp_dti = pd.DatetimeIndex(["2017-01-01", "2017-01-03"], dtype="M8[ns]", freq="2D")
200+
expected = Series([0.0, unit], index=exp_dti)
201201
tm.assert_series_equal(result, expected)
202202

203203

@@ -233,7 +233,13 @@ def test_aggregate_with_nat(func, fill_value):
233233
pad = DataFrame([[fill_value] * 4], index=[3], columns=["A", "B", "C", "D"])
234234
expected = pd.concat([normal_result, pad])
235235
expected = expected.sort_index()
236-
dti = date_range(start="2013-01-01", freq="D", periods=5, name="key")
236+
dti = date_range(
237+
start="2013-01-01",
238+
freq="D",
239+
periods=5,
240+
name="key",
241+
unit=dt_df["key"]._values.unit,
242+
)
237243
expected.index = dti._with_freq(None) # TODO: is this desired?
238244
tm.assert_frame_equal(expected, dt_result)
239245
assert dt_result.index.name == "key"
@@ -265,7 +271,11 @@ def test_aggregate_with_nat_size():
265271
expected = pd.concat([normal_result, pad])
266272
expected = expected.sort_index()
267273
expected.index = date_range(
268-
start="2013-01-01", freq="D", periods=5, name="key"
274+
start="2013-01-01",
275+
freq="D",
276+
periods=5,
277+
name="key",
278+
unit=dt_df["key"]._values.unit,
269279
)._with_freq(None)
270280
tm.assert_series_equal(expected, dt_result)
271281
assert dt_result.index.name == "key"

0 commit comments

Comments
 (0)