Index#
- class Index#
Immutable sequence used for indexing and alignment.
Example#
import pandasCore as pd
# Create Index
idx = pd.Index([1, 2, 3], name='my_index')
print(len(idx)) # 3
Attributes#
Attribute |
Description |
Example |
|---|---|---|
|
Return the transpose (returns copy for 1D Index) |
|
|
Return the dtype object of the underlying data |
|
|
Indicator whether Index is empty |
|
|
Return True if Index has duplicate values |
|
|
Return True if Index has NaN values |
|
|
Return string describing the type of the Index |
|
|
Return if the index is monotonic decreasing |
|
|
Return if the index is monotonic increasing |
|
|
Return if the index has unique values |
|
|
Return the name of the Index |
|
|
Return the number of bytes in the underlying data |
|
|
Number of dimensions (always 1 for Index) |
|
|
Number of levels (always 1 for non-MultiIndex) |
|
|
Return a tuple of the shape of the underlying data |
|
|
Return the number of elements in the Index |
|
|
Vectorized string functions for Index. |
|
|
Return an array representing the data in the Index |
Construction#
Method |
Description |
Example |
|---|---|---|
|
Immutable sequence used for indexing and alignment. |
Indexing / Selection#
Method |
Description |
Example |
|---|---|---|
|
Get element by position |
|
|
Return a new Index of the values selected by the indices. |
|
|
Replace values where the condition is False. |
Data Manipulation#
Method |
Description |
Example |
|---|---|---|
|
Make new Index with passed list of labels deleted. |
|
|
Return Index with requested level removed. |
|
|
Make new Index inserting new item at location. |
|
|
Create index with target’s values. |
|
|
Alter Index name. |
|
|
Set Index name. |
Missing Data#
Method |
Description |
Example |
|---|---|---|
|
Return Index without NA/NaN values. |
|
|
Fill NA/NaN values with the specified value. |
|
|
Detect missing values. |
|
|
Detect missing values. Alias for isna. |
|
|
Detect existing (non-missing) values. |
|
|
Detect existing (non-missing) values. Alias for notna. |
Statistics#
Method |
Description |
Example |
|---|---|---|
|
Return the maximum value of the Index. |
|
|
Return the minimum value of the Index. |
|
|
Return number of unique elements in the Index. |
|
|
Return a dict containing counts of unique values. |
Aggregation#
Comparison#
Method |
Description |
Example |
|---|---|---|
|
||
|
||
|
||
|
||
|
||
|
||
|
Determine if two Index objects are equal. |
Sorting#
Method |
Description |
Example |
|---|---|---|
|
Return the integer indices that would sort the Index. |
|
|
Find indices where elements should be inserted to maintain order. |
|
|
Return a sorted copy of the Index. |
Reshaping#
Combining#
Method |
Description |
Example |
|---|---|---|
|
Append a collection of Index objects together. |
|
|
Compute join between Index and other Index. |
Time Series#
Method |
Description |
Example |
|---|---|---|
|
Return the label from the index, or, if not present, the previous one. |
|
|
Computes the difference between consecutive values. |
|
|
Shift index by desired number of periods. |
I/O#
Method |
Description |
Example |
|---|---|---|
|
Identity method. |
|
|
Return a list of the Index values |
|
|
Return a NumPy ndarray representing the values in this Index |
|
|
Create a Series with both index and values equal to the index keys. |
|
|
Return a list of the Index values |
Conversion#
Method |
Description |
Example |
|---|---|---|
|
Create an Index with values cast to specified dtype. |
|
|
Make a copy of this object. |
|
|
Attempt to infer better dtypes for object columns. |
|
|
Return a view of the Index data. |
Iteration#
Method |
Description |
Example |
|---|---|---|
|
Return True if key is in the Index |
|
|
Iterate over the Index values |
|
|
Return the number of elements |
Set Operations#
Method |
Description |
Example |
|---|---|---|
|
Return a new Index with elements of index not in other. |
|
|
Return Index with duplicate values removed. |
|
|
Indicate duplicate index values. |
|
|
Form the intersection of two Index objects. |
|
|
Return a boolean array indicating whether each element is in values. |
|
|
Compute the symmetric difference of two Index objects. |
|
|
Form the union of two Index objects. |
|
|
Return unique values in the Index. |
Datetime Methods#
Method |
Description |
Example |
|---|---|---|
|
Round each value in the Index to the given number of decimals. |
Other Methods#
Method |
Description |
Example |
|---|---|---|
|
||
|
||
|
Return str(self). |
|
|
Return whether all elements are Truthy. |
|
|
Return whether any element is Truthy. |
|
|
Return int position of the largest value in the Index. |
|
|
Return int position of the smallest value in the Index. |
|
|
Return the locations for asof-lookup of labels. |
|
|
Make new Index with element(s) at position(s) removed. |
|
|
Encode the object as an enumerated type. |
|
|
Render a string representation of the Index. |
|
|
Compute indexer and mask for new index given the current index. |
|
|
Guaranteed to return an indexer even when target contains non-uniqu… |
|
|
Compute indexer and mask for new index given a non-unique current i… |
|
|
Return an Index of values for requested level. |
|
|
Get integer location for requested label. |
|
|
Calculate slice bound that corresponds to given label. |
|
|
Whether the type is an integer type. |
|
|
Check if two Index objects are identical (values and metadata). |
|
|
Return the first element of the underlying data as a Python scalar. |
|
|
Memory usage of the Index. |
|
|
Return a new Index with values put in locations specified by mask. |
|
|
Return a flattened numpy array representing the values. |
|
|
Repeat elements of an Index. |
|
|
Compute slice indexer for input labels and step. |
|
|
Compute slice locations for input labels. |
|
|
Return a sorted copy of the Index. |
|
|
Sort Index by the requested level. |
Code Examples#
The following examples are extracted from the test suite.
dtype (test_index.py:40)
30# ==================== Test Functions ====================
31
32def test_constructor_and_properties():
33 """Test Index constructor and basic properties"""
34 f_print_header("Test: Constructor and Properties")
35
36 # Basic construction
37 idx = pandasCore.Index([1, 2, 3, 4, 5], name="test_idx")
38 ctx.assert_equal(5, idx.size, "size")
39 ctx.assert_equal("test_idx", idx.name, "name")
40 ctx.assert_equal("int64", idx.dtype, "dtype")
41 ctx.assert_false(idx.empty, "empty")
42 ctx.assert_true(idx.is_unique, "is_unique")
43 ctx.assert_true(idx.is_monotonic_increasing, "is_monotonic_increasing")
44 ctx.assert_false(idx.is_monotonic_decreasing, "is_monotonic_decreasing")
45 ctx.assert_equal(1, idx.ndim, "ndim")
46 ctx.assert_equal((5,), idx.shape, "shape")
47
48 # Duplicate values
49 idx_dup = pandasCore.Index([1, 2, 2, 3, 3, 3])
50 ctx.assert_false(idx_dup.is_unique, "is_unique with dups")
empty (test_index.py:41)
31def test_constructor_and_properties():
32 """Test Index constructor and basic properties"""
33 f_print_header("Test: Constructor and Properties")
34
35 # Basic construction
36 idx = pandasCore.Index([1, 2, 3, 4, 5], name="test_idx")
37 ctx.assert_equal(5, idx.size, "size")
38 ctx.assert_equal("test_idx", idx.name, "name")
39 ctx.assert_equal("int64", idx.dtype, "dtype")
40 ctx.assert_false(idx.empty, "empty")
41 ctx.assert_true(idx.is_unique, "is_unique")
42 ctx.assert_true(idx.is_monotonic_increasing, "is_monotonic_increasing")
43 ctx.assert_false(idx.is_monotonic_decreasing, "is_monotonic_decreasing")
44 ctx.assert_equal(1, idx.ndim, "ndim")
45 ctx.assert_equal((5,), idx.shape, "shape")
46
47 # Duplicate values
48 idx_dup = pandasCore.Index([1, 2, 2, 3, 3, 3])
49 ctx.assert_false(idx_dup.is_unique, "is_unique with dups")
50 ctx.assert_true(idx_dup.has_duplicates, "has_duplicates")
is_monotonic_decreasing (test_index.py:44)
34 f_print_header("Test: Constructor and Properties")
35
36 # Basic construction
37 idx = pandasCore.Index([1, 2, 3, 4, 5], name="test_idx")
38 ctx.assert_equal(5, idx.size, "size")
39 ctx.assert_equal("test_idx", idx.name, "name")
40 ctx.assert_equal("int64", idx.dtype, "dtype")
41 ctx.assert_false(idx.empty, "empty")
42 ctx.assert_true(idx.is_unique, "is_unique")
43 ctx.assert_true(idx.is_monotonic_increasing, "is_monotonic_increasing")
44 ctx.assert_false(idx.is_monotonic_decreasing, "is_monotonic_decreasing")
45 ctx.assert_equal(1, idx.ndim, "ndim")
46 ctx.assert_equal((5,), idx.shape, "shape")
47
48 # Duplicate values
49 idx_dup = pandasCore.Index([1, 2, 2, 3, 3, 3])
50 ctx.assert_false(idx_dup.is_unique, "is_unique with dups")
51 ctx.assert_true(idx_dup.has_duplicates, "has_duplicates")
52
53
54def test_copy():
is_monotonic_increasing (test_example.py:185)
175 return False
176
177 # Check is_unique
178 if idx.is_unique:
179 f_print_success("is_unique is True")
180 else:
181 f_print_error("is_unique should be True")
182 return False
183
184 # Check is_monotonic_increasing
185 if idx.is_monotonic_increasing:
186 f_print_success("is_monotonic_increasing is True")
187 else:
188 f_print_error("is_monotonic_increasing should be True")
189 return False
190
191 # Check len
192 if len(idx) == 5:
193 f_print_success("len(idx) is 5")
194 else:
195 f_print_error(f"len(idx) is {len(idx)}, expected 5")
is_unique (test_example.py:178)
168 return False
169
170 # Check name
171 if idx.name == "test_index":
172 f_print_success(f"Name is correct: {idx.name}")
173 else:
174 f_print_error(f"Name is wrong: {idx.name}, expected 'test_index'")
175 return False
176
177 # Check is_unique
178 if idx.is_unique:
179 f_print_success("is_unique is True")
180 else:
181 f_print_error("is_unique should be True")
182 return False
183
184 # Check is_monotonic_increasing
185 if idx.is_monotonic_increasing:
186 f_print_success("is_monotonic_increasing is True")
187 else:
188 f_print_error("is_monotonic_increasing should be True")
name (test_example.py:171)
161 f_print_success("Created Index from list")
162
163 # Check size
164 if idx.size == 5:
165 f_print_success(f"Size is correct: {idx.size}")
166 else:
167 f_print_error(f"Size is wrong: {idx.size}, expected 5")
168 return False
169
170 # Check name
171 if idx.name == "test_index":
172 f_print_success(f"Name is correct: {idx.name}")
173 else:
174 f_print_error(f"Name is wrong: {idx.name}, expected 'test_index'")
175 return False
176
177 # Check is_unique
178 if idx.is_unique:
179 f_print_success("is_unique is True")
180 else:
181 f_print_error("is_unique should be True")
ndim (test_index.py:45)
35 # Basic construction
36 idx = pandasCore.Index([1, 2, 3, 4, 5], name="test_idx")
37 ctx.assert_equal(5, idx.size, "size")
38 ctx.assert_equal("test_idx", idx.name, "name")
39 ctx.assert_equal("int64", idx.dtype, "dtype")
40 ctx.assert_false(idx.empty, "empty")
41 ctx.assert_true(idx.is_unique, "is_unique")
42 ctx.assert_true(idx.is_monotonic_increasing, "is_monotonic_increasing")
43 ctx.assert_false(idx.is_monotonic_decreasing, "is_monotonic_decreasing")
44 ctx.assert_equal(1, idx.ndim, "ndim")
45 ctx.assert_equal((5,), idx.shape, "shape")
46
47 # Duplicate values
48 idx_dup = pandasCore.Index([1, 2, 2, 3, 3, 3])
49 ctx.assert_false(idx_dup.is_unique, "is_unique with dups")
50 ctx.assert_true(idx_dup.has_duplicates, "has_duplicates")
51
52
53def test_copy():
54 """Test copy method"""
nlevels (test_index.py:773)
763 ctx.assert_list_equal([1, 2, 3], result.tolist(), "infer_objects")
764
765
766def test_nlevels():
767 """Test nlevels property"""
768 f_print_header("Test: nlevels")
769
770 idx = pandasCore.Index([1, 2, 3])
771
772 ctx.assert_equal(1, idx.nlevels, "nlevels")
773
774
775def f_main():
776 """Main test function"""
777 f_print_header("Index Phase 1 & 2: Core, Set Operations, Reshaping, Conversion Tests")
778
779 # Run all tests
780 test_functions = [
781 # Phase 1: Core & Set Operations
782 test_constructor_and_properties,
shape (test_index.py:46)
36 # Basic construction
37 idx = pandasCore.Index([1, 2, 3, 4, 5], name="test_idx")
38 ctx.assert_equal(5, idx.size, "size")
39 ctx.assert_equal("test_idx", idx.name, "name")
40 ctx.assert_equal("int64", idx.dtype, "dtype")
41 ctx.assert_false(idx.empty, "empty")
42 ctx.assert_true(idx.is_unique, "is_unique")
43 ctx.assert_true(idx.is_monotonic_increasing, "is_monotonic_increasing")
44 ctx.assert_false(idx.is_monotonic_decreasing, "is_monotonic_decreasing")
45 ctx.assert_equal(1, idx.ndim, "ndim")
46 ctx.assert_equal((5,), idx.shape, "shape")
47
48 # Duplicate values
49 idx_dup = pandasCore.Index([1, 2, 2, 3, 3, 3])
50 ctx.assert_false(idx_dup.is_unique, "is_unique with dups")
51 ctx.assert_true(idx_dup.has_duplicates, "has_duplicates")
52
53
54def test_copy():
55 """Test copy method"""
56 f_print_header("Test: copy")
size (test_example.py:164)
154 """Test Index creation from list"""
155 f_print_header("Test: Index Creation")
156
157 try:
158 # Create Index from list
159 data = [10, 20, 30, 40, 50]
160 idx = pandasCore.Index(data, name="test_index")
161 f_print_success("Created Index from list")
162
163 # Check size
164 if idx.size == 5:
165 f_print_success(f"Size is correct: {idx.size}")
166 else:
167 f_print_error(f"Size is wrong: {idx.size}, expected 5")
168 return False
169
170 # Check name
171 if idx.name == "test_index":
172 f_print_success(f"Name is correct: {idx.name}")
173 else:
174 f_print_error(f"Name is wrong: {idx.name}, expected 'test_index'")
str (test_index_str_accessor.py:52)
42def test_string_index_str_accessor():
43 """Test StringIndex.str accessor works."""
44 print("Testing StringIndex.str accessor...")
45
46 # Create a StringIndex
47 idx = pandasCore.StringIndex(['hello', 'world', 'test'])
48 print(f" Created StringIndex: {idx}")
49
50 # Access .str
51 str_methods = idx.str
52 print(f" idx.str type: {type(str_methods)}")
53
54 # Test upper()
55 result = str_methods.upper()
56 print(f" idx.str.upper() result: {result}")
57
58 # Verify result
59 result_list = result.tolist()
60 expected = ['HELLO', 'WORLD', 'TEST']
61 assert result_list == expected, f"Expected {expected}, got {result_list}"
__init__ (test_example.py:160)
150 return False
151
152
153def test_index_creation():
154 """Test Index creation from list"""
155 f_print_header("Test: Index Creation")
156
157 try:
158 # Create Index from list
159 data = [10, 20, 30, 40, 50]
160 idx = pandasCore.Index(data, name="test_index")
161 f_print_success("Created Index from list")
162
163 # Check size
164 if idx.size == 5:
165 f_print_success(f"Size is correct: {idx.size}")
166 else:
167 f_print_error(f"Size is wrong: {idx.size}, expected 5")
168 return False
169
170 # Check name
take (test_index.py:405)
395 ctx.assert_equal(1, start, "slice_locs start")
396 ctx.assert_equal(4, stop, "slice_locs stop")
397
398
399def test_take():
400 """Test take method"""
401 f_print_header("Test: take")
402
403 idx = pandasCore.Index([10, 20, 30, 40])
404 result = idx.take([0, 2])
405
406 ctx.assert_list_equal([10, 30], result.tolist(), "take")
407
408
409def test_insert():
410 """Test insert method"""
411 f_print_header("Test: insert")
412
413 idx = pandasCore.Index([1, 2, 4])
414 result = idx.insert(2, 3)
drop (test_index.py:495)
485 ctx.assert_list_equal([1, 2, 3], result, "to_list")
486
487
488# ==================== Phase 2: Reshaping, Conversion & Testing ====================
489
490def test_drop():
491 """Test drop method"""
492 f_print_header("Test: drop")
493
494 idx = pandasCore.Index([1, 2, 3, 4, 5])
495 result = idx.drop([2, 4])
496
497 ctx.assert_list_equal([1, 3, 5], result.tolist(), "drop")
498
499
500def test_get_level_values():
501 """Test get_level_values method"""
502 f_print_header("Test: get_level_values")
503
504 idx = pandasCore.Index([1, 2, 3], name="level0")
505 result = idx.get_level_values(0)
insert (test_index.py:415)
405 result = idx.take([0, 2])
406
407 ctx.assert_list_equal([10, 30], result.tolist(), "take")
408
409
410def test_insert():
411 """Test insert method"""
412 f_print_header("Test: insert")
413
414 idx = pandasCore.Index([1, 2, 4])
415 result = idx.insert(2, 3)
416
417 ctx.assert_list_equal([1, 2, 3, 4], result.tolist(), "insert")
418
419
420def test_delete():
421 """Test delete method"""
422 f_print_header("Test: delete")
423
424 idx = pandasCore.Index([1, 2, 3, 4])
425 result = idx.delete(1)
reindex (test_index.py:516)
506 ctx.assert_list_equal([1, 2, 3], result.tolist(), "get_level_values")
507
508
509def test_reindex():
510 """Test reindex method"""
511 f_print_header("Test: reindex")
512
513 idx = pandasCore.Index([1, 2, 3])
514 target = pandasCore.Index([1, 3, 5])
515 new_idx, indexer = idx.reindex(target)
516
517 ctx.assert_list_equal([1, 3, 5], new_idx.tolist(), "reindex new_idx")
518 ctx.assert_list_equal([0, 2, -1], indexer, "reindex indexer")
519
520
521def test_sortlevel():
522 """Test sortlevel method"""
523 f_print_header("Test: sortlevel")
524
525 idx = pandasCore.Index([3, 1, 2])
rename (test_index.py:70)
60 ctx.assert_list_equal(idx.tolist(), idx_copy.tolist(), "copy values")
61 ctx.assert_equal(idx.name, idx_copy.name, "copy name")
62
63
64def test_rename():
65 """Test rename method"""
66 f_print_header("Test: rename")
67
68 idx = pandasCore.Index([1, 2, 3], name="old_name")
69 idx_new = idx.rename("new_name")
70
71 ctx.assert_equal("new_name", idx_new.name, "rename")
72 ctx.assert_equal("old_name", idx.name, "original unchanged")
73
74
75def test_set_names():
76 """Test set_names method"""
77 f_print_header("Test: set_names")
78
79 idx = pandasCore.Index([1, 2, 3], name="old")
set_names (test_index.py:81)
71 ctx.assert_equal("new_name", idx_new.name, "rename")
72 ctx.assert_equal("old_name", idx.name, "original unchanged")
73
74
75def test_set_names():
76 """Test set_names method"""
77 f_print_header("Test: set_names")
78
79 idx = pandasCore.Index([1, 2, 3], name="old")
80 idx_new = idx.set_names("new")
81
82 ctx.assert_equal("new", idx_new.name, "set_names")
83
84
85def test_map():
86 """Test map method"""
87 f_print_header("Test: map")
88
89 idx = pandasCore.Index([1, 2, 3])
90 idx_mapped = idx.map(lambda x: x * 2)
dropna (test_index.py:111)
101 idx_filled = idx.fillna(0)
102
103 ctx.assert_list_equal([1, 2, 3], idx_filled.tolist(), "fillna (int64)")
104
105
106def test_dropna():
107 """Test dropna method (for int64, essentially a no-op)"""
108 f_print_header("Test: dropna")
109
110 idx = pandasCore.Index([1, 2, 3])
111 idx_dropped = idx.dropna()
112
113 ctx.assert_list_equal([1, 2, 3], idx_dropped.tolist(), "dropna (int64)")
114
115
116def test_isna_notna():
117 """Test isna, notna, isnull, notnull methods"""
118 f_print_header("Test: isna/notna")
119
120 idx = pandasCore.Index([1, 2, 3])
fillna (test_index.py:101)
91 idx_mapped = idx.map(lambda x: x * 2)
92
93 ctx.assert_list_equal([2, 4, 6], idx_mapped.tolist(), "map")
94
95
96def test_fillna():
97 """Test fillna method (for int64, essentially a no-op)"""
98 f_print_header("Test: fillna")
99
100 idx = pandasCore.Index([1, 2, 3])
101 idx_filled = idx.fillna(0)
102
103 ctx.assert_list_equal([1, 2, 3], idx_filled.tolist(), "fillna (int64)")
104
105
106def test_dropna():
107 """Test dropna method (for int64, essentially a no-op)"""
108 f_print_header("Test: dropna")
109
110 idx = pandasCore.Index([1, 2, 3])
111 idx_dropped = idx.dropna()
isna (test_index.py:123)
113 ctx.assert_list_equal([1, 2, 3], idx_dropped.tolist(), "dropna (int64)")
114
115
116def test_isna_notna():
117 """Test isna, notna, isnull, notnull methods"""
118 f_print_header("Test: isna/notna")
119
120 idx = pandasCore.Index([1, 2, 3])
121
122 # For int64, isna should return all False
123 isna_result = list(idx.isna())
124 ctx.assert_list_equal([False, False, False], isna_result, "isna (int64)")
125
126 notna_result = list(idx.notna())
127 ctx.assert_list_equal([True, True, True], notna_result, "notna (int64)")
128
129 # Test aliases
130 isnull_result = list(idx.isnull())
131 ctx.assert_list_equal([False, False, False], isnull_result, "isnull (int64)")
132
133 notnull_result = list(idx.notnull())
isnull (test_index.py:130)
120 idx = pandasCore.Index([1, 2, 3])
121
122 # For int64, isna should return all False
123 isna_result = list(idx.isna())
124 ctx.assert_list_equal([False, False, False], isna_result, "isna (int64)")
125
126 notna_result = list(idx.notna())
127 ctx.assert_list_equal([True, True, True], notna_result, "notna (int64)")
128
129 # Test aliases
130 isnull_result = list(idx.isnull())
131 ctx.assert_list_equal([False, False, False], isnull_result, "isnull (int64)")
132
133 notnull_result = list(idx.notnull())
134 ctx.assert_list_equal([True, True, True], notnull_result, "notnull (int64)")
135
136
137def test_drop_duplicates():
138 """Test drop_duplicates method"""
139 f_print_header("Test: drop_duplicates")
notna (test_index.py:126)
116def test_isna_notna():
117 """Test isna, notna, isnull, notnull methods"""
118 f_print_header("Test: isna/notna")
119
120 idx = pandasCore.Index([1, 2, 3])
121
122 # For int64, isna should return all False
123 isna_result = list(idx.isna())
124 ctx.assert_list_equal([False, False, False], isna_result, "isna (int64)")
125
126 notna_result = list(idx.notna())
127 ctx.assert_list_equal([True, True, True], notna_result, "notna (int64)")
128
129 # Test aliases
130 isnull_result = list(idx.isnull())
131 ctx.assert_list_equal([False, False, False], isnull_result, "isnull (int64)")
132
133 notnull_result = list(idx.notnull())
134 ctx.assert_list_equal([True, True, True], notnull_result, "notnull (int64)")
notnull (test_index.py:133)
123 isna_result = list(idx.isna())
124 ctx.assert_list_equal([False, False, False], isna_result, "isna (int64)")
125
126 notna_result = list(idx.notna())
127 ctx.assert_list_equal([True, True, True], notna_result, "notna (int64)")
128
129 # Test aliases
130 isnull_result = list(idx.isnull())
131 ctx.assert_list_equal([False, False, False], isnull_result, "isnull (int64)")
132
133 notnull_result = list(idx.notnull())
134 ctx.assert_list_equal([True, True, True], notnull_result, "notnull (int64)")
135
136
137def test_drop_duplicates():
138 """Test drop_duplicates method"""
139 f_print_header("Test: drop_duplicates")
140
141 idx = pandasCore.Index([1, 2, 2, 3, 3, 3])
142
143 # keep='first' (default)
max (test_index.py:214)
204 ctx.assert_equal(3, uniques.size, "factorize uniques size")
205
206
207def test_min_max():
208 """Test min and max methods"""
209 f_print_header("Test: min/max")
210
211 idx = pandasCore.Index([3, 1, 4, 1, 5, 9, 2, 6])
212
213 ctx.assert_equal(1, idx.min(), "min")
214 ctx.assert_equal(9, idx.max(), "max")
215
216
217def test_argmin_argmax():
218 """Test argmin and argmax methods"""
219 f_print_header("Test: argmin/argmax")
220
221 idx = pandasCore.Index([3, 1, 4, 1, 5, 9, 2, 6])
222
223 ctx.assert_equal(1, idx.argmin(), "argmin") # First occurrence of min (1) is at index 1
224 ctx.assert_equal(5, idx.argmax(), "argmax") # Max (9) is at index 5
min (test_index.py:213)
203 # Verify uniques
204 ctx.assert_equal(3, uniques.size, "factorize uniques size")
205
206
207def test_min_max():
208 """Test min and max methods"""
209 f_print_header("Test: min/max")
210
211 idx = pandasCore.Index([3, 1, 4, 1, 5, 9, 2, 6])
212
213 ctx.assert_equal(1, idx.min(), "min")
214 ctx.assert_equal(9, idx.max(), "max")
215
216
217def test_argmin_argmax():
218 """Test argmin and argmax methods"""
219 f_print_header("Test: argmin/argmax")
220
221 idx = pandasCore.Index([3, 1, 4, 1, 5, 9, 2, 6])
222
223 ctx.assert_equal(1, idx.argmin(), "argmin") # First occurrence of min (1) is at index 1
nunique (test_index.py:178)
168 ctx.assert_list_equal([1, 2, 3], result.tolist(), "unique")
169
170
171def test_nunique():
172 """Test nunique method"""
173 f_print_header("Test: nunique")
174
175 idx = pandasCore.Index([1, 2, 2, 3, 3, 3])
176
177 ctx.assert_equal(3, idx.nunique(), "nunique")
178
179
180def test_value_counts():
181 """Test value_counts method"""
182 f_print_header("Test: value_counts")
183
184 idx = pandasCore.Index([1, 2, 2, 3, 3, 3])
185 result = idx.value_counts()
186
187 # Should return dict {3: 3, 2: 2, 1: 1} (sorted by count descending)
value_counts (test_index.py:186)
176 idx = pandasCore.Index([1, 2, 2, 3, 3, 3])
177
178 ctx.assert_equal(3, idx.nunique(), "nunique")
179
180
181def test_value_counts():
182 """Test value_counts method"""
183 f_print_header("Test: value_counts")
184
185 idx = pandasCore.Index([1, 2, 2, 3, 3, 3])
186 result = idx.value_counts()
187
188 # Should return dict {3: 3, 2: 2, 1: 1} (sorted by count descending)
189 ctx.assert_equal(3, result[3], "value_counts 3")
190 ctx.assert_equal(2, result[2], "value_counts 2")
191 ctx.assert_equal(1, result[1], "value_counts 1")
192
193
194def test_factorize():
195 """Test factorize method"""
196 f_print_header("Test: factorize")
groupby (test_index.py:593)
583 idx_some_nonzero = pandasCore.Index([0, 1, 0])
584 ctx.assert_true(idx_some_nonzero.any(), "any True")
585
586
587def test_groupby():
588 """Test groupby method"""
589 f_print_header("Test: groupby")
590
591 idx = pandasCore.Index([10, 20, 30, 40])
592 groups = idx.groupby([1, 1, 2, 2])
593
594 ctx.assert_list_equal([0, 1], groups[1], "groupby group 1")
595 ctx.assert_list_equal([2, 3], groups[2], "groupby group 2")
596
597
598def test_memory_usage():
599 """Test memory_usage method"""
600 f_print_header("Test: memory_usage")
601
602 idx = pandasCore.Index([1, 2, 3])
map (test_index.py:91)
81 idx_new = idx.set_names("new")
82
83 ctx.assert_equal("new", idx_new.name, "set_names")
84
85
86def test_map():
87 """Test map method"""
88 f_print_header("Test: map")
89
90 idx = pandasCore.Index([1, 2, 3])
91 idx_mapped = idx.map(lambda x: x * 2)
92
93 ctx.assert_list_equal([2, 4, 6], idx_mapped.tolist(), "map")
94
95
96def test_fillna():
97 """Test fillna method (for int64, essentially a no-op)"""
98 f_print_header("Test: fillna")
99
100 idx = pandasCore.Index([1, 2, 3])
101 idx_filled = idx.fillna(0)
argsort (test_index.py:232)
222 ctx.assert_equal(1, idx.argmin(), "argmin") # First occurrence of min (1) is at index 1
223 ctx.assert_equal(5, idx.argmax(), "argmax") # Max (9) is at index 5
224
225
226def test_argsort():
227 """Test argsort method"""
228 f_print_header("Test: argsort")
229
230 idx = pandasCore.Index([3, 1, 2])
231 result = list(idx.argsort())
232
233 ctx.assert_list_equal([1, 2, 0], result, "argsort")
234
235
236def test_searchsorted():
237 """Test searchsorted method"""
238 f_print_header("Test: searchsorted")
239
240 idx = pandasCore.Index([1, 2, 3, 4, 5])
searchsorted (test_index.py:244)
234 ctx.assert_list_equal([1, 2, 0], result, "argsort")
235
236
237def test_searchsorted():
238 """Test searchsorted method"""
239 f_print_header("Test: searchsorted")
240
241 idx = pandasCore.Index([1, 2, 3, 4, 5])
242
243 # Single value returns scalar
244 result = idx.searchsorted(3)
245 ctx.assert_equal(2, result, "searchsorted single")
246
247
248def test_append():
249 """Test append method"""
250 f_print_header("Test: append")
251
252 idx1 = pandasCore.Index([1, 2, 3])
253 idx2 = pandasCore.Index([4, 5])
254 result = idx1.append(idx2)
sort_values (test_index.py:459)
449 ctx.assert_true(idx1.identical(idx2), "identical True")
450 ctx.assert_false(idx1.identical(idx3), "identical False (different name)")
451
452
453def test_sort_values():
454 """Test sort_values method"""
455 f_print_header("Test: sort_values")
456
457 idx = pandasCore.Index([3, 1, 2])
458 result = idx.sort_values()
459
460 ctx.assert_list_equal([1, 2, 3], result.tolist(), "sort_values")
461
462 # Descending
463 result_desc = idx.sort_values(ascending=False)
464 ctx.assert_list_equal([3, 2, 1], result_desc.tolist(), "sort_values descending")
465
466
467def test_contains():
468 """Test __contains__ method"""
to_frame (test_index.py:690)
680 result = idx.item()
681
682 ctx.assert_equal(42, result, "item")
683
684
685def test_to_frame():
686 """Test to_frame method"""
687 f_print_header("Test: to_frame")
688
689 idx = pandasCore.Index([1, 2, 3], name="values")
690 frame_data = idx.to_frame()
691
692 ctx.assert_equal("values", frame_data["column_name"], "to_frame column_name")
693 ctx.assert_list_equal([1, 2, 3], frame_data["values"], "to_frame values")
694
695
696def test_to_series():
697 """Test to_series method"""
698 f_print_header("Test: to_series")
699
700 idx = pandasCore.Index([1, 2, 3], name="values")
transpose (test_index.py:722)
712 result = idx.to_flat_index()
713
714 ctx.assert_list_equal([1, 2, 3], result.tolist(), "to_flat_index")
715
716
717def test_transpose():
718 """Test transpose method"""
719 f_print_header("Test: transpose")
720
721 idx = pandasCore.Index([1, 2, 3])
722 result = idx.transpose()
723
724 ctx.assert_list_equal([1, 2, 3], result.tolist(), "transpose")
725
726
727def test_repeat():
728 """Test repeat method"""
729 f_print_header("Test: repeat")
730
731 idx = pandasCore.Index([1, 2, 3])
732 result = idx.repeat(2)
asof (test_index.py:616)
606 ctx.assert_true(mem > 0, "memory_usage positive")
607
608
609def test_asof():
610 """Test asof method"""
611 f_print_header("Test: asof")
612
613 idx = pandasCore.Index([1, 3, 5, 7])
614
615 # Exact match
616 result = idx.asof(3)
617 ctx.assert_equal(3, result, "asof exact")
618
619 # Lookup between values
620 result = idx.asof(4)
621 ctx.assert_equal(3, result, "asof between")
622
623
624def test_asof_locs():
625 """Test asof_locs method"""
626 f_print_header("Test: asof_locs")
diff (test_index.py:561)
551 result = idx.round(2)
552
553 ctx.assert_list_equal([1, 2, 3], result.tolist(), "round (int64 no-op)")
554
555
556def test_diff():
557 """Test diff method"""
558 f_print_header("Test: diff")
559
560 idx = pandasCore.Index([1, 3, 6, 10])
561 result = idx.diff()
562
563 ctx.assert_list_equal([0, 2, 3, 4], result.tolist(), "diff")
564
565
566def test_all():
567 """Test all method"""
568 f_print_header("Test: all")
569
570 idx_all_true = pandasCore.Index([1, 2, 3])
571 ctx.assert_true(idx_all_true.all(), "all True")
shift (test_index.py:538)
528 ctx.assert_list_equal([1, 2, 3], sorted_idx.tolist(), "sortlevel sorted")
529 ctx.assert_list_equal([1, 2, 0], indexer, "sortlevel indexer")
530
531
532def test_shift():
533 """Test shift method"""
534 f_print_header("Test: shift")
535
536 idx = pandasCore.Index([1, 2, 3, 4])
537 result = idx.shift(2)
538
539 ctx.assert_list_equal([0, 0, 1, 2], result.tolist(), "shift positive")
540
541 result_neg = idx.shift(-1)
542 ctx.assert_list_equal([2, 3, 4, 0], result_neg.tolist(), "shift negative")
543
544
545def test_round():
546 """Test round method (no-op for int64)"""
547 f_print_header("Test: round")
to_flat_index (test_index.py:712)
702 ctx.assert_list_equal([1, 2, 3], series_data["values"], "to_series values")
703 ctx.assert_equal("values", series_data["name"], "to_series name")
704
705
706def test_to_flat_index():
707 """Test to_flat_index method"""
708 f_print_header("Test: to_flat_index")
709
710 idx = pandasCore.Index([1, 2, 3])
711 result = idx.to_flat_index()
712
713 ctx.assert_list_equal([1, 2, 3], result.tolist(), "to_flat_index")
714
715
716def test_transpose():
717 """Test transpose method"""
718 f_print_header("Test: transpose")
719
720 idx = pandasCore.Index([1, 2, 3])
721 result = idx.transpose()
to_list (test_index.py:483)
473 ctx.assert_true(2 in idx, "__contains__ True")
474 ctx.assert_false(5 in idx, "__contains__ False")
475
476
477def test_to_list():
478 """Test to_list method"""
479 f_print_header("Test: to_list")
480
481 idx = pandasCore.Index([1, 2, 3])
482 result = idx.to_list()
483
484 ctx.assert_list_equal([1, 2, 3], result, "to_list")
485
486
487# ==================== Phase 2: Reshaping, Conversion & Testing ====================
488
489def test_drop():
490 """Test drop method"""
491 f_print_header("Test: drop")
to_series (test_index.py:701)
691 ctx.assert_equal("values", frame_data["column_name"], "to_frame column_name")
692 ctx.assert_list_equal([1, 2, 3], frame_data["values"], "to_frame values")
693
694
695def test_to_series():
696 """Test to_series method"""
697 f_print_header("Test: to_series")
698
699 idx = pandasCore.Index([1, 2, 3], name="values")
700 series_data = idx.to_series()
701
702 ctx.assert_list_equal([1, 2, 3], series_data["values"], "to_series values")
703 ctx.assert_equal("values", series_data["name"], "to_series name")
704
705
706def test_to_flat_index():
707 """Test to_flat_index method"""
708 f_print_header("Test: to_flat_index")
709
710 idx = pandasCore.Index([1, 2, 3])
tolist (test_index.py:61)
51 ctx.assert_true(idx_dup.has_duplicates, "has_duplicates")
52
53
54def test_copy():
55 """Test copy method"""
56 f_print_header("Test: copy")
57
58 idx = pandasCore.Index([1, 2, 3], name="original")
59 idx_copy = idx.copy()
60
61 ctx.assert_list_equal(idx.tolist(), idx_copy.tolist(), "copy values")
62 ctx.assert_equal(idx.name, idx_copy.name, "copy name")
63
64
65def test_rename():
66 """Test rename method"""
67 f_print_header("Test: rename")
68
69 idx = pandasCore.Index([1, 2, 3], name="old_name")
70 idx_new = idx.rename("new_name")
copy (test_index.py:59)
49 idx_dup = pandasCore.Index([1, 2, 2, 3, 3, 3])
50 ctx.assert_false(idx_dup.is_unique, "is_unique with dups")
51 ctx.assert_true(idx_dup.has_duplicates, "has_duplicates")
52
53
54def test_copy():
55 """Test copy method"""
56 f_print_header("Test: copy")
57
58 idx = pandasCore.Index([1, 2, 3], name="original")
59 idx_copy = idx.copy()
60
61 ctx.assert_list_equal(idx.tolist(), idx_copy.tolist(), "copy values")
62 ctx.assert_equal(idx.name, idx_copy.name, "copy name")
63
64
65def test_rename():
66 """Test rename method"""
67 f_print_header("Test: rename")
68
69 idx = pandasCore.Index([1, 2, 3], name="old_name")
infer_objects (test_index.py:762)
752 result = idx.putmask([False, True, False, True], 0)
753
754 ctx.assert_list_equal([1, 0, 3, 0], result.tolist(), "putmask")
755
756
757def test_infer_objects():
758 """Test infer_objects method"""
759 f_print_header("Test: infer_objects")
760
761 idx = pandasCore.Index([1, 2, 3])
762 result = idx.infer_objects()
763
764 ctx.assert_list_equal([1, 2, 3], result.tolist(), "infer_objects")
765
766
767def test_nlevels():
768 """Test nlevels property"""
769 f_print_header("Test: nlevels")
770
771 idx = pandasCore.Index([1, 2, 3])
view (test_index_methods.py:57)
47 tests_run += 1
48 idx = pandasCore.PeriodIndex(['2023-03', '2023-01', '2023-02'], freq='M')
49 result = idx.sort()
50 assert len(result) == 3, f"Expected 3 elements, got {len(result)}"
51
52def test_periodindex_view():
53 """Test PeriodIndex.view"""
54 global tests_run
55 tests_run += 1
56 idx = pandasCore.PeriodIndex(['2023-01', '2023-02'], freq='M')
57 result = idx.view()
58 assert len(result) == 2, f"Expected 2 elements, got {len(result)}"
59
60def test_intervalindex_infer_objects():
61 """Test IntervalIndex.infer_objects"""
62 global tests_run
63 tests_run += 1
64 idx = pandasCore.IntervalIndex.from_breaks([0, 1, 2, 3])
65 result = idx.infer_objects()
66 assert len(result) == 3, f"Expected 3 elements, got {len(result)}"
drop_duplicates (test_index.py:144)
134 ctx.assert_list_equal([True, True, True], notnull_result, "notnull (int64)")
135
136
137def test_drop_duplicates():
138 """Test drop_duplicates method"""
139 f_print_header("Test: drop_duplicates")
140
141 idx = pandasCore.Index([1, 2, 2, 3, 3, 3])
142
143 # keep='first' (default)
144 result = idx.drop_duplicates()
145 ctx.assert_list_equal([1, 2, 3], result.tolist(), "drop_duplicates (first)")
146
147 # keep='last'
148 result_last = idx.drop_duplicates(keep="last")
149 ctx.assert_list_equal([1, 2, 3], result_last.tolist(), "drop_duplicates (last)")
150
151
152def test_duplicated():
153 """Test duplicated method"""
154 f_print_header("Test: duplicated")
duplicated (test_index.py:158)
148 result_last = idx.drop_duplicates(keep="last")
149 ctx.assert_list_equal([1, 2, 3], result_last.tolist(), "drop_duplicates (last)")
150
151
152def test_duplicated():
153 """Test duplicated method"""
154 f_print_header("Test: duplicated")
155
156 idx = pandasCore.Index([1, 2, 2, 3])
157
158 result = list(idx.duplicated())
159 ctx.assert_list_equal([False, False, True, False], result, "duplicated")
160
161
162def test_unique():
163 """Test unique method"""
164 f_print_header("Test: unique")
165
166 idx = pandasCore.Index([1, 2, 2, 3, 3, 3])
167 result = idx.unique()
isin (test_index.py:331)
321 result_set = set(result.tolist())
322 expected_set = {2, 3}
323 ctx.assert_equal(expected_set, result_set, "join (inner)")
324
325
326def test_isin():
327 """Test isin method"""
328 f_print_header("Test: isin")
329
330 idx = pandasCore.Index([1, 2, 3, 4])
331 result = list(idx.isin([2, 4]))
332
333 ctx.assert_list_equal([False, True, False, True], result, "isin")
334
335
336def test_get_loc():
337 """Test get_loc method"""
338 f_print_header("Test: get_loc")
339
340 idx = pandasCore.Index([10, 20, 30, 40])
unique (test_index.py:167)
157 result = list(idx.duplicated())
158 ctx.assert_list_equal([False, False, True, False], result, "duplicated")
159
160
161def test_unique():
162 """Test unique method"""
163 f_print_header("Test: unique")
164
165 idx = pandasCore.Index([1, 2, 2, 3, 3, 3])
166 result = idx.unique()
167
168 ctx.assert_list_equal([1, 2, 3], result.tolist(), "unique")
169
170
171def test_nunique():
172 """Test nunique method"""
173 f_print_header("Test: nunique")
174
175 idx = pandasCore.Index([1, 2, 2, 3, 3, 3])
round (test_index.py:551)
541 result_neg = idx.shift(-1)
542 ctx.assert_list_equal([2, 3, 4, 0], result_neg.tolist(), "shift negative")
543
544
545def test_round():
546 """Test round method (no-op for int64)"""
547 f_print_header("Test: round")
548
549 idx = pandasCore.Index([1, 2, 3])
550 result = idx.round(2)
551
552 ctx.assert_list_equal([1, 2, 3], result.tolist(), "round (int64 no-op)")
553
554
555def test_diff():
556 """Test diff method"""
557 f_print_header("Test: diff")
558
559 idx = pandasCore.Index([1, 3, 6, 10])
560 result = idx.diff()
argmax (test_index.py:224)
214 ctx.assert_equal(9, idx.max(), "max")
215
216
217def test_argmin_argmax():
218 """Test argmin and argmax methods"""
219 f_print_header("Test: argmin/argmax")
220
221 idx = pandasCore.Index([3, 1, 4, 1, 5, 9, 2, 6])
222
223 ctx.assert_equal(1, idx.argmin(), "argmin") # First occurrence of min (1) is at index 1
224 ctx.assert_equal(5, idx.argmax(), "argmax") # Max (9) is at index 5
225
226
227def test_argsort():
228 """Test argsort method"""
229 f_print_header("Test: argsort")
230
231 idx = pandasCore.Index([3, 1, 2])
232 result = list(idx.argsort())
233
234 ctx.assert_list_equal([1, 2, 0], result, "argsort")
argmin (test_index.py:223)
213 ctx.assert_equal(1, idx.min(), "min")
214 ctx.assert_equal(9, idx.max(), "max")
215
216
217def test_argmin_argmax():
218 """Test argmin and argmax methods"""
219 f_print_header("Test: argmin/argmax")
220
221 idx = pandasCore.Index([3, 1, 4, 1, 5, 9, 2, 6])
222
223 ctx.assert_equal(1, idx.argmin(), "argmin") # First occurrence of min (1) is at index 1
224 ctx.assert_equal(5, idx.argmax(), "argmax") # Max (9) is at index 5
225
226
227def test_argsort():
228 """Test argsort method"""
229 f_print_header("Test: argsort")
230
231 idx = pandasCore.Index([3, 1, 2])
232 result = list(idx.argsort())
asof_locs (test_index.py:630)
620 result = idx.asof(4)
621 ctx.assert_equal(3, result, "asof between")
622
623
624def test_asof_locs():
625 """Test asof_locs method"""
626 f_print_header("Test: asof_locs")
627
628 idx = pandasCore.Index([1, 3, 5, 7])
629 where = pandasCore.Index([2, 4, 6])
630 result = idx.asof_locs(where)
631
632 ctx.assert_equal(3, len(result), "asof_locs length")
633
634
635def test_get_slice_bound():
636 """Test get_slice_bound method"""
637 f_print_header("Test: get_slice_bound")
638
639 idx = pandasCore.Index([1, 2, 3, 4, 5])
delete (test_index.py:425)
415 result = idx.insert(2, 3)
416
417 ctx.assert_list_equal([1, 2, 3, 4], result.tolist(), "insert")
418
419
420def test_delete():
421 """Test delete method"""
422 f_print_header("Test: delete")
423
424 idx = pandasCore.Index([1, 2, 3, 4])
425 result = idx.delete(1)
426
427 ctx.assert_list_equal([1, 3, 4], result.tolist(), "delete")
428
429
430def test_equals():
431 """Test equals method"""
432 f_print_header("Test: equals")
433
434 idx1 = pandasCore.Index([1, 2, 3])
435 idx2 = pandasCore.Index([1, 2, 3])
factorize (test_index.py:199)
189 ctx.assert_equal(3, result[3], "value_counts 3")
190 ctx.assert_equal(2, result[2], "value_counts 2")
191 ctx.assert_equal(1, result[1], "value_counts 1")
192
193
194def test_factorize():
195 """Test factorize method"""
196 f_print_header("Test: factorize")
197
198 idx = pandasCore.Index([3, 1, 2, 3, 1])
199 codes, uniques = idx.factorize()
200
201 # Verify codes length matches
202 ctx.assert_equal(5, len(list(codes)), "factorize codes length")
203 # Verify uniques
204 ctx.assert_equal(3, uniques.size, "factorize uniques size")
205
206
207def test_min_max():
208 """Test min and max methods"""
209 f_print_header("Test: min/max")
format (test_index.py:742)
732 result = idx.repeat(2)
733
734 ctx.assert_list_equal([1, 1, 2, 2, 3, 3], result.tolist(), "repeat scalar")
735
736
737def test_format():
738 """Test format method"""
739 f_print_header("Test: format")
740
741 idx = pandasCore.Index([1, 2, 3])
742 result = idx.format()
743
744 ctx.assert_list_equal(["1", "2", "3"], result, "format")
745
746
747def test_putmask():
748 """Test putmask method"""
749 f_print_header("Test: putmask")
750
751 idx = pandasCore.Index([1, 2, 3, 4])
752 result = idx.putmask([False, True, False, True], 0)
get_indexer (test_index.py:362)
352 ctx.tests_run += 1
353 ctx.tests_passed += 1
354
355
356def test_get_indexer():
357 """Test get_indexer method"""
358 f_print_header("Test: get_indexer")
359
360 idx = pandasCore.Index([1, 2, 3])
361 target = pandasCore.Index([1, 3, 5])
362 result = list(idx.get_indexer(target))
363
364 ctx.assert_list_equal([0, 2, -1], result, "get_indexer")
365
366
367def test_get_indexer_for():
368 """Test get_indexer_for method"""
369 f_print_header("Test: get_indexer_for")
370
371 idx = pandasCore.Index([1, 2, 3])
372 result = list(idx.get_indexer_for([1, 3]))
get_indexer_for (test_index.py:372)
362 result = list(idx.get_indexer(target))
363
364 ctx.assert_list_equal([0, 2, -1], result, "get_indexer")
365
366
367def test_get_indexer_for():
368 """Test get_indexer_for method"""
369 f_print_header("Test: get_indexer_for")
370
371 idx = pandasCore.Index([1, 2, 3])
372 result = list(idx.get_indexer_for([1, 3]))
373
374 ctx.assert_list_equal([0, 2], result, "get_indexer_for")
375
376
377def test_get_indexer_non_unique():
378 """Test get_indexer_non_unique method"""
379 f_print_header("Test: get_indexer_non_unique")
380
381 idx = pandasCore.Index([1, 2, 2, 3])
382 target = pandasCore.Index([2, 5])
get_indexer_non_unique (test_index.py:383)
373 ctx.assert_list_equal([0, 2], result, "get_indexer_for")
374
375
376def test_get_indexer_non_unique():
377 """Test get_indexer_non_unique method"""
378 f_print_header("Test: get_indexer_non_unique")
379
380 idx = pandasCore.Index([1, 2, 2, 3])
381 target = pandasCore.Index([2, 5])
382 indexer, missing = idx.get_indexer_non_unique(target)
383
384 # Should find indices for 2 and mark 5 as missing
385 ctx.assert_true(len(list(indexer)) > 0, "get_indexer_non_unique has indexer")
386
387
388def test_slice_locs():
389 """Test slice_locs method"""
390 f_print_header("Test: slice_locs")
391
392 idx = pandasCore.Index([1, 2, 3, 4, 5])
get_level_values (test_index.py:505)
495 result = idx.drop([2, 4])
496
497 ctx.assert_list_equal([1, 3, 5], result.tolist(), "drop")
498
499
500def test_get_level_values():
501 """Test get_level_values method"""
502 f_print_header("Test: get_level_values")
503
504 idx = pandasCore.Index([1, 2, 3], name="level0")
505 result = idx.get_level_values(0)
506
507 ctx.assert_list_equal([1, 2, 3], result.tolist(), "get_level_values")
508
509
510def test_reindex():
511 """Test reindex method"""
512 f_print_header("Test: reindex")
513
514 idx = pandasCore.Index([1, 2, 3])
515 target = pandasCore.Index([1, 3, 5])
get_loc (test_index.py:342)
332 ctx.assert_list_equal([False, True, False, True], result, "isin")
333
334
335def test_get_loc():
336 """Test get_loc method"""
337 f_print_header("Test: get_loc")
338
339 idx = pandasCore.Index([10, 20, 30, 40])
340
341 ctx.assert_equal(1, idx.get_loc(20), "get_loc")
342
343 # Test KeyError for missing key
344 try:
345 idx.get_loc(99)
346 f_print_error("get_loc should raise KeyError for missing key")
347 ctx.tests_run += 1
348 ctx.tests_failed += 1
349 except KeyError:
350 f_print_success("get_loc raises KeyError for missing key: PASS")
351 ctx.tests_run += 1
get_slice_bound (test_index.py:641)
631 ctx.assert_equal(3, len(result), "asof_locs length")
632
633
634def test_get_slice_bound():
635 """Test get_slice_bound method"""
636 f_print_header("Test: get_slice_bound")
637
638 idx = pandasCore.Index([1, 2, 3, 4, 5])
639
640 left_bound = idx.get_slice_bound(3, "left")
641 ctx.assert_equal(2, left_bound, "get_slice_bound left")
642
643 right_bound = idx.get_slice_bound(3, "right")
644 ctx.assert_equal(3, right_bound, "get_slice_bound right")
645
646
647def test_type_checks():
648 """Test type checking methods"""
649 f_print_header("Test: type checking methods")
holds_integer (test_index.py:654)
644 right_bound = idx.get_slice_bound(3, "right")
645 ctx.assert_equal(3, right_bound, "get_slice_bound right")
646
647
648def test_type_checks():
649 """Test type checking methods"""
650 f_print_header("Test: type checking methods")
651
652 idx = pandasCore.Index([1, 2, 3])
653
654 ctx.assert_true(idx.holds_integer(), "holds_integer")
655 ctx.assert_true(idx.is_integer(), "is_integer")
656 ctx.assert_true(idx.is_numeric(), "is_numeric")
657 ctx.assert_false(idx.is_boolean(), "is_boolean")
658 ctx.assert_false(idx.is_floating(), "is_floating")
659 ctx.assert_false(idx.is_categorical(), "is_categorical")
660 ctx.assert_false(idx.is_interval(), "is_interval")
661 ctx.assert_false(idx.is_object(), "is_object")
662
663
664def test_is_():
item (test_index.py:680)
670 # Different objects
671 ctx.assert_false(idx1.is_(idx2), "is_ different objects")
672
673
674def test_item():
675 """Test item method"""
676 f_print_header("Test: item")
677
678 idx = pandasCore.Index([42])
679 result = idx.item()
680
681 ctx.assert_equal(42, result, "item")
682
683
684def test_to_frame():
685 """Test to_frame method"""
686 f_print_header("Test: to_frame")
687
688 idx = pandasCore.Index([1, 2, 3], name="values")
689 frame_data = idx.to_frame()
memory_usage (test_index.py:604)
594 ctx.assert_list_equal([0, 1], groups[1], "groupby group 1")
595 ctx.assert_list_equal([2, 3], groups[2], "groupby group 2")
596
597
598def test_memory_usage():
599 """Test memory_usage method"""
600 f_print_header("Test: memory_usage")
601
602 idx = pandasCore.Index([1, 2, 3])
603 mem = idx.memory_usage()
604
605 ctx.assert_true(mem > 0, "memory_usage positive")
606
607
608def test_asof():
609 """Test asof method"""
610 f_print_header("Test: asof")
611
612 idx = pandasCore.Index([1, 3, 5, 7])
putmask (test_index.py:752)
742 result = idx.format()
743
744 ctx.assert_list_equal(["1", "2", "3"], result, "format")
745
746
747def test_putmask():
748 """Test putmask method"""
749 f_print_header("Test: putmask")
750
751 idx = pandasCore.Index([1, 2, 3, 4])
752 result = idx.putmask([False, True, False, True], 0)
753
754 ctx.assert_list_equal([1, 0, 3, 0], result.tolist(), "putmask")
755
756
757def test_infer_objects():
758 """Test infer_objects method"""
759 f_print_header("Test: infer_objects")
760
761 idx = pandasCore.Index([1, 2, 3])
762 result = idx.infer_objects()
repeat (test_index.py:732)
722 result = idx.transpose()
723
724 ctx.assert_list_equal([1, 2, 3], result.tolist(), "transpose")
725
726
727def test_repeat():
728 """Test repeat method"""
729 f_print_header("Test: repeat")
730
731 idx = pandasCore.Index([1, 2, 3])
732 result = idx.repeat(2)
733
734 ctx.assert_list_equal([1, 1, 2, 2, 3, 3], result.tolist(), "repeat scalar")
735
736
737def test_format():
738 """Test format method"""
739 f_print_header("Test: format")
740
741 idx = pandasCore.Index([1, 2, 3])
742 result = idx.format()
slice_locs (test_index.py:394)
384 # Should find indices for 2 and mark 5 as missing
385 ctx.assert_true(len(list(indexer)) > 0, "get_indexer_non_unique has indexer")
386
387
388def test_slice_locs():
389 """Test slice_locs method"""
390 f_print_header("Test: slice_locs")
391
392 idx = pandasCore.Index([1, 2, 3, 4, 5])
393 start, stop = idx.slice_locs(2, 4)
394
395 ctx.assert_equal(1, start, "slice_locs start")
396 ctx.assert_equal(4, stop, "slice_locs stop")
397
398
399def test_take():
400 """Test take method"""
401 f_print_header("Test: take")
402
403 idx = pandasCore.Index([10, 20, 30, 40])
sort (test_index_methods.py:16)
6import pandasCore
7
8tests_run = 0
9
10def test_index_sort():
11 """Test Index.sort"""
12 global tests_run
13 tests_run += 1
14 idx = pandasCore.Index([3, 1, 2])
15 result = idx.sort()
16 assert len(result) == 3, f"Expected 3 elements, got {len(result)}"
17
18def test_timedeltaindex_sortlevel():
19 """Test TimedeltaIndex.sortlevel"""
20 global tests_run
21 tests_run += 1
22 idx = pandasCore.TimedeltaIndex(['1 day', '2 days', '1 hour'])
23 result = idx.sortlevel()
24 # Returns tuple of (sorted_index, indexer)
25 assert len(result) == 2, f"Expected tuple of 2, got {len(result)}"
sortlevel (test_index.py:527)
517 ctx.assert_list_equal([1, 3, 5], new_idx.tolist(), "reindex new_idx")
518 ctx.assert_list_equal([0, 2, -1], indexer, "reindex indexer")
519
520
521def test_sortlevel():
522 """Test sortlevel method"""
523 f_print_header("Test: sortlevel")
524
525 idx = pandasCore.Index([3, 1, 2])
526 sorted_idx, indexer = idx.sortlevel()
527
528 ctx.assert_list_equal([1, 2, 3], sorted_idx.tolist(), "sortlevel sorted")
529 ctx.assert_list_equal([1, 2, 0], indexer, "sortlevel indexer")
530
531
532def test_shift():
533 """Test shift method"""
534 f_print_header("Test: shift")
535
536 idx = pandasCore.Index([1, 2, 3, 4])