Skip to content

Commit dbaaa1e

Browse files
jbrockmendelrhshadrach
authored andcommitted
TST: parametrize test_partial_slicing tests (pandas-dev#33873)
1 parent 47f8eda commit dbaaa1e

File tree

5 files changed

+195
-253
lines changed

5 files changed

+195
-253
lines changed

pandas/tests/indexes/datetimes/test_partial_slicing.py

Lines changed: 29 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import numpy as np
77
import pytest
88

9-
import pandas as pd
109
from pandas import (
1110
DataFrame,
1211
DatetimeIndex,
@@ -21,69 +20,26 @@
2120

2221

2322
class TestSlicing:
24-
def test_slice_with_negative_step(self):
25-
ts = Series(np.arange(20), date_range("2014-01-01", periods=20, freq="MS"))
26-
SLC = pd.IndexSlice
27-
28-
def assert_slices_equivalent(l_slc, i_slc):
29-
expected = ts.iloc[i_slc]
30-
31-
tm.assert_series_equal(ts[l_slc], expected)
32-
tm.assert_series_equal(ts.loc[l_slc], expected)
33-
tm.assert_series_equal(ts.loc[l_slc], expected)
34-
35-
assert_slices_equivalent(SLC[Timestamp("2014-10-01") :: -1], SLC[9::-1])
36-
assert_slices_equivalent(SLC["2014-10-01"::-1], SLC[9::-1])
37-
38-
assert_slices_equivalent(SLC[: Timestamp("2014-10-01") : -1], SLC[:8:-1])
39-
assert_slices_equivalent(SLC[:"2014-10-01":-1], SLC[:8:-1])
40-
41-
assert_slices_equivalent(SLC["2015-02-01":"2014-10-01":-1], SLC[13:8:-1])
42-
assert_slices_equivalent(
43-
SLC[Timestamp("2015-02-01") : Timestamp("2014-10-01") : -1], SLC[13:8:-1]
44-
)
45-
assert_slices_equivalent(
46-
SLC["2015-02-01" : Timestamp("2014-10-01") : -1], SLC[13:8:-1]
47-
)
48-
assert_slices_equivalent(
49-
SLC[Timestamp("2015-02-01") : "2014-10-01" : -1], SLC[13:8:-1]
50-
)
51-
52-
assert_slices_equivalent(SLC["2014-10-01":"2015-02-01":-1], SLC[0:0:-1])
53-
54-
def test_slice_with_zero_step_raises(self):
55-
ts = Series(np.arange(20), date_range("2014-01-01", periods=20, freq="MS"))
56-
with pytest.raises(ValueError, match="slice step cannot be zero"):
57-
ts[::0]
58-
with pytest.raises(ValueError, match="slice step cannot be zero"):
59-
ts.loc[::0]
60-
with pytest.raises(ValueError, match="slice step cannot be zero"):
61-
ts.loc[::0]
62-
6323
def test_monotone_DTI_indexing_bug(self):
6424
# GH 19362
6525
# Testing accessing the first element in a monotonic descending
6626
# partial string indexing.
6727

68-
df = pd.DataFrame(list(range(5)))
28+
df = DataFrame(list(range(5)))
6929
date_list = [
7030
"2018-01-02",
7131
"2017-02-10",
7232
"2016-03-10",
7333
"2015-03-15",
7434
"2014-03-16",
7535
]
76-
date_index = pd.to_datetime(date_list)
36+
date_index = DatetimeIndex(date_list)
7737
df["date"] = date_index
78-
expected = pd.DataFrame({0: list(range(5)), "date": date_index})
38+
expected = DataFrame({0: list(range(5)), "date": date_index})
7939
tm.assert_frame_equal(df, expected)
8040

81-
df = pd.DataFrame(
82-
{"A": [1, 2, 3]}, index=pd.date_range("20170101", periods=3)[::-1]
83-
)
84-
expected = pd.DataFrame(
85-
{"A": 1}, index=pd.date_range("20170103", periods=1)[::-1]
86-
)
41+
df = DataFrame({"A": [1, 2, 3]}, index=date_range("20170101", periods=3)[::-1])
42+
expected = DataFrame({"A": 1}, index=date_range("20170103", periods=1)[::-1])
8743
tm.assert_frame_equal(df.loc["2017-01-03"], expected)
8844

8945
def test_slice_year(self):
@@ -120,7 +76,7 @@ def test_slice_end_of_period_resolution(self, partial_dtime):
12076
# GH#31064
12177
dti = date_range("2019-12-31 23:59:55.999999999", periods=10, freq="s")
12278

123-
ser = pd.Series(range(10), index=dti)
79+
ser = Series(range(10), index=dti)
12480
result = ser[partial_dtime]
12581
expected = ser.iloc[:5]
12682
tm.assert_series_equal(result, expected)
@@ -321,7 +277,7 @@ def test_partial_slicing_with_multiindex(self):
321277
tm.assert_frame_equal(result, expected)
322278

323279
expected = df_multi.loc[
324-
(pd.Timestamp("2013-06-19 09:30:00", tz=None), "ACCT1", "ABC")
280+
(Timestamp("2013-06-19 09:30:00", tz=None), "ACCT1", "ABC")
325281
]
326282
result = df_multi.loc[("2013-06-19 09:30:00", "ACCT1", "ABC")]
327283
tm.assert_series_equal(result, expected)
@@ -334,31 +290,31 @@ def test_partial_slicing_with_multiindex(self):
334290

335291
# GH 4294
336292
# partial slice on a series mi
337-
s = pd.DataFrame(
338-
np.random.rand(1000, 1000), index=pd.date_range("2000-1-1", periods=1000)
293+
s = DataFrame(
294+
np.random.rand(1000, 1000), index=date_range("2000-1-1", periods=1000)
339295
).stack()
340296

341297
s2 = s[:-1].copy()
342298
expected = s2["2000-1-4"]
343-
result = s2[pd.Timestamp("2000-1-4")]
299+
result = s2[Timestamp("2000-1-4")]
344300
tm.assert_series_equal(result, expected)
345301

346-
result = s[pd.Timestamp("2000-1-4")]
302+
result = s[Timestamp("2000-1-4")]
347303
expected = s["2000-1-4"]
348304
tm.assert_series_equal(result, expected)
349305

350-
df2 = pd.DataFrame(s)
306+
df2 = DataFrame(s)
351307
expected = df2.xs("2000-1-4")
352-
result = df2.loc[pd.Timestamp("2000-1-4")]
308+
result = df2.loc[Timestamp("2000-1-4")]
353309
tm.assert_frame_equal(result, expected)
354310

355311
def test_partial_slice_doesnt_require_monotonicity(self):
356312
# For historical reasons.
357-
s = pd.Series(np.arange(10), pd.date_range("2014-01-01", periods=10))
313+
s = Series(np.arange(10), date_range("2014-01-01", periods=10))
358314

359315
nonmonotonic = s[[3, 5, 4]]
360316
expected = nonmonotonic.iloc[:0]
361-
timestamp = pd.Timestamp("2014-01-10")
317+
timestamp = Timestamp("2014-01-10")
362318

363319
tm.assert_series_equal(nonmonotonic["2014-01-10":], expected)
364320
with pytest.raises(KeyError, match=r"Timestamp\('2014-01-10 00:00:00'\)"):
@@ -370,9 +326,9 @@ def test_partial_slice_doesnt_require_monotonicity(self):
370326

371327
def test_loc_datetime_length_one(self):
372328
# GH16071
373-
df = pd.DataFrame(
329+
df = DataFrame(
374330
columns=["1"],
375-
index=pd.date_range("2016-10-01T00:00:00", "2016-10-01T23:59:59"),
331+
index=date_range("2016-10-01T00:00:00", "2016-10-01T23:59:59"),
376332
)
377333
result = df.loc[datetime(2016, 10, 1) :]
378334
tm.assert_frame_equal(result, df)
@@ -403,10 +359,10 @@ def test_selection_by_datetimelike(self, datetimelike, op, expected):
403359
df = DataFrame(
404360
{
405361
"A": [
406-
pd.Timestamp("20120101"),
407-
pd.Timestamp("20130101"),
362+
Timestamp("20120101"),
363+
Timestamp("20130101"),
408364
np.nan,
409-
pd.Timestamp("20130103"),
365+
Timestamp("20130103"),
410366
]
411367
}
412368
)
@@ -418,26 +374,26 @@ def test_selection_by_datetimelike(self, datetimelike, op, expected):
418374
"start",
419375
[
420376
"2018-12-02 21:50:00+00:00",
421-
pd.Timestamp("2018-12-02 21:50:00+00:00"),
422-
pd.Timestamp("2018-12-02 21:50:00+00:00").to_pydatetime(),
377+
Timestamp("2018-12-02 21:50:00+00:00"),
378+
Timestamp("2018-12-02 21:50:00+00:00").to_pydatetime(),
423379
],
424380
)
425381
@pytest.mark.parametrize(
426382
"end",
427383
[
428384
"2018-12-02 21:52:00+00:00",
429-
pd.Timestamp("2018-12-02 21:52:00+00:00"),
430-
pd.Timestamp("2018-12-02 21:52:00+00:00").to_pydatetime(),
385+
Timestamp("2018-12-02 21:52:00+00:00"),
386+
Timestamp("2018-12-02 21:52:00+00:00").to_pydatetime(),
431387
],
432388
)
433389
def test_getitem_with_datestring_with_UTC_offset(self, start, end):
434390
# GH 24076
435-
idx = pd.date_range(
391+
idx = date_range(
436392
start="2018-12-02 14:50:00-07:00",
437393
end="2018-12-02 14:50:00-07:00",
438394
freq="1min",
439395
)
440-
df = pd.DataFrame(1, index=idx, columns=["A"])
396+
df = DataFrame(1, index=idx, columns=["A"])
441397
result = df[start:end]
442398
expected = df.iloc[0:3, :]
443399
tm.assert_frame_equal(result, expected)
@@ -454,11 +410,9 @@ def test_getitem_with_datestring_with_UTC_offset(self, start, end):
454410

455411
def test_slice_reduce_to_series(self):
456412
# GH 27516
457-
df = pd.DataFrame(
458-
{"A": range(24)}, index=pd.date_range("2000", periods=24, freq="M")
459-
)
460-
expected = pd.Series(
461-
range(12), index=pd.date_range("2000", periods=12, freq="M"), name="A"
413+
df = DataFrame({"A": range(24)}, index=date_range("2000", periods=24, freq="M"))
414+
expected = Series(
415+
range(12), index=date_range("2000", periods=12, freq="M"), name="A"
462416
)
463417
result = df.loc["2000", "A"]
464418
tm.assert_series_equal(result, expected)
Lines changed: 33 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,11 @@
1-
import numpy as np
21
import pytest
32

4-
import pandas as pd
5-
from pandas import DataFrame, MultiIndex, date_range
3+
from pandas import DataFrame, IndexSlice, MultiIndex, date_range
64
import pandas._testing as tm
75

86

9-
def test_partial_string_timestamp_multiindex():
10-
# GH10331
11-
dr = pd.date_range("2016-01-01", "2016-01-03", freq="12H")
12-
abc = ["a", "b", "c"]
13-
ix = pd.MultiIndex.from_product([dr, abc])
14-
df = pd.DataFrame({"c1": range(0, 15)}, index=ix)
15-
idx = pd.IndexSlice
16-
7+
@pytest.fixture
8+
def df():
179
# c1
1810
# 2016-01-01 00:00:00 a 0
1911
# b 1
@@ -30,33 +22,39 @@ def test_partial_string_timestamp_multiindex():
3022
# 2016-01-03 00:00:00 a 12
3123
# b 13
3224
# c 14
25+
dr = date_range("2016-01-01", "2016-01-03", freq="12H")
26+
abc = ["a", "b", "c"]
27+
mi = MultiIndex.from_product([dr, abc])
28+
frame = DataFrame({"c1": range(0, 15)}, index=mi)
29+
return frame
3330

31+
32+
def test_partial_string_matching_single_index(df):
3433
# partial string matching on a single index
35-
for df_swap in (df.swaplevel(), df.swaplevel(0), df.swaplevel(0, 1)):
34+
for df_swap in [df.swaplevel(), df.swaplevel(0), df.swaplevel(0, 1)]:
3635
df_swap = df_swap.sort_index()
3736
just_a = df_swap.loc["a"]
3837
result = just_a.loc["2016-01-01"]
39-
expected = df.loc[idx[:, "a"], :].iloc[0:2]
38+
expected = df.loc[IndexSlice[:, "a"], :].iloc[0:2]
4039
expected.index = expected.index.droplevel(1)
4140
tm.assert_frame_equal(result, expected)
4241

42+
43+
def test_partial_string_timestamp_multiindex(df):
44+
# GH10331
45+
df_swap = df.swaplevel(0, 1).sort_index()
46+
SLC = IndexSlice
47+
4348
# indexing with IndexSlice
44-
result = df.loc[idx["2016-01-01":"2016-02-01", :], :]
49+
result = df.loc[SLC["2016-01-01":"2016-02-01", :], :]
4550
expected = df
4651
tm.assert_frame_equal(result, expected)
4752

4853
# match on secondary index
49-
result = df_swap.loc[idx[:, "2016-01-01":"2016-01-01"], :]
54+
result = df_swap.loc[SLC[:, "2016-01-01":"2016-01-01"], :]
5055
expected = df_swap.iloc[[0, 1, 5, 6, 10, 11]]
5156
tm.assert_frame_equal(result, expected)
5257

53-
# Even though this syntax works on a single index, this is somewhat
54-
# ambiguous and we don't want to extend this behavior forward to work
55-
# in multi-indexes. This would amount to selecting a scalar from a
56-
# column.
57-
with pytest.raises(KeyError, match="'2016-01-01'"):
58-
df["2016-01-01"]
59-
6058
# partial string match on year only
6159
result = df.loc["2016"]
6260
expected = df
@@ -73,7 +71,7 @@ def test_partial_string_timestamp_multiindex():
7371
tm.assert_frame_equal(result, expected)
7472

7573
# partial string match on secondary index
76-
result = df_swap.loc[idx[:, "2016-01-02"], :]
74+
result = df_swap.loc[SLC[:, "2016-01-02"], :]
7775
expected = df_swap.iloc[[2, 3, 7, 8, 12, 13]]
7876
tm.assert_frame_equal(result, expected)
7977

@@ -86,11 +84,18 @@ def test_partial_string_timestamp_multiindex():
8684
with pytest.raises(KeyError, match="'2016-01-01'"):
8785
df_swap.loc["2016-01-01"]
8886

89-
# GH12685 (partial string with daily resolution or below)
90-
dr = date_range("2013-01-01", periods=100, freq="D")
91-
ix = MultiIndex.from_product([dr, ["a", "b"]])
92-
df = DataFrame(np.random.randn(200, 1), columns=["A"], index=ix)
9387

94-
result = df.loc[idx["2013-03":"2013-03", :], :]
88+
def test_partial_string_timestamp_multiindex_str_key_raises(df):
89+
# Even though this syntax works on a single index, this is somewhat
90+
# ambiguous and we don't want to extend this behavior forward to work
91+
# in multi-indexes. This would amount to selecting a scalar from a
92+
# column.
93+
with pytest.raises(KeyError, match="'2016-01-01'"):
94+
df["2016-01-01"]
95+
96+
97+
def test_partial_string_timestamp_multiindex_daily_resolution(df):
98+
# GH12685 (partial string with daily resolution or below)
99+
result = df.loc[IndexSlice["2013-03":"2013-03", :], :]
95100
expected = df.iloc[118:180]
96101
tm.assert_frame_equal(result, expected)

0 commit comments

Comments
 (0)