33
44import pandas as pd
55import numpy as np
6+ import pytest
67from vtools .data .timeseries import is_regular
78
8- # Irregular DatetimeIndex Series
9- irreg_datetime_series = pd .Series (
10- data = [1 , 2 , 3 , 4 ],
11- index = pd .DatetimeIndex (
12- [
13- pd .Timestamp ("2025-01-01 00:00" ),
14- pd .Timestamp ("2025-01-01 01:00" ),
15- pd .Timestamp ("2025-01-01 03:00" ), # Gap: should be 02:00
16- pd .Timestamp ("2025-01-01 04:00" ),
17- ]
18- ),
19- )
20-
21-
22- # Regular DatetimeIndex Series
23- reg_datetime_series = pd .Series (
24- data = [1 , 2 , 3 , 4 ],
25- index = pd .DatetimeIndex (
26- [
27- pd .Timestamp ("2025-01-01 00:00" ),
28- pd .Timestamp ("2025-01-01 01:00" ),
29- pd .Timestamp ("2025-01-01 02:00" ),
30- pd .Timestamp ("2025-01-01 03:00" ),
31- ]
32- ),
33- )
34-
35- # Irregular Float Index Series
36- irreg_float_series = pd .Series (
37- data = [1 , 2 , 3 , 4 ],
38- index = pd .Index (
39- [0.0 , 90.0 , 180.0 , 300.0 ], dtype = "float64"
40- ), # Last diff is 120, not 90
41- )
42-
43- # Regular Float Index Series
44- reg_float_series = pd .Series (
45- data = [1 , 2 , 3 , 4 ],
46- index = pd .Index ([0.0 , 90.0 , 180.0 , 270.0 ], dtype = "float64" ), # All diffs are 90.0
47- )
48-
49-
50- # Irregular Int Index Series
51- irreg_int_series = pd .Series (
52- data = [1 , 2 , 3 , 4 ],
53- index = pd .Index ([0 , 100 , 200 , 350 ], dtype = "int64" ), # Last diff is 150, not 100
54- )
55-
56- # Regular Int Index Series
57- reg_int_series = pd .Series (
58- data = [1 , 2 , 3 , 4 ],
59- index = pd .Index ([0 , 100 , 200 , 300 ], dtype = "int64" ), # All diffs are 100
60- )
9+
10+ @pytest .fixture
11+ def irreg_datetime_series ():
12+ return pd .Series (
13+ data = [1 , 2 , 3 , 4 ],
14+ index = pd .DatetimeIndex (
15+ [
16+ pd .Timestamp ("2025-01-01 00:00" ),
17+ pd .Timestamp ("2025-01-01 01:00" ),
18+ pd .Timestamp ("2025-01-01 03:00" ), # Gap: should be 02:00
19+ pd .Timestamp ("2025-01-01 04:00" ),
20+ ]
21+ ),
22+ )
23+
24+
25+ @pytest .fixture
26+ def reg_datetime_series ():
27+ return pd .Series (
28+ data = [1 , 2 , 3 , 4 ],
29+ index = pd .DatetimeIndex (
30+ [
31+ pd .Timestamp ("2025-01-01 00:00" ),
32+ pd .Timestamp ("2025-01-01 01:00" ),
33+ pd .Timestamp ("2025-01-01 02:00" ),
34+ pd .Timestamp ("2025-01-01 03:00" ),
35+ ]
36+ ),
37+ )
38+
39+
40+ @pytest .fixture
41+ def irreg_float_series ():
42+ return pd .Series (
43+ data = [1 , 2 , 3 , 4 ],
44+ index = pd .Index (
45+ [0.0 , 90.0 , 180.0 , 300.0 ], dtype = "float64"
46+ ), # Last diff is 120, not 90
47+ )
48+
49+
50+ @pytest .fixture
51+ def reg_float_series ():
52+ return pd .Series (
53+ data = [1 , 2 , 3 , 4 ],
54+ index = pd .Index ([0.0 , 90.0 , 180.0 , 270.0 ], dtype = "float64" ), # All diffs are 90.0
55+ )
56+
57+
58+ @pytest .fixture
59+ def irreg_int_series ():
60+ return pd .Series (
61+ data = [1 , 2 , 3 , 4 ],
62+ index = pd .Index ([0 , 100 , 200 , 350 ], dtype = "int64" ), # Last diff is 150, not 100
63+ )
64+
65+
66+ @pytest .fixture
67+ def reg_int_series ():
68+ return pd .Series (
69+ data = [1 , 2 , 3 , 4 ],
70+ index = pd .Index ([0 , 100 , 200 , 300 ], dtype = "int64" ), # All diffs are 100
71+ )
6172
6273
6374# ============================================================================
6475# Tests
6576# ============================================================================
66- def test_irregular_datetime_series ():
77+ def test_irregular_datetime_series (irreg_datetime_series ):
6778 """Test that irregular datetime series is identified as not regular"""
6879 assert is_regular (irreg_datetime_series ) is False
6980
7081
71- def test_regular_datetime_series ():
82+ def test_regular_datetime_series (reg_datetime_series ):
7283 """Test that regular datetime series is identified as regular"""
7384 assert is_regular (reg_datetime_series ) is True
7485
7586
76- def test_irregular_float_series ():
87+ def test_irregular_float_series (irreg_float_series ):
7788 """Test that irregular float index series is identified as not regular"""
7889 assert is_regular (irreg_float_series ) is False
7990
8091
81- def test_regular_float_series ():
92+ def test_regular_float_series (reg_float_series ):
8293 """Test that regular float index series is identified as regular"""
8394 assert is_regular (reg_float_series ) is True
8495
8596
86- def test_irregular_int_series ():
97+ def test_irregular_int_series (irreg_int_series ):
8798 """Test that irregular int index series is identified as not regular"""
8899 assert is_regular (irreg_int_series ) is False
89100
90101
91- def test_regular_int_series ():
102+ def test_regular_int_series (reg_int_series ):
92103 """Test that regular int index series is identified as regular"""
93104 assert is_regular (reg_int_series ) is True
94105
95106
96- def test_irregular_datetime_raises ():
107+ def test_irregular_datetime_raises (irreg_datetime_series ):
97108 """Test that raise_exception=True raises for irregular datetime"""
98109 try :
99110 is_regular (irreg_datetime_series , raise_exception = True )
@@ -102,13 +113,13 @@ def test_irregular_datetime_raises():
102113 pass
103114
104115
105- def test_regular_datetime_raises ():
116+ def test_regular_datetime_raises (reg_datetime_series ):
106117 """Test that raise_exception=True does not raise for regular datetime"""
107118 result = is_regular (reg_datetime_series , raise_exception = True )
108119 assert result is True
109120
110121
111- def test_irregular_float_raises ():
122+ def test_irregular_float_raises (irreg_float_series ):
112123 """Test that raise_exception=True raises for irregular float"""
113124 try :
114125 is_regular (irreg_float_series , raise_exception = True )
@@ -117,13 +128,13 @@ def test_irregular_float_raises():
117128 pass
118129
119130
120- def test_regular_float_raises ():
131+ def test_regular_float_raises (reg_float_series ):
121132 """Test that raise_exception=True does not raise for regular float"""
122133 result = is_regular (reg_float_series , raise_exception = True )
123134 assert result is True
124135
125136
126- def test_irregular_int_raises ():
137+ def test_irregular_int_raises (irreg_int_series ):
127138 """Test that raise_exception=True raises for irregular int"""
128139 try :
129140 is_regular (irreg_int_series , raise_exception = True )
@@ -132,7 +143,7 @@ def test_irregular_int_raises():
132143 pass
133144
134145
135- def test_regular_int_raises ():
146+ def test_regular_int_raises (reg_int_series ):
136147 """Test that raise_exception=True does not raise for regular int"""
137148 result = is_regular (reg_int_series , raise_exception = True )
138149 assert result is True
0 commit comments