Skip to content

Commit 9e8ab56

Browse files
jbrockmendelnickleus27
authored andcommitted
REF/TST: collect index tests (pandas-dev#44377)
1 parent 7a31ca8 commit 9e8ab56

17 files changed

+315
-283
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
from pandas import (
2+
Index,
3+
NaT,
4+
date_range,
5+
)
6+
7+
8+
def test_is_monotonic_with_nat():
9+
# GH#31437
10+
# PeriodIndex.is_monotonic should behave analogously to DatetimeIndex,
11+
# in particular never be monotonic when we have NaT
12+
dti = date_range("2016-01-01", periods=3)
13+
pi = dti.to_period("D")
14+
tdi = Index(dti.view("timedelta64[ns]"))
15+
16+
for obj in [pi, pi._engine, dti, dti._engine, tdi, tdi._engine]:
17+
if isinstance(obj, Index):
18+
# i.e. not Engines
19+
assert obj.is_monotonic
20+
assert obj.is_monotonic_increasing
21+
assert not obj.is_monotonic_decreasing
22+
assert obj.is_unique
23+
24+
dti1 = dti.insert(0, NaT)
25+
pi1 = dti1.to_period("D")
26+
tdi1 = Index(dti1.view("timedelta64[ns]"))
27+
28+
for obj in [pi1, pi1._engine, dti1, dti1._engine, tdi1, tdi1._engine]:
29+
if isinstance(obj, Index):
30+
# i.e. not Engines
31+
assert not obj.is_monotonic
32+
assert not obj.is_monotonic_increasing
33+
assert not obj.is_monotonic_decreasing
34+
assert obj.is_unique
35+
36+
dti2 = dti.insert(3, NaT)
37+
pi2 = dti2.to_period("H")
38+
tdi2 = Index(dti2.view("timedelta64[ns]"))
39+
40+
for obj in [pi2, pi2._engine, dti2, dti2._engine, tdi2, tdi2._engine]:
41+
if isinstance(obj, Index):
42+
# i.e. not Engines
43+
assert not obj.is_monotonic
44+
assert not obj.is_monotonic_increasing
45+
assert not obj.is_monotonic_decreasing
46+
assert obj.is_unique
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from pandas import (
2+
DataFrame,
3+
DatetimeIndex,
4+
)
5+
import pandas._testing as tm
6+
7+
8+
def test_isocalendar_returns_correct_values_close_to_new_year_with_tz():
9+
# GH#6538: Check that DatetimeIndex and its TimeStamp elements
10+
# return the same weekofyear accessor close to new year w/ tz
11+
dates = ["2013/12/29", "2013/12/30", "2013/12/31"]
12+
dates = DatetimeIndex(dates, tz="Europe/Brussels")
13+
result = dates.isocalendar()
14+
expected_data_frame = DataFrame(
15+
[[2013, 52, 7], [2014, 1, 1], [2014, 1, 2]],
16+
columns=["year", "week", "day"],
17+
index=dates,
18+
dtype="UInt32",
19+
)
20+
tm.assert_frame_equal(result, expected_data_frame)

pandas/tests/indexes/datetimes/test_asof.py

+17
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1+
from datetime import timedelta
2+
13
from pandas import (
24
Index,
35
Timestamp,
46
date_range,
7+
isna,
58
)
9+
import pandas._testing as tm
610

711

812
class TestAsOf:
@@ -12,3 +16,16 @@ def test_asof_partial(self):
1216
result = index.asof("2010-02")
1317
assert result == expected
1418
assert not isinstance(result, Index)
19+
20+
def test_asof(self):
21+
index = tm.makeDateIndex(100)
22+
23+
dt = index[0]
24+
assert index.asof(dt) == dt
25+
assert isna(index.asof(dt - timedelta(1)))
26+
27+
dt = index[-1]
28+
assert index.asof(dt + timedelta(1)) == dt
29+
30+
dt = index[0].to_pydatetime()
31+
assert isinstance(index.asof(dt), Timestamp)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import pytest
2+
3+
from pandas import (
4+
DatetimeIndex,
5+
date_range,
6+
)
7+
8+
from pandas.tseries.offsets import (
9+
BDay,
10+
DateOffset,
11+
Day,
12+
Hour,
13+
)
14+
15+
16+
class TestFreq:
17+
def test_freq_setter_errors(self):
18+
# GH#20678
19+
idx = DatetimeIndex(["20180101", "20180103", "20180105"])
20+
21+
# setting with an incompatible freq
22+
msg = (
23+
"Inferred frequency 2D from passed values does not conform to "
24+
"passed frequency 5D"
25+
)
26+
with pytest.raises(ValueError, match=msg):
27+
idx._data.freq = "5D"
28+
29+
# setting with non-freq string
30+
with pytest.raises(ValueError, match="Invalid frequency"):
31+
idx._data.freq = "foo"
32+
33+
@pytest.mark.parametrize("values", [["20180101", "20180103", "20180105"], []])
34+
@pytest.mark.parametrize("freq", ["2D", Day(2), "2B", BDay(2), "48H", Hour(48)])
35+
@pytest.mark.parametrize("tz", [None, "US/Eastern"])
36+
def test_freq_setter(self, values, freq, tz):
37+
# GH#20678
38+
idx = DatetimeIndex(values, tz=tz)
39+
40+
# can set to an offset, converting from string if necessary
41+
idx._data.freq = freq
42+
assert idx.freq == freq
43+
assert isinstance(idx.freq, DateOffset)
44+
45+
# can reset to None
46+
idx._data.freq = None
47+
assert idx.freq is None
48+
49+
def test_freq_view_safe(self):
50+
# Setting the freq for one DatetimeIndex shouldn't alter the freq
51+
# for another that views the same data
52+
53+
dti = date_range("2016-01-01", periods=5)
54+
dta = dti._data
55+
56+
dti2 = DatetimeIndex(dta)._with_freq(None)
57+
assert dti2.freq is None
58+
59+
# Original was not altered
60+
assert dti.freq == "D"
61+
assert dta.freq == "D"

pandas/tests/indexes/datetimes/test_misc.py

-15
Original file line numberDiff line numberDiff line change
@@ -297,21 +297,6 @@ def test_week_and_weekofyear_are_deprecated():
297297
idx.weekofyear
298298

299299

300-
def test_isocalendar_returns_correct_values_close_to_new_year_with_tz():
301-
# GH 6538: Check that DatetimeIndex and its TimeStamp elements
302-
# return the same weekofyear accessor close to new year w/ tz
303-
dates = ["2013/12/29", "2013/12/30", "2013/12/31"]
304-
dates = DatetimeIndex(dates, tz="Europe/Brussels")
305-
result = dates.isocalendar()
306-
expected_data_frame = pd.DataFrame(
307-
[[2013, 52, 7], [2014, 1, 1], [2014, 1, 2]],
308-
columns=["year", "week", "day"],
309-
index=dates,
310-
dtype="UInt32",
311-
)
312-
tm.assert_frame_equal(result, expected_data_frame)
313-
314-
315300
def test_add_timedelta_preserves_freq():
316301
# GH#37295 should hold for any DTI with freq=None or Tick freq
317302
tz = "Canada/Eastern"

pandas/tests/indexes/datetimes/test_ops.py

+13-100
Original file line numberDiff line numberDiff line change
@@ -6,43 +6,17 @@
66
from pandas.compat import IS64
77

88
from pandas import (
9-
DateOffset,
109
DatetimeIndex,
1110
Index,
12-
Series,
1311
bdate_range,
1412
date_range,
1513
)
1614
import pandas._testing as tm
1715

18-
from pandas.tseries.offsets import (
19-
BDay,
20-
Day,
21-
Hour,
22-
)
23-
2416
START, END = datetime(2009, 1, 1), datetime(2010, 1, 1)
2517

2618

2719
class TestDatetimeIndexOps:
28-
def test_ops_properties_basic(self, datetime_series):
29-
30-
# sanity check that the behavior didn't change
31-
# GH#7206
32-
for op in ["year", "day", "second", "weekday"]:
33-
msg = f"'Series' object has no attribute '{op}'"
34-
with pytest.raises(AttributeError, match=msg):
35-
getattr(datetime_series, op)
36-
37-
# attribute access should still work!
38-
s = Series({"year": 2000, "month": 1, "day": 10})
39-
assert s.year == 2000
40-
assert s.month == 1
41-
assert s.day == 10
42-
msg = "'Series' object has no attribute 'weekday'"
43-
with pytest.raises(AttributeError, match=msg):
44-
s.weekday
45-
4620
@pytest.mark.parametrize(
4721
"freq,expected",
4822
[
@@ -74,72 +48,28 @@ def test_infer_freq(self, freq_sample):
7448
tm.assert_index_equal(idx, result)
7549
assert result.freq == freq_sample
7650

77-
@pytest.mark.parametrize("values", [["20180101", "20180103", "20180105"], []])
78-
@pytest.mark.parametrize("freq", ["2D", Day(2), "2B", BDay(2), "48H", Hour(48)])
79-
@pytest.mark.parametrize("tz", [None, "US/Eastern"])
80-
def test_freq_setter(self, values, freq, tz):
81-
# GH 20678
82-
idx = DatetimeIndex(values, tz=tz)
83-
84-
# can set to an offset, converting from string if necessary
85-
idx._data.freq = freq
86-
assert idx.freq == freq
87-
assert isinstance(idx.freq, DateOffset)
88-
89-
# can reset to None
90-
idx._data.freq = None
91-
assert idx.freq is None
92-
93-
def test_freq_setter_errors(self):
94-
# GH 20678
95-
idx = DatetimeIndex(["20180101", "20180103", "20180105"])
96-
97-
# setting with an incompatible freq
98-
msg = (
99-
"Inferred frequency 2D from passed values does not conform to "
100-
"passed frequency 5D"
101-
)
102-
with pytest.raises(ValueError, match=msg):
103-
idx._data.freq = "5D"
104-
105-
# setting with non-freq string
106-
with pytest.raises(ValueError, match="Invalid frequency"):
107-
idx._data.freq = "foo"
108-
109-
def test_freq_view_safe(self):
110-
# Setting the freq for one DatetimeIndex shouldn't alter the freq
111-
# for another that views the same data
112-
113-
dti = date_range("2016-01-01", periods=5)
114-
dta = dti._data
115-
116-
dti2 = DatetimeIndex(dta)._with_freq(None)
117-
assert dti2.freq is None
118-
119-
# Original was not altered
120-
assert dti.freq == "D"
121-
assert dta.freq == "D"
122-
12351

52+
@pytest.mark.parametrize("freq", ["B", "C"])
12453
class TestBusinessDatetimeIndex:
125-
def setup_method(self, method):
126-
self.rng = bdate_range(START, END)
54+
@pytest.fixture
55+
def rng(self, freq):
56+
return bdate_range(START, END, freq=freq)
12757

128-
def test_comparison(self):
129-
d = self.rng[10]
58+
def test_comparison(self, rng):
59+
d = rng[10]
13060

131-
comp = self.rng > d
61+
comp = rng > d
13262
assert comp[11]
13363
assert not comp[9]
13464

135-
def test_copy(self):
136-
cp = self.rng.copy()
65+
def test_copy(self, rng):
66+
cp = rng.copy()
13767
repr(cp)
138-
tm.assert_index_equal(cp, self.rng)
68+
tm.assert_index_equal(cp, rng)
13969

140-
def test_identical(self):
141-
t1 = self.rng.copy()
142-
t2 = self.rng.copy()
70+
def test_identical(self, rng):
71+
t1 = rng.copy()
72+
t2 = rng.copy()
14373
assert t1.identical(t2)
14474

14575
# name
@@ -153,20 +83,3 @@ def test_identical(self):
15383
t2v = Index(t2.values)
15484
assert t1.equals(t2v)
15585
assert not t1.identical(t2v)
156-
157-
158-
class TestCustomDatetimeIndex:
159-
def setup_method(self, method):
160-
self.rng = bdate_range(START, END, freq="C")
161-
162-
def test_comparison(self):
163-
d = self.rng[10]
164-
165-
comp = self.rng > d
166-
assert comp[11]
167-
assert not comp[9]
168-
169-
def test_copy(self):
170-
cp = self.rng.copy()
171-
repr(cp)
172-
tm.assert_index_equal(cp, self.rng)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import pytest
2+
3+
from pandas import (
4+
offsets,
5+
period_range,
6+
)
7+
import pandas._testing as tm
8+
9+
10+
class TestFreq:
11+
def test_freq_setter_deprecated(self):
12+
# GH#20678
13+
idx = period_range("2018Q1", periods=4, freq="Q")
14+
15+
# no warning for getter
16+
with tm.assert_produces_warning(None):
17+
idx.freq
18+
19+
# warning for setter
20+
with pytest.raises(AttributeError, match="can't set attribute"):
21+
idx.freq = offsets.Day()

0 commit comments

Comments
 (0)