Skip to content

Commit 713c4dc

Browse files
authored
TST: parametrize over dt64 unit (#55974)
* TST: parametrize tests over dt64 unit * TST: parametrize over unit * revert * TST: parametrize over dt64 unit * xfail non-nano * revert
1 parent 3bb8ad1 commit 713c4dc

18 files changed

+247
-130
lines changed

pandas/tests/arrays/test_datetimelike.py

+10-4
Original file line numberDiff line numberDiff line change
@@ -853,10 +853,14 @@ def test_concat_same_type_invalid(self, arr1d):
853853
with pytest.raises(ValueError, match="to_concat must have the same"):
854854
arr._concat_same_type([arr, other])
855855

856-
def test_concat_same_type_different_freq(self):
856+
def test_concat_same_type_different_freq(self, unit):
857857
# we *can* concatenate DTI with different freqs.
858-
a = DatetimeArray(pd.date_range("2000", periods=2, freq="D", tz="US/Central"))
859-
b = DatetimeArray(pd.date_range("2000", periods=2, freq="h", tz="US/Central"))
858+
a = DatetimeArray(
859+
pd.date_range("2000", periods=2, freq="D", tz="US/Central", unit=unit)
860+
)
861+
b = DatetimeArray(
862+
pd.date_range("2000", periods=2, freq="h", tz="US/Central", unit=unit)
863+
)
860864
result = DatetimeArray._concat_same_type([a, b])
861865
expected = DatetimeArray(
862866
pd.to_datetime(
@@ -866,7 +870,9 @@ def test_concat_same_type_different_freq(self):
866870
"2000-01-01 00:00:00",
867871
"2000-01-01 01:00:00",
868872
]
869-
).tz_localize("US/Central")
873+
)
874+
.tz_localize("US/Central")
875+
.as_unit(unit)
870876
)
871877

872878
tm.assert_datetime_array_equal(result, expected)

pandas/tests/groupby/methods/test_nth.py

+9-5
Original file line numberDiff line numberDiff line change
@@ -286,8 +286,10 @@ def test_nth5():
286286
tm.assert_frame_equal(gb.nth([3, 4]), df.loc[[]])
287287

288288

289-
def test_nth_bdays():
290-
business_dates = pd.date_range(start="4/1/2014", end="6/30/2014", freq="B")
289+
def test_nth_bdays(unit):
290+
business_dates = pd.date_range(
291+
start="4/1/2014", end="6/30/2014", freq="B", unit=unit
292+
)
291293
df = DataFrame(1, index=business_dates, columns=["a", "b"])
292294
# get the first, fourth and last two business days for each month
293295
key = [df.index.year, df.index.month]
@@ -307,7 +309,7 @@ def test_nth_bdays():
307309
"2014/6/27",
308310
"2014/6/30",
309311
]
310-
)
312+
).as_unit(unit)
311313
expected = DataFrame(1, columns=["a", "b"], index=expected_dates)
312314
tm.assert_frame_equal(result, expected)
313315

@@ -401,14 +403,15 @@ def test_first_last_tz(data, expected_first, expected_last):
401403
["last", Timestamp("2013-01-02", tz="US/Eastern"), "b"],
402404
],
403405
)
404-
def test_first_last_tz_multi_column(method, ts, alpha):
406+
def test_first_last_tz_multi_column(method, ts, alpha, unit):
405407
# GH 21603
406408
category_string = Series(list("abc")).astype("category")
409+
dti = pd.date_range("20130101", periods=3, tz="US/Eastern", unit=unit)
407410
df = DataFrame(
408411
{
409412
"group": [1, 1, 2],
410413
"category_string": category_string,
411-
"datetimetz": pd.date_range("20130101", periods=3, tz="US/Eastern"),
414+
"datetimetz": dti,
412415
}
413416
)
414417
result = getattr(df.groupby("group"), method)()
@@ -421,6 +424,7 @@ def test_first_last_tz_multi_column(method, ts, alpha):
421424
},
422425
index=Index([1, 2], name="group"),
423426
)
427+
expected["datetimetz"] = expected["datetimetz"].dt.as_unit(unit)
424428
tm.assert_frame_equal(result, expected)
425429

