Interval#
- class Interval#
Immutable object implementing an Interval.
Example#
import pandasCore as pd
# Create Interval
obj = pd.Interval(...)
Attributes#
Attribute |
Description |
Example |
|---|---|---|
|
String describing the inclusive side of the interval. |
|
|
Check if the interval is closed on the left side. |
|
|
Check if the interval is closed on the right side. |
|
|
Check if the interval is empty. |
|
|
Left bound for the interval. |
|
|
Return the length of the interval. |
|
|
Return the midpoint of the interval. |
|
|
Right bound for the interval. |
Construction#
Method |
Description |
Example |
|---|---|---|
|
Create a default interval (0, 0] |
Comparison#
Method |
Description |
Example |
|---|---|---|
|
Check equality with another Interval. |
|
|
Greater than or equal comparison |
|
|
Greater than comparison |
|
|
Less than or equal comparison |
|
|
Less than comparison (lexicographic by left, then right) |
|
|
Check inequality with another Interval |
Iteration#
Method |
Description |
Example |
|---|---|---|
|
Check if a value is contained in the interval. |
Other Methods#
Method |
Description |
Example |
|---|---|---|
|
Return a hash value for the Interval. |
|
|
Return a string representation of the Interval. |
|
|
Return a string representation of the interval bounds. |
|
|
Check whether two Interval objects overlap. |
Code Examples#
The following examples are extracted from the test suite.
closed (test_interval.py:37)
27def test_interval_constructor():
28 """Test Interval construction"""
29 f_print_header("Test: Interval Constructor")
30
31 try:
32 # Default constructor
33 iv = pandasCore.Interval(0, 5)
34 f_print_success("Created Interval(0, 5)")
35
36 # Check default closed is 'right'
37 if iv.closed == 'right':
38 f_print_success(f"Default closed is 'right': {iv.closed}")
39 else:
40 f_print_error(f"Expected closed='right', got '{iv.closed}'")
41 return False
42
43 # With explicit closed
44 iv_left = pandasCore.Interval(0, 5, closed='left')
45 if iv_left.closed == 'left':
46 f_print_success(f"Created Interval with closed='left'")
47 else:
closed_left (test_interval.py:108)
98 return False
99
100 # Test length
101 if iv.length == 6.0:
102 f_print_success(f"length property correct: {iv.length}")
103 else:
104 f_print_error(f"Expected length=6.0, got {iv.length}")
105 return False
106
107 # Test closed_left/closed_right
108 if not iv.closed_left:
109 f_print_success(f"closed_left is False for closed='right'")
110 else:
111 f_print_error(f"Expected closed_left=False for closed='right'")
112 return False
113
114 if iv.closed_right:
115 f_print_success(f"closed_right is True for closed='right'")
116 else:
117 f_print_error(f"Expected closed_right=True for closed='right'")
118 return False
closed_right (test_interval.py:114)
104 f_print_error(f"Expected length=6.0, got {iv.length}")
105 return False
106
107 # Test closed_left/closed_right
108 if not iv.closed_left:
109 f_print_success(f"closed_left is False for closed='right'")
110 else:
111 f_print_error(f"Expected closed_left=False for closed='right'")
112 return False
113
114 if iv.closed_right:
115 f_print_success(f"closed_right is True for closed='right'")
116 else:
117 f_print_error(f"Expected closed_right=True for closed='right'")
118 return False
119
120 return True
121
122 except Exception as e:
123 f_print_error(f"Exception: {e}")
124 return False
left (test_interval.py:80)
70def test_interval_properties():
71 """Test Interval properties"""
72 f_print_header("Test: Interval Properties")
73
74 try:
75 iv = pandasCore.Interval(2, 8, closed='right')
76
77 # Test left
78 if iv.left == 2.0:
79 f_print_success(f"left property correct: {iv.left}")
80 else:
81 f_print_error(f"Expected left=2.0, got {iv.left}")
82 return False
83
84 # Test right
85 if iv.right == 8.0:
86 f_print_success(f"right property correct: {iv.right}")
87 else:
88 f_print_error(f"Expected right=8.0, got {iv.right}")
length (test_interval.py:101)
91 return False
92
93 # Test mid
94 if iv.mid == 5.0:
95 f_print_success(f"mid property correct: {iv.mid}")
96 else:
97 f_print_error(f"Expected mid=5.0, got {iv.mid}")
98 return False
99
100 # Test length
101 if iv.length == 6.0:
102 f_print_success(f"length property correct: {iv.length}")
103 else:
104 f_print_error(f"Expected length=6.0, got {iv.length}")
105 return False
106
107 # Test closed_left/closed_right
108 if not iv.closed_left:
109 f_print_success(f"closed_left is False for closed='right'")
110 else:
111 f_print_error(f"Expected closed_left=False for closed='right'")
mid (test_interval.py:94)
84 return False
85
86 # Test right
87 if iv.right == 8.0:
88 f_print_success(f"right property correct: {iv.right}")
89 else:
90 f_print_error(f"Expected right=8.0, got {iv.right}")
91 return False
92
93 # Test mid
94 if iv.mid == 5.0:
95 f_print_success(f"mid property correct: {iv.mid}")
96 else:
97 f_print_error(f"Expected mid=5.0, got {iv.mid}")
98 return False
99
100 # Test length
101 if iv.length == 6.0:
102 f_print_success(f"length property correct: {iv.length}")
103 else:
104 f_print_error(f"Expected length=6.0, got {iv.length}")
right (test_interval.py:87)
77 iv = pandasCore.Interval(2, 8, closed='right')
78
79 # Test left
80 if iv.left == 2.0:
81 f_print_success(f"left property correct: {iv.left}")
82 else:
83 f_print_error(f"Expected left=2.0, got {iv.left}")
84 return False
85
86 # Test right
87 if iv.right == 8.0:
88 f_print_success(f"right property correct: {iv.right}")
89 else:
90 f_print_error(f"Expected right=8.0, got {iv.right}")
91 return False
92
93 # Test mid
94 if iv.mid == 5.0:
95 f_print_success(f"mid property correct: {iv.mid}")
96 else:
97 f_print_error(f"Expected mid=5.0, got {iv.mid}")
__init__ (test_interval.py:33)
23import pandasCore
24import pandas as pd
25
26
27def test_interval_constructor():
28 """Test Interval construction"""
29 f_print_header("Test: Interval Constructor")
30
31 try:
32 # Default constructor
33 iv = pandasCore.Interval(0, 5)
34 f_print_success("Created Interval(0, 5)")
35
36 # Check default closed is 'right'
37 if iv.closed == 'right':
38 f_print_success(f"Default closed is 'right': {iv.closed}")
39 else:
40 f_print_error(f"Expected closed='right', got '{iv.closed}'")
41 return False
42
43 # With explicit closed