426430

pandas/tests/groupby/methods/test_quantile.py

+7-13
Original file line numberDiff line numberDiff line change
@@ -415,36 +415,30 @@ def test_columns_groupby_quantile():
415415
tm.assert_frame_equal(result, expected)
416416

417417

418-
def test_timestamp_groupby_quantile():
418+
def test_timestamp_groupby_quantile(unit):
419419
# GH 33168
420+
dti = pd.date_range(
421+
start="2020-04-19 00:00:00", freq="1min", periods=100, tz="UTC", unit=unit
422+
).floor("1h")
420423
df = DataFrame(
421424
{
422-
"timestamp": pd.date_range(
423-
start="2020-04-19 00:00:00", freq="1min", periods=100, tz="UTC"
424-
).floor("1h"),
425+
"timestamp": dti,
425426
"category": list(range(1, 101)),
426427
"value": list(range(101, 201)),
427428
}
428429
)
429430

430431
result = df.groupby("timestamp").quantile([0.2, 0.8])
431432

433+
mi = pd.MultiIndex.from_product([dti[::99], [0.2, 0.8]], names=("timestamp", None))
432434
expected = DataFrame(
433435
[
434436
{"category": 12.8, "value": 112.8},
435437
{"category": 48.2, "value": 148.2},
436438
{"category": 68.8, "value": 168.8},
437439
{"category": 92.2, "value": 192.2},
438440
],
439-
index=pd.MultiIndex.from_tuples(
440-
[
441-
(pd.Timestamp("2020-04-19 00:00:00+00:00"), 0.2),
442-
(pd.Timestamp("2020-04-19 00:00:00+00:00"), 0.8),
443-
(pd.Timestamp("2020-04-19 01:00:00+00:00"), 0.2),
444-
(pd.Timestamp("2020-04-19 01:00:00+00:00"), 0.8),
445-
],
446-
names=("timestamp", None),
447-
),
441+
index=mi,
448442
)
449443

450444
tm.assert_frame_equal(result, expected)

pandas/tests/groupby/methods/test_value_counts.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -1135,7 +1135,7 @@ def test_subset_duplicate_columns():
11351135

11361136

11371137
@pytest.mark.parametrize("utc", [True, False])
1138-
def test_value_counts_time_grouper(utc):
1138+
def test_value_counts_time_grouper(utc, unit):
11391139
# GH#50486
11401140
df = DataFrame(
11411141
{
@@ -1152,12 +1152,12 @@ def test_value_counts_time_grouper(utc):
11521152
}
11531153
).drop([3])
11541154

1155-
df["Datetime"] = to_datetime(df["Timestamp"], utc=utc, unit="s")
1155+
df["Datetime"] = to_datetime(df["Timestamp"], utc=utc, unit="s").dt.as_unit(unit)
11561156
gb = df.groupby(Grouper(freq="1D", key="Datetime"))
11571157
result = gb.value_counts()
11581158
dates = to_datetime(
11591159
["2019-08-06", "2019-08-07", "2019-08-09", "2019-08-10"], utc=utc
1160-
)
1160+
).as_unit(unit)
11611161
timestamps = df["Timestamp"].unique()
11621162
index = MultiIndex(
11631163
levels=[dates, timestamps, ["apple", "banana", "orange", "pear"]],

pandas/tests/groupby/test_groupby.py

+13-9
Original file line numberDiff line numberDiff line change
@@ -3172,28 +3172,32 @@ def test_groupby_selection_other_methods(df):
31723172
)
31733173

31743174

3175-
def test_groupby_with_Time_Grouper():
3176-
idx2 = [
3177-
to_datetime("2016-08-31 22:08:12.000"),
3178-
to_datetime("2016-08-31 22:09:12.200"),
3179-
to_datetime("2016-08-31 22:20:12.400"),
3180-
]
3175+
def test_groupby_with_Time_Grouper(unit):
3176+
idx2 = to_datetime(
3177+
[
3178+
"2016-08-31 22:08:12.000",
3179+
"2016-08-31 22:09:12.200",
3180+
"2016-08-31 22:20:12.400",
3181+
]
3182+
).as_unit(unit)
31813183

31823184
test_data = DataFrame(
31833185
{"quant": [1.0, 1.0, 3.0], "quant2": [1.0, 1.0, 3.0], "time2": idx2}
31843186
)
31853187

3188+
time2 = date_range("2016-08-31 22:08:00", periods=13, freq="1min", unit=unit)
31863189
expected_output = DataFrame(
31873190
{
3188-
"time2": date_range("2016-08-31 22:08:00", periods=13, freq="1min"),
3191+
"time2": time2,
31893192
"quant": [1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
31903193
"quant2": [1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
31913194
}
31923195
)
31933196

3194-
df = test_data.groupby(Grouper(key="time2", freq="1min")).count().reset_index()
3197+
gb = test_data.groupby(Grouper(key="time2", freq="1min"))
3198+
result = gb.count().reset_index()
31953199

3196-
tm.assert_frame_equal(df, expected_output)
3200+
tm.assert_frame_equal(result, expected_output)
31973201

31983202

31993203
def test_groupby_series_with_datetimeindex_month_name():

pandas/tests/groupby/test_grouping.py

+11-6
Original file line numberDiff line numberDiff line change
@@ -276,19 +276,24 @@ def test_grouper_creation_bug2(self):
276276
result = g.sum()
277277
tm.assert_frame_equal(result, expected)
278278

279-
def test_grouper_creation_bug3(self):
279+
def test_grouper_creation_bug3(self, unit):
280280
# GH8866
281+
dti = date_range("20130101", periods=2, unit=unit)
282+
mi = MultiIndex.from_product(
283+
[list("ab"), range(2), dti],
284+
names=["one", "two", "three"],
285+
)
281286
ser = Series(
282287
np.arange(8, dtype="int64"),
283-
index=MultiIndex.from_product(
284-
[list("ab"), range(2), date_range("20130101", periods=2)],
285-
names=["one", "two", "three"],
286-
),
288+
index=mi,
287289
)
288290
result = ser.groupby(Grouper(level="three", freq="ME")).sum()
291+
exp_dti = pd.DatetimeIndex(
292+
[Timestamp("2013-01-31")], freq="ME", name="three"
293+
).as_unit(unit)
289294
expected = Series(
290295
[28],
291-
index=pd.DatetimeIndex([Timestamp("2013-01-31")], freq="ME", name="three"),
296+
index=exp_dti,
292297
)
293298
tm.assert_series_equal(result, expected)
294299

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

+7-19
Original file line numberDiff line numberDiff line change
@@ -116,37 +116,25 @@ def test_delete_slice(self, unit):
116116

117117
# TODO: belongs in Series.drop tests?
118118
@pytest.mark.parametrize("tz", [None, "Asia/Tokyo", "US/Pacific"])
119-
def test_delete_slice2(self, tz):
119+
def test_delete_slice2(self, tz, unit):
120+
dti = date_range(
121+
"2000-01-01 09:00", periods=10, freq="h", name="idx", tz=tz, unit=unit
122+
)
120123
ts = Series(
121124
1,
122-
index=date_range(
123-
"2000-01-01 09:00", periods=10, freq="h", name="idx", tz=tz
124-
),
125+
index=dti,
125126
)
126127
# preserve freq
127128
result = ts.drop(ts.index[:5]).index
128-
expected = date_range(
129-
"2000-01-01 14:00", periods=5, freq="h", name="idx", tz=tz
130-
)
129+
expected = dti[5:]
131130
tm.assert_index_equal(result, expected)
132131
assert result.name == expected.name
133132
assert result.freq == expected.freq
134133
assert result.tz == expected.tz
135134

136135
# reset freq to None
137136
result = ts.drop(ts.index[[1, 3, 5, 7, 9]]).index
138-
expected = DatetimeIndex(
139-
[
140-
"2000-01-01 09:00",
141-
"2000-01-01 11:00",
142-
"2000-01-01 13:00",
143-
"2000-01-01 15:00",
144-
"2000-01-01 17:00",
145-
],
146-
freq=None,
147-
name="idx",
148-
tz=tz,
149-
)
137+
expected = dti[::2]._with_freq(None)
150138
tm.assert_index_equal(result, expected)
151139
assert result.name == expected.name
152140
assert result.freq == expected.freq

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

+12-12
Original file line numberDiff line numberDiff line change
@@ -17,29 +17,29 @@ def test_repeat_range(self, tz_naive_fixture):
1717
assert result.freq is None
1818
assert len(result) == 5 * len(rng)
1919

20-
def test_repeat_range2(self, tz_naive_fixture):
20+
def test_repeat_range2(self, tz_naive_fixture, unit):
2121
tz = tz_naive_fixture
22-
index = date_range("2001-01-01", periods=2, freq="D", tz=tz)
22+
index = date_range("2001-01-01", periods=2, freq="D", tz=tz, unit=unit)
2323
exp = DatetimeIndex(
2424
["2001-01-01", "2001-01-01", "2001-01-02", "2001-01-02"], tz=tz
25-
)
25+
).as_unit(unit)
2626
for res in [index.repeat(2), np.repeat(index, 2)]:
2727
tm.assert_index_equal(res, exp)
2828
assert res.freq is None
2929

30-
def test_repeat_range3(self, tz_naive_fixture):
30+
def test_repeat_range3(self, tz_naive_fixture, unit):
3131
tz = tz_naive_fixture
32-
index = date_range("2001-01-01", periods=2, freq="2D", tz=tz)
32+
index = date_range("2001-01-01", periods=2, freq="2D", tz=tz, unit=unit)
3333
exp = DatetimeIndex(
3434
["2001-01-01", "2001-01-01", "2001-01-03", "2001-01-03"], tz=tz
35-
)
35+
).as_unit(unit)
3636
for res in [index.repeat(2), np.repeat(index, 2)]:
3737
tm.assert_index_equal(res, exp)
3838
assert res.freq is None
3939

40-
def test_repeat_range4(self, tz_naive_fixture):
40+
def test_repeat_range4(self, tz_naive_fixture, unit):
4141
tz = tz_naive_fixture
42-
index = DatetimeIndex(["2001-01-01", "NaT", "2003-01-01"], tz=tz)
42+
index = DatetimeIndex(["2001-01-01", "NaT", "2003-01-01"], tz=tz).as_unit(unit)
4343
exp = DatetimeIndex(
4444
[
4545
"2001-01-01",
@@ -53,17 +53,17 @@ def test_repeat_range4(self, tz_naive_fixture):
5353
"2003-01-01",
5454
],
5555
tz=tz,
56-
)
56+
).as_unit(unit)
5757
for res in [index.repeat(3), np.repeat(index, 3)]:
5858
tm.assert_index_equal(res, exp)
5959
assert res.freq is None
6060

61-
def test_repeat(self, tz_naive_fixture):
61+
def test_repeat(self, tz_naive_fixture, unit):
6262
tz = tz_naive_fixture
6363
reps = 2
6464
msg = "the 'axis' parameter is not supported"
6565

66-
rng = date_range(start="2016-01-01", periods=2, freq="30Min", tz=tz)
66+
rng = date_range(start="2016-01-01", periods=2, freq="30Min", tz=tz, unit=unit)
6767

6868
expected_rng = DatetimeIndex(
6969
[
@@ -72,7 +72,7 @@ def test_repeat(self, tz_naive_fixture):
7272
Timestamp("2016-01-01 00:30:00", tz=tz),
7373
Timestamp("2016-01-01 00:30:00", tz=tz),
7474
]
75-
)
75+
).as_unit(unit)
7676

7777
res = rng.repeat(reps)
7878
tm.assert_index_equal(res, expected_rng)

0 commit comments

Comments
 (0)