Index ===== .. currentmodule:: pandasCore .. class:: Index Immutable sequence used for indexing and alignment. Example ------- .. code-block:: python import pandasCore as pd # Create Index idx = pd.Index([1, 2, 3], name='my_index') print(len(idx)) # 3 Attributes ---------- .. list-table:: :widths: 25 60 15 :header-rows: 1 * - Attribute - Description - Example * - :attr:`~Index.T` - Return the transpose (returns copy for 1D Index) - * - :attr:`~Index.dtype` - Return the dtype object of the underlying data - :ref:`View ` * - :attr:`~Index.empty` - Indicator whether Index is empty - :ref:`View ` * - :attr:`~Index.has_duplicates` - Return True if Index has duplicate values - * - :attr:`~Index.hasnans` - Return True if Index has NaN values - * - :attr:`~Index.inferred_type` - Return string describing the type of the Index - * - :attr:`~Index.is_monotonic_decreasing` - Return if the index is monotonic decreasing - :ref:`View ` * - :attr:`~Index.is_monotonic_increasing` - Return if the index is monotonic increasing - :ref:`View ` * - :attr:`~Index.is_unique` - Return if the index has unique values - :ref:`View ` * - :attr:`~Index.name` - Return the name of the Index - :ref:`View ` * - :attr:`~Index.nbytes` - Return the number of bytes in the underlying data - * - :attr:`~Index.ndim` - Number of dimensions (always 1 for Index) - :ref:`View ` * - :attr:`~Index.nlevels` - Number of levels (always 1 for non-MultiIndex) - :ref:`View ` * - :attr:`~Index.shape` - Return a tuple of the shape of the underlying data - :ref:`View ` * - :attr:`~Index.size` - Return the number of elements in the Index - :ref:`View ` * - :attr:`~Index.str` - Vectorized string functions for Index. - :ref:`View ` * - :attr:`~Index.values` - Return an array representing the data in the Index - Construction ------------ .. list-table:: :widths: 25 60 15 :header-rows: 1 * - Method - Description - Example * - :meth:`~Index.__init__` ``(data: object = None, dtype: object = None, copy: bool = False, name: object = None, tupleize_cols: bool = True)`` - Immutable sequence used for indexing and alignment. - :ref:`View ` Indexing / Selection -------------------- .. list-table:: :widths: 25 60 15 :header-rows: 1 * - Method - Description - Example * - :meth:`~Index.__getitem__` ``(arg0: typing.SupportsInt)`` - Get element by position - * - :meth:`~Index.take` ``(indices: collections.abc.Sequence[typing.SupportsInt], axis: typing.SupportsInt = 0, allow_fill: bool = True, fill_value: object = None, **kwargs)`` - Return a new Index of the values selected by the indices. - :ref:`View ` * - :meth:`~Index.where` ``(cond: object, other: object = None)`` - Replace values where the condition is False. - Data Manipulation ----------------- .. list-table:: :widths: 25 60 15 :header-rows: 1 * - Method - Description - Example * - :meth:`~Index.drop` ``(labels: collections.abc.Sequence[typing.SupportsInt], errors: str = 'raise')`` - Make new Index with passed list of labels deleted. - :ref:`View ` * - :meth:`~Index.droplevel` ``(level: object = 0)`` - Return Index with requested level removed. - * - :meth:`~Index.insert` ``(loc: typing.SupportsInt, item: typing.SupportsInt)`` - Make new Index inserting new item at location. - :ref:`View ` * - :meth:`~Index.reindex` ``(target: pandasCore.Index, method: object = None, level: object = None, limit: object = None, tolerance: object = None)`` - Create index with target's values. - :ref:`View ` * - :meth:`~Index.rename` ``(name: str | None, *, inplace: bool = False)`` - Alter Index name. - :ref:`View ` * - :meth:`~Index.set_names` ``(names: str | None, *, level: object = None, inplace: bool = False)`` - Set Index name. - :ref:`View ` Missing Data ------------ .. list-table:: :widths: 25 60 15 :header-rows: 1 * - Method - Description - Example * - :meth:`~Index.dropna` ``(how: str = 'any')`` - Return Index without NA/NaN values. - :ref:`View ` * - :meth:`~Index.fillna` ``(value: object = None, downcast: object = None)`` - Fill NA/NaN values with the specified value. - :ref:`View ` * - :meth:`~Index.isna` ``()`` - Detect missing values. - :ref:`View ` * - :meth:`~Index.isnull` ``()`` - Detect missing values. Alias for isna. - :ref:`View ` * - :meth:`~Index.notna` ``()`` - Detect existing (non-missing) values. - :ref:`View ` * - :meth:`~Index.notnull` ``()`` - Detect existing (non-missing) values. Alias for notna. - :ref:`View ` Statistics ---------- .. list-table:: :widths: 25 60 15 :header-rows: 1 * - Method - Description - Example * - :meth:`~Index.max` ``(axis: object = None, skipna: bool = True)`` - Return the maximum value of the Index. - :ref:`View ` * - :meth:`~Index.min` ``(axis: object = None, skipna: bool = True)`` - Return the minimum value of the Index. - :ref:`View ` * - :meth:`~Index.nunique` ``(dropna: bool = True)`` - Return number of unique elements in the Index. - :ref:`View ` * - :meth:`~Index.value_counts` ``(normalize: bool = False, sort: bool = True, ascending: bool = False, bins: object = None, dropna: bool = True)`` - Return a dict containing counts of unique values. - :ref:`View ` Aggregation ----------- .. list-table:: :widths: 25 60 15 :header-rows: 1 * - Method - Description - Example * - :meth:`~Index.groupby` ``(values: collections.abc.Sequence[typing.SupportsInt])`` - Group the index labels by a mapper. - :ref:`View ` * - :meth:`~Index.map` ``(mapper: collections.abc.Callable, na_action: object = None)`` - Map values using input correspondence. - :ref:`View ` Comparison ---------- .. list-table:: :widths: 25 60 15 :header-rows: 1 * - Method - Description - Example * - :meth:`~Index.__eq__` ``(arg0: object)`` - - * - :meth:`~Index.__ge__` ``(arg0: typing.SupportsInt)`` - - * - :meth:`~Index.__gt__` ``(arg0: typing.SupportsInt)`` - - * - :meth:`~Index.__le__` ``(arg0: typing.SupportsInt)`` - - * - :meth:`~Index.__lt__` ``(arg0: typing.SupportsInt)`` - - * - :meth:`~Index.__ne__` ``(arg0: object)`` - - * - :meth:`~Index.equals` ``(other: pandasCore.Index)`` - Determine if two Index objects are equal. - Sorting ------- .. list-table:: :widths: 25 60 15 :header-rows: 1 * - Method - Description - Example * - :meth:`~Index.argsort` ``(*args, **kwargs)`` - Return the integer indices that would sort the Index. - :ref:`View ` * - :meth:`~Index.searchsorted` ``(value: object, side: str = 'left', sorter: object = None)`` - Find indices where elements should be inserted to maintain order. - :ref:`View ` * - :meth:`~Index.sort_values` ``(*, return_indexer: bool = False, ascending: bool = True, na_position: str = 'last', key: object = None)`` - Return a sorted copy of the Index. - :ref:`View ` Reshaping --------- .. list-table:: :widths: 25 60 15 :header-rows: 1 * - Method - Description - Example * - :meth:`~Index.to_frame` ``(index: bool = True, name: object = None)`` - Create a DataFrame with a column containing the Index. - :ref:`View ` * - :meth:`~Index.transpose` ``(*args)`` - Return the transpose, which is by definition self. - :ref:`View ` Combining --------- .. list-table:: :widths: 25 60 15 :header-rows: 1 * - Method - Description - Example * - :meth:`~Index.append` ``(other: pandasCore.Index)`` - Append a collection of Index objects together. - * - :meth:`~Index.join` ``(other: pandasCore.Index, *, how: str = 'left', level: object = None, return_indexers: bool = False, sort: bool = False)`` - Compute join between Index and other Index. - Time Series ----------- .. list-table:: :widths: 25 60 15 :header-rows: 1 * - Method - Description - Example * - :meth:`~Index.asof` ``(label: typing.SupportsInt)`` - Return the label from the index, or, if not present, the previous one. - :ref:`View ` * - :meth:`~Index.diff` ``(periods: typing.SupportsInt = 1)`` - Computes the difference between consecutive values. - :ref:`View ` * - :meth:`~Index.shift` ``(periods: typing.SupportsInt = 1, freq: object = None)`` - Shift index by desired number of periods. - :ref:`View ` I/O --- .. list-table:: :widths: 25 60 15 :header-rows: 1 * - Method - Description - Example * - :meth:`~Index.to_flat_index` ``()`` - Identity method. - :ref:`View ` * - :meth:`~Index.to_list` ``()`` - Return a list of the Index values - :ref:`View ` * - :meth:`~Index.to_numpy` ``(dtype: object = None, copy: bool = False, na_value: object = None, **kwargs)`` - Return a NumPy ndarray representing the values in this Index - * - :meth:`~Index.to_series` ``(index: object = None, name: object = None)`` - Create a Series with both index and values equal to the index keys. - :ref:`View ` * - :meth:`~Index.tolist` ``()`` - Return a list of the Index values - :ref:`View ` Conversion ---------- .. list-table:: :widths: 25 60 15 :header-rows: 1 * - Method - Description - Example * - :meth:`~Index.astype` ``(dtype: object, copy: bool = True)`` - Create an Index with values cast to specified dtype. - * - :meth:`~Index.copy` ``(name: object = None, deep: bool = False)`` - Make a copy of this object. - :ref:`View ` * - :meth:`~Index.infer_objects` ``(copy: bool = True)`` - Attempt to infer better dtypes for object columns. - :ref:`View ` * - :meth:`~Index.view` ``(cls: object = None)`` - Return a view of the Index data. - :ref:`View ` Iteration --------- .. list-table:: :widths: 25 60 15 :header-rows: 1 * - Method - Description - Example * - :meth:`~Index.__contains__` ``(arg0: typing.SupportsInt)`` - Return True if key is in the Index - * - :meth:`~Index.__iter__` ``()`` - Iterate over the Index values - * - :meth:`~Index.__len__` ``()`` - Return the number of elements - Set Operations -------------- .. list-table:: :widths: 25 60 15 :header-rows: 1 * - Method - Description - Example * - :meth:`~Index.difference` ``(other: pandasCore.Index, sort: object = None)`` - Return a new Index with elements of index not in other. - * - :meth:`~Index.drop_duplicates` ``(*, keep: str = 'first')`` - Return Index with duplicate values removed. - :ref:`View ` * - :meth:`~Index.duplicated` ``(keep: str = 'first')`` - Indicate duplicate index values. - :ref:`View ` * - :meth:`~Index.intersection` ``(other: pandasCore.Index, sort: bool = False)`` - Form the intersection of two Index objects. - * - :meth:`~Index.isin` ``(values: collections.abc.Sequence[typing.SupportsInt], level: object = None)`` - Return a boolean array indicating whether each element is in values. - :ref:`View ` * - :meth:`~Index.symmetric_difference` ``(other: pandasCore.Index, result_name: object = None, sort: object = None)`` - Compute the symmetric difference of two Index objects. - * - :meth:`~Index.union` ``(other: pandasCore.Index, sort: object = None)`` - Form the union of two Index objects. - * - :meth:`~Index.unique` ``(level: object = None)`` - Return unique values in the Index. - :ref:`View ` Datetime Methods ---------------- .. list-table:: :widths: 25 60 15 :header-rows: 1 * - Method - Description - Example * - :meth:`~Index.round` ``(decimals: typing.SupportsInt = 0)`` - Round each value in the Index to the given number of decimals. - :ref:`View ` Other Methods ------------- .. list-table:: :widths: 25 60 15 :header-rows: 1 * - Method - Description - Example * - :meth:`~Index.__hash__` - - * - :meth:`~Index.__repr__` ``()`` - - * - :meth:`~Index.__str__` ``(self, /)`` - Return str(self). - * - :meth:`~Index.all` ``(*args, **kwargs)`` - Return whether all elements are Truthy. - * - :meth:`~Index.any` ``(*args, **kwargs)`` - Return whether any element is Truthy. - * - :meth:`~Index.argmax` ``(axis: object = None, skipna: bool = True, *args, **kwargs)`` - Return int position of the largest value in the Index. - :ref:`View ` * - :meth:`~Index.argmin` ``(axis: object = None, skipna: bool = True, *args, **kwargs)`` - Return int position of the smallest value in the Index. - :ref:`View ` * - :meth:`~Index.asof_locs` ``(where: pandasCore.Index, mask: collections.abc.Sequence[bool] | None = None)`` - Return the locations for asof-lookup of labels. - :ref:`View ` * - :meth:`~Index.delete` ``(loc: object)`` - Make new Index with element(s) at position(s) removed. - :ref:`View ` * - :meth:`~Index.factorize` ``(sort: bool = False, use_na_sentinel: bool = True)`` - Encode the object as an enumerated type. - :ref:`View ` * - :meth:`~Index.format` ``(name: bool = False, formatter: object = None, na_rep: str = 'NaN')`` - Render a string representation of the Index. - :ref:`View ` * - :meth:`~Index.get_indexer` ``(target: pandasCore.Index, method: object = None, limit: object = None, tolerance: object = None)`` - Compute indexer and mask for new index given the current index. - :ref:`View ` * - :meth:`~Index.get_indexer_for` ``(target: collections.abc.Sequence[typing.SupportsInt])`` - Guaranteed to return an indexer even when target contains non-uniqu... - :ref:`View ` * - :meth:`~Index.get_indexer_non_unique` ``(target: pandasCore.Index)`` - Compute indexer and mask for new index given a non-unique current i... - :ref:`View ` * - :meth:`~Index.get_level_values` ``(level: object)`` - Return an Index of values for requested level. - :ref:`View ` * - :meth:`~Index.get_loc` ``(key: typing.SupportsInt)`` - Get integer location for requested label. - :ref:`View ` * - :meth:`~Index.get_slice_bound` ``(label: typing.SupportsInt, side: str)`` - Calculate slice bound that corresponds to given label. - :ref:`View ` * - :meth:`~Index.holds_integer` ``()`` - Whether the type is an integer type. - :ref:`View ` * - :meth:`~Index.identical` ``(other: pandasCore.Index)`` - Check if two Index objects are identical (values and metadata). - * - :meth:`~Index.item` ``()`` - Return the first element of the underlying data as a Python scalar. - :ref:`View ` * - :meth:`~Index.memory_usage` ``(deep: bool = False)`` - Memory usage of the Index. - :ref:`View ` * - :meth:`~Index.putmask` ``(mask: collections.abc.Sequence[bool], value: typing.SupportsInt)`` - Return a new Index with values put in locations specified by mask. - :ref:`View ` * - :meth:`~Index.ravel` ``(order: object = 'C')`` - Return a flattened numpy array representing the values. - * - :meth:`~Index.repeat` ``(repeats: object, axis: object = None)`` - Repeat elements of an Index. - :ref:`View ` * - :meth:`~Index.slice_indexer` ``(start: object = None, end: object = None, step: object = None)`` - Compute slice indexer for input labels and step. - * - :meth:`~Index.slice_locs` ``(start: object = None, end: object = None, step: object = None)`` - Compute slice locations for input labels. - :ref:`View ` * - :meth:`~Index.sort` ``(*args, **kwargs) | (*args, **kwargs)`` - Return a sorted copy of the Index. - :ref:`View ` * - :meth:`~Index.sortlevel` ``(level: object = None, ascending: object = True, sort_remaining: object = None, na_position: str = 'first')`` - Sort Index by the requested level. - :ref:`View ` Code Examples ------------- The following examples are extracted from the test suite. .. _example-index-dtype-0: .. dropdown:: dtype (test_index.py:40) :class-title: example-dropdown .. code-block:: python :linenos: :lineno-start: 30 :emphasize-lines: 11 # ==================== Test Functions ==================== def test_constructor_and_properties(): """Test Index constructor and basic properties""" f_print_header("Test: Constructor and Properties") # Basic construction idx = pandasCore.Index([1, 2, 3, 4, 5], name="test_idx") ctx.assert_equal(5, idx.size, "size") ctx.assert_equal("test_idx", idx.name, "name") ctx.assert_equal("int64", idx.dtype, "dtype") ctx.assert_false(idx.empty, "empty") ctx.assert_true(idx.is_unique, "is_unique") ctx.assert_true(idx.is_monotonic_increasing, "is_monotonic_increasing") ctx.assert_false(idx.is_monotonic_decreasing, "is_monotonic_decreasing") ctx.assert_equal(1, idx.ndim, "ndim") ctx.assert_equal((5,), idx.shape, "shape") # Duplicate values idx_dup = pandasCore.Index([1, 2, 2, 3, 3, 3]) ctx.assert_false(idx_dup.is_unique, "is_unique with dups") .. _example-index-empty-1: .. dropdown:: empty (test_index.py:41) :class-title: example-dropdown .. code-block:: python :linenos: :lineno-start: 31 :emphasize-lines: 11 def test_constructor_and_properties(): """Test Index constructor and basic properties""" f_print_header("Test: Constructor and Properties") # Basic construction idx = pandasCore.Index([1, 2, 3, 4, 5], name="test_idx") ctx.assert_equal(5, idx.size, "size") ctx.assert_equal("test_idx", idx.name, "name") ctx.assert_equal("int64", idx.dtype, "dtype") ctx.assert_false(idx.empty, "empty") ctx.assert_true(idx.is_unique, "is_unique") ctx.assert_true(idx.is_monotonic_increasing, "is_monotonic_increasing") ctx.assert_false(idx.is_monotonic_decreasing, "is_monotonic_decreasing") ctx.assert_equal(1, idx.ndim, "ndim") ctx.assert_equal((5,), idx.shape, "shape") # Duplicate values idx_dup = pandasCore.Index([1, 2, 2, 3, 3, 3]) ctx.assert_false(idx_dup.is_unique, "is_unique with dups") ctx.assert_true(idx_dup.has_duplicates, "has_duplicates") .. _example-index-is_monotonic_decreasing-2: .. dropdown:: is_monotonic_decreasing (test_index.py:44) :class-title: example-dropdown .. code-block:: python :linenos: :lineno-start: 34 :emphasize-lines: 11 f_print_header("Test: Constructor and Properties") # Basic construction idx = pandasCore.Index([1, 2, 3, 4, 5], name="test_idx") ctx.assert_equal(5, idx.size, "size") ctx.assert_equal("test_idx", idx.name, "name") ctx.assert_equal("int64", idx.dtype, "dtype") ctx.assert_false(idx.empty, "empty") ctx.assert_true(idx.is_unique, "is_unique") ctx.assert_true(idx.is_monotonic_increasing, "is_monotonic_increasing") ctx.assert_false(idx.is_monotonic_decreasing, "is_monotonic_decreasing") ctx.assert_equal(1, idx.ndim, "ndim") ctx.assert_equal((5,), idx.shape, "shape") # Duplicate values idx_dup = pandasCore.Index([1, 2, 2, 3, 3, 3]) ctx.assert_false(idx_dup.is_unique, "is_unique with dups") ctx.assert_true(idx_dup.has_duplicates, "has_duplicates") def test_copy(): .. _example-index-is_monotonic_increasing-3: .. dropdown:: is_monotonic_increasing (test_example.py:185) :class-title: example-dropdown .. code-block:: python :linenos: :lineno-start: 175 :emphasize-lines: 11 return False # Check is_unique if idx.is_unique: f_print_success("is_unique is True") else: f_print_error("is_unique should be True") return False # Check is_monotonic_increasing if idx.is_monotonic_increasing: f_print_success("is_monotonic_increasing is True") else: f_print_error("is_monotonic_increasing should be True") return False # Check len if len(idx) == 5: f_print_success("len(idx) is 5") else: f_print_error(f"len(idx) is {len(idx)}, expected 5") .. _example-index-is_unique-4: .. dropdown:: is_unique (test_example.py:178) :class-title: example-dropdown .. code-block:: python :linenos: :lineno-start: 168 :emphasize-lines: 11 return False # Check name if idx.name == "test_index": f_print_success(f"Name is correct: {idx.name}") else: f_print_error(f"Name is wrong: {idx.name}, expected 'test_index'") return False # Check is_unique if idx.is_unique: f_print_success("is_unique is True") else: f_print_error("is_unique should be True") return False # Check is_monotonic_increasing if idx.is_monotonic_increasing: f_print_success("is_monotonic_increasing is True") else: f_print_error("is_monotonic_increasing should be True") .. _example-index-name-5: .. dropdown:: name (test_example.py:171) :class-title: example-dropdown .. code-block:: python :linenos: :lineno-start: 161 :emphasize-lines: 11 f_print_success("Created Index from list") # Check size if idx.size == 5: f_print_success(f"Size is correct: {idx.size}") else: f_print_error(f"Size is wrong: {idx.size}, expected 5") return False # Check name if idx.name == "test_index": f_print_success(f"Name is correct: {idx.name}") else: f_print_error(f"Name is wrong: {idx.name}, expected 'test_index'") return False # Check is_unique if idx.is_unique: f_print_success("is_unique is True") else: f_print_error("is_unique should be True") .. _example-index-ndim-6: .. dropdown:: ndim (test_index.py:45) :class-title: example-dropdown .. code-block:: python :linenos: :lineno-start: 35 :emphasize-lines: 11 # Basic construction idx = pandasCore.Index([1, 2, 3, 4, 5], name="test_idx") ctx.assert_equal(5, idx.size, "size") ctx.assert_equal("test_idx", idx.name, "name") ctx.assert_equal("int64", idx.dtype, "dtype") ctx.assert_false(idx.empty, "empty") ctx.assert_true(idx.is_unique, "is_unique") ctx.assert_true(idx.is_monotonic_increasing, "is_monotonic_increasing") ctx.assert_false(idx.is_monotonic_decreasing, "is_monotonic_decreasing") ctx.assert_equal(1, idx.ndim, "ndim") ctx.assert_equal((5,), idx.shape, "shape") # Duplicate values idx_dup = pandasCore.Index([1, 2, 2, 3, 3, 3]) ctx.assert_false(idx_dup.is_unique, "is_unique with dups") ctx.assert_true(idx_dup.has_duplicates, "has_duplicates") def test_copy(): """Test copy method""" .. _example-index-nlevels-7: .. dropdown:: nlevels (test_index.py:773) :class-title: example-dropdown .. code-block:: python :linenos: :lineno-start: 763 :emphasize-lines: 11 ctx.assert_list_equal([1, 2, 3], result.tolist(), "infer_objects") def test_nlevels(): """Test nlevels property""" f_print_header("Test: nlevels") idx = pandasCore.Index([1, 2, 3]) ctx.assert_equal(1, idx.nlevels, "nlevels") def f_main(): """Main test function""" f_print_header("Index Phase 1 & 2: Core, Set Operations, Reshaping, Conversion Tests") # Run all tests test_functions = [ # Phase 1: Core & Set Operations test_constructor_and_properties, .. _example-index-shape-8: .. dropdown:: shape (test_index.py:46) :class-title: example-dropdown .. code-block:: python :linenos: :lineno-start: 36 :emphasize-lines: 11 # Basic construction idx = pandasCore.Index([1, 2, 3, 4, 5], name="test_idx") ctx.assert_equal(5, idx.size, "size") ctx.assert_equal("test_idx", idx.name, "name") ctx.assert_equal("int64", idx.dtype, "dtype") ctx.assert_false(idx.empty, "empty") ctx.assert_true(idx.is_unique, "is_unique") ctx.assert_true(idx.is_monotonic_increasing, "is_monotonic_increasing") ctx.assert_false(idx.is_monotonic_decreasing, "is_monotonic_decreasing") ctx.assert_equal(1, idx.ndim, "ndim") ctx.assert_equal((5,), idx.shape, "shape") # Duplicate values idx_dup = pandasCore.Index([1, 2, 2, 3, 3, 3]) ctx.assert_false(idx_dup.is_unique, "is_unique with dups") ctx.assert_true(idx_dup.has_duplicates, "has_duplicates") def test_copy(): """Test copy method""" f_print_header("Test: copy") .. _example-index-size-9: .. dropdown:: size (test_example.py:164) :class-title: example-dropdown .. code-block:: python :linenos: :lineno-start: 154 :emphasize-lines: 11 """Test Index creation from list""" f_print_header("Test: Index Creation") try: # Create Index from list data = [10, 20, 30, 40, 50] idx = pandasCore.Index(data, name="test_index") f_print_success("Created Index from list") # Check size if idx.size == 5: f_print_success(f"Size is correct: {idx.size}") else: f_print_error(f"Size is wrong: {idx.size}, expected 5") return False # Check name if idx.name == "test_index": f_print_success(f"Name is correct: {idx.name}") else: f_print_error(f"Name is wrong: {idx.name}, expected 'test_index'") .. _example-index-str-10: .. dropdown:: str (test_index_str_accessor.py:52) :class-title: example-dropdown .. code-block:: python :linenos: :lineno-start: 42 :emphasize-lines: 11 def test_string_index_str_accessor(): """Test StringIndex.str accessor works.""" print("Testing StringIndex.str accessor...") # Create a StringIndex idx = pandasCore.StringIndex(['hello', 'world', 'test']) print(f" Created StringIndex: {idx}") # Access .str str_methods = idx.str print(f" idx.str type: {type(str_methods)}") # Test upper() result = str_methods.upper() print(f" idx.str.upper() result: {result}") # Verify result result_list = result.tolist() expected = ['HELLO', 'WORLD', 'TEST'] assert result_list == expected, f"Expected {expected}, got {result_list}" .. _example-index-dunder-initdunder--11: .. dropdown:: __init__ (test_example.py:160) :class-title: example-dropdown .. code-block:: python :linenos: :lineno-start: 150 :emphasize-lines: 11 return False def test_index_creation(): """Test Index creation from list""" f_print_header("Test: Index Creation") try: # Create Index from list data = [10, 20, 30, 40, 50] idx = pandasCore.Index(data, name="test_index") f_print_success("Created Index from list") # Check size if idx.size == 5: f_print_success(f"Size is correct: {idx.size}") else: f_print_error(f"Size is wrong: {idx.size}, expected 5") return False # Check name .. _example-index-take-12: .. dropdown:: take (test_index.py:405) :class-title: example-dropdown .. code-block:: python :linenos: :lineno-start: 395 :emphasize-lines: 11 ctx.assert_equal(1, start, "slice_locs start") ctx.assert_equal(4, stop, "slice_locs stop") def test_take(): """Test take method""" f_print_header("Test: take") idx = pandasCore.Index([10, 20, 30, 40]) result = idx.take([0, 2]) ctx.assert_list_equal([10, 30], result.tolist(), "take") def test_insert(): """Test insert method""" f_print_header("Test: insert") idx = pandasCore.Index([1, 2, 4]) result = idx.insert(2, 3) .. _example-index-drop-13: .. dropdown:: drop (test_index.py:495) :class-title: example-dropdown .. code-block:: python :linenos: :lineno-start: 485 :emphasize-lines: 11 ctx.assert_list_equal([1, 2, 3], result, "to_list") # ==================== Phase 2: Reshaping, Conversion & Testing ==================== def test_drop(): """Test drop method""" f_print_header("Test: drop") idx = pandasCore.Index([1, 2, 3, 4, 5]) result = idx.drop([2, 4]) ctx.assert_list_equal([1, 3, 5], result.tolist(), "drop") def test_get_level_values(): """Test get_level_values method""" f_print_header("Test: get_level_values") idx = pandasCore.Index([1, 2, 3], name="level0") result = idx.get_level_values(0) .. _example-index-insert-14: .. dropdown:: insert (test_index.py:415) :class-title: example-dropdown .. code-block:: python :linenos: :lineno-start: 405 :emphasize-lines: 11 result = idx.take([0, 2]) ctx.assert_list_equal([10, 30], result.tolist(), "take") def test_insert(): """Test insert method""" f_print_header("Test: insert") idx = pandasCore.Index([1, 2, 4]) result = idx.insert(2, 3) ctx.assert_list_equal([1, 2, 3, 4], result.tolist(), "insert") def test_delete(): """Test delete method""" f_print_header("Test: delete") idx = pandasCore.Index([1, 2, 3, 4]) result = idx.delete(1) .. _example-index-reindex-15: .. dropdown:: reindex (test_index.py:516) :class-title: example-dropdown .. code-block:: python :linenos: :lineno-start: 506 :emphasize-lines: 11 ctx.assert_list_equal([1, 2, 3], result.tolist(), "get_level_values") def test_reindex(): """Test reindex method""" f_print_header("Test: reindex") idx = pandasCore.Index([1, 2, 3]) target = pandasCore.Index([1, 3, 5]) new_idx, indexer = idx.reindex(target) ctx.assert_list_equal([1, 3, 5], new_idx.tolist(), "reindex new_idx") ctx.assert_list_equal([0, 2, -1], indexer, "reindex indexer") def test_sortlevel(): """Test sortlevel method""" f_print_header("Test: sortlevel") idx = pandasCore.Index([3, 1, 2]) .. _example-index-rename-16: .. dropdown:: rename (test_index.py:70) :class-title: example-dropdown .. code-block:: python :linenos: :lineno-start: 60 :emphasize-lines: 11 ctx.assert_list_equal(idx.tolist(), idx_copy.tolist(), "copy values") ctx.assert_equal(idx.name, idx_copy.name, "copy name") def test_rename(): """Test rename method""" f_print_header("Test: rename") idx = pandasCore.Index([1, 2, 3], name="old_name") idx_new = idx.rename("new_name") ctx.assert_equal("new_name", idx_new.name, "rename") ctx.assert_equal("old_name", idx.name, "original unchanged") def test_set_names(): """Test set_names method""" f_print_header("Test: set_names") idx = pandasCore.Index([1, 2, 3], name="old") .. _example-index-set_names-17: .. dropdown:: set_names (test_index.py:81) :class-title: example-dropdown .. code-block:: python :linenos: :lineno-start: 71 :emphasize-lines: 11 ctx.assert_equal("new_name", idx_new.name, "rename") ctx.assert_equal("old_name", idx.name, "original unchanged") def test_set_names(): """Test set_names method""" f_print_header("Test: set_names") idx = pandasCore.Index([1, 2, 3], name="old") idx_new = idx.set_names("new") ctx.assert_equal("new", idx_new.name, "set_names") def test_map(): """Test map method""" f_print_header("Test: map") idx = pandasCore.Index([1, 2, 3]) idx_mapped = idx.map(lambda x: x * 2) .. _example-index-dropna-18: .. dropdown:: dropna (test_index.py:111) :class-title: example-dropdown .. code-block:: python :linenos: :lineno-start: 101 :emphasize-lines: 11 idx_filled = idx.fillna(0) ctx.assert_list_equal([1, 2, 3], idx_filled.tolist(), "fillna (int64)") def test_dropna(): """Test dropna method (for int64, essentially a no-op)""" f_print_header("Test: dropna") idx = pandasCore.Index([1, 2, 3]) idx_dropped = idx.dropna() ctx.assert_list_equal([1, 2, 3], idx_dropped.tolist(), "dropna (int64)") def test_isna_notna(): """Test isna, notna, isnull, notnull methods""" f_print_header("Test: isna/notna") idx = pandasCore.Index([1, 2, 3]) .. _example-index-fillna-19: .. dropdown:: fillna (test_index.py:101) :class-title: example-dropdown .. code-block:: python :linenos: :lineno-start: 91 :emphasize-lines: 11 idx_mapped = idx.map(lambda x: x * 2) ctx.assert_list_equal([2, 4, 6], idx_mapped.tolist(), "map") def test_fillna(): """Test fillna method (for int64, essentially a no-op)""" f_print_header("Test: fillna") idx = pandasCore.Index([1, 2, 3]) idx_filled = idx.fillna(0) ctx.assert_list_equal([1, 2, 3], idx_filled.tolist(), "fillna (int64)") def test_dropna(): """Test dropna method (for int64, essentially a no-op)""" f_print_header("Test: dropna") idx = pandasCore.Index([1, 2, 3]) idx_dropped = idx.dropna() .. _example-index-isna-20: .. dropdown:: isna (test_index.py:123) :class-title: example-dropdown .. code-block:: python :linenos: :lineno-start: 113 :emphasize-lines: 11 ctx.assert_list_equal([1, 2, 3], idx_dropped.tolist(), "dropna (int64)") def test_isna_notna(): """Test isna, notna, isnull, notnull methods""" f_print_header("Test: isna/notna") idx = pandasCore.Index([1, 2, 3]) # For int64, isna should return all False isna_result = list(idx.isna()) ctx.assert_list_equal([False, False, False], isna_result, "isna (int64)") notna_result = list(idx.notna()) ctx.assert_list_equal([True, True, True], notna_result, "notna (int64)") # Test aliases isnull_result = list(idx.isnull()) ctx.assert_list_equal([False, False, False], isnull_result, "isnull (int64)") notnull_result = list(idx.notnull()) .. _example-index-isnull-21: .. dropdown:: isnull (test_index.py:130) :class-title: example-dropdown .. code-block:: python :linenos: :lineno-start: 120 :emphasize-lines: 11 idx = pandasCore.Index([1, 2, 3]) # For int64, isna should return all False isna_result = list(idx.isna()) ctx.assert_list_equal([False, False, False], isna_result, "isna (int64)") notna_result = list(idx.notna()) ctx.assert_list_equal([True, True, True], notna_result, "notna (int64)") # Test aliases isnull_result = list(idx.isnull()) ctx.assert_list_equal([False, False, False], isnull_result, "isnull (int64)") notnull_result = list(idx.notnull()) ctx.assert_list_equal([True, True, True], notnull_result, "notnull (int64)") def test_drop_duplicates(): """Test drop_duplicates method""" f_print_header("Test: drop_duplicates") .. _example-index-notna-22: .. dropdown:: notna (test_index.py:126) :class-title: example-dropdown .. code-block:: python :linenos: :lineno-start: 116 :emphasize-lines: 11 def test_isna_notna(): """Test isna, notna, isnull, notnull methods""" f_print_header("Test: isna/notna") idx = pandasCore.Index([1, 2, 3]) # For int64, isna should return all False isna_result = list(idx.isna()) ctx.assert_list_equal([False, False, False], isna_result, "isna (int64)") notna_result = list(idx.notna()) ctx.assert_list_equal([True, True, True], notna_result, "notna (int64)") # Test aliases isnull_result = list(idx.isnull()) ctx.assert_list_equal([False, False, False], isnull_result, "isnull (int64)") notnull_result = list(idx.notnull()) ctx.assert_list_equal([True, True, True], notnull_result, "notnull (int64)") .. _example-index-notnull-23: .. dropdown:: notnull (test_index.py:133) :class-title: example-dropdown .. code-block:: python :linenos: :lineno-start: 123 :emphasize-lines: 11 isna_result = list(idx.isna()) ctx.assert_list_equal([False, False, False], isna_result, "isna (int64)") notna_result = list(idx.notna()) ctx.assert_list_equal([True, True, True], notna_result, "notna (int64)") # Test aliases isnull_result = list(idx.isnull()) ctx.assert_list_equal([False, False, False], isnull_result, "isnull (int64)") notnull_result = list(idx.notnull()) ctx.assert_list_equal([True, True, True], notnull_result, "notnull (int64)") def test_drop_duplicates(): """Test drop_duplicates method""" f_print_header("Test: drop_duplicates") idx = pandasCore.Index([1, 2, 2, 3, 3, 3]) # keep='first' (default) .. _example-index-max-24: .. dropdown:: max (test_index.py:214) :class-title: example-dropdown .. code-block:: python :linenos: :lineno-start: 204 :emphasize-lines: 11 ctx.assert_equal(3, uniques.size, "factorize uniques size") def test_min_max(): """Test min and max methods""" f_print_header("Test: min/max") idx = pandasCore.Index([3, 1, 4, 1, 5, 9, 2, 6]) ctx.assert_equal(1, idx.min(), "min") ctx.assert_equal(9, idx.max(), "max") def test_argmin_argmax(): """Test argmin and argmax methods""" f_print_header("Test: argmin/argmax") idx = pandasCore.Index([3, 1, 4, 1, 5, 9, 2, 6]) ctx.assert_equal(1, idx.argmin(), "argmin") # First occurrence of min (1) is at index 1 ctx.assert_equal(5, idx.argmax(), "argmax") # Max (9) is at index 5 .. _example-index-min-25: .. dropdown:: min (test_index.py:213) :class-title: example-dropdown .. code-block:: python :linenos: :lineno-start: 203 :emphasize-lines: 11 # Verify uniques ctx.assert_equal(3, uniques.size, "factorize uniques size") def test_min_max(): """Test min and max methods""" f_print_header("Test: min/max") idx = pandasCore.Index([3, 1, 4, 1, 5, 9, 2, 6]) ctx.assert_equal(1, idx.min(), "min") ctx.assert_equal(9, idx.max(), "max") def test_argmin_argmax(): """Test argmin and argmax methods""" f_print_header("Test: argmin/argmax") idx = pandasCore.Index([3, 1, 4, 1, 5, 9, 2, 6]) ctx.assert_equal(1, idx.argmin(), "argmin") # First occurrence of min (1) is at index 1 .. _example-index-nunique-26: .. dropdown:: nunique (test_index.py:178) :class-title: example-dropdown .. code-block:: python :linenos: :lineno-start: 168 :emphasize-lines: 11 ctx.assert_list_equal([1, 2, 3], result.tolist(), "unique") def test_nunique(): """Test nunique method""" f_print_header("Test: nunique") idx = pandasCore.Index([1, 2, 2, 3, 3, 3]) ctx.assert_equal(3, idx.nunique(), "nunique") def test_value_counts(): """Test value_counts method""" f_print_header("Test: value_counts") idx = pandasCore.Index([1, 2, 2, 3, 3, 3]) result = idx.value_counts() # Should return dict {3: 3, 2: 2, 1: 1} (sorted by count descending) .. _example-index-value_counts-27: .. dropdown:: value_counts (test_index.py:186) :class-title: example-dropdown .. code-block:: python :linenos: :lineno-start: 176 :emphasize-lines: 11 idx = pandasCore.Index([1, 2, 2, 3, 3, 3]) ctx.assert_equal(3, idx.nunique(), "nunique") def test_value_counts(): """Test value_counts method""" f_print_header("Test: value_counts") idx = pandasCore.Index([1, 2, 2, 3, 3, 3]) result = idx.value_counts() # Should return dict {3: 3, 2: 2, 1: 1} (sorted by count descending) ctx.assert_equal(3, result[3], "value_counts 3") ctx.assert_equal(2, result[2], "value_counts 2") ctx.assert_equal(1, result[1], "value_counts 1") def test_factorize(): """Test factorize method""" f_print_header("Test: factorize") .. _example-index-groupby-28: .. dropdown:: groupby (test_index.py:593) :class-title: example-dropdown .. code-block:: python :linenos: :lineno-start: 583 :emphasize-lines: 11 idx_some_nonzero = pandasCore.Index([0, 1, 0]) ctx.assert_true(idx_some_nonzero.any(), "any True") def test_groupby(): """Test groupby method""" f_print_header("Test: groupby") idx = pandasCore.Index([10, 20, 30, 40]) groups = idx.groupby([1, 1, 2, 2]) ctx.assert_list_equal([0, 1], groups[1], "groupby group 1") ctx.assert_list_equal([2, 3], groups[2], "groupby group 2") def test_memory_usage(): """Test memory_usage method""" f_print_header("Test: memory_usage") idx = pandasCore.Index([1, 2, 3]) .. _example-index-map-29: .. dropdown:: map (test_index.py:91) :class-title: example-dropdown .. code-block:: python :linenos: :lineno-start: 81 :emphasize-lines: 11 idx_new = idx.set_names("new") ctx.assert_equal("new", idx_new.name, "set_names") def test_map(): """Test map method""" f_print_header("Test: map") idx = pandasCore.Index([1, 2, 3]) idx_mapped = idx.map(lambda x: x * 2) ctx.assert_list_equal([2, 4, 6], idx_mapped.tolist(), "map") def test_fillna(): """Test fillna method (for int64, essentially a no-op)""" f_print_header("Test: fillna") idx = pandasCore.Index([1, 2, 3]) idx_filled = idx.fillna(0) .. _example-index-argsort-30: .. dropdown:: argsort (test_index.py:232) :class-title: example-dropdown .. code-block:: python :linenos: :lineno-start: 222 :emphasize-lines: 11 ctx.assert_equal(1, idx.argmin(), "argmin") # First occurrence of min (1) is at index 1 ctx.assert_equal(5, idx.argmax(), "argmax") # Max (9) is at index 5 def test_argsort(): """Test argsort method""" f_print_header("Test: argsort") idx = pandasCore.Index([3, 1, 2]) result = list(idx.argsort()) ctx.assert_list_equal([1, 2, 0], result, "argsort") def test_searchsorted(): """Test searchsorted method""" f_print_header("Test: searchsorted") idx = pandasCore.Index([1, 2, 3, 4, 5]) .. _example-index-searchsorted-31: .. dropdown:: searchsorted (test_index.py:244) :class-title: example-dropdown .. code-block:: python :linenos: :lineno-start: 234 :emphasize-lines: 11 ctx.assert_list_equal([1, 2, 0], result, "argsort") def test_searchsorted(): """Test searchsorted method""" f_print_header("Test: searchsorted") idx = pandasCore.Index([1, 2, 3, 4, 5]) # Single value returns scalar result = idx.searchsorted(3) ctx.assert_equal(2, result, "searchsorted single") def test_append(): """Test append method""" f_print_header("Test: append") idx1 = pandasCore.Index([1, 2, 3]) idx2 = pandasCore.Index([4, 5]) result = idx1.append(idx2) .. _example-index-sort_values-32: .. dropdown:: sort_values (test_index.py:459) :class-title: example-dropdown .. code-block:: python :linenos: :lineno-start: 449 :emphasize-lines: 11 ctx.assert_true(idx1.identical(idx2), "identical True") ctx.assert_false(idx1.identical(idx3), "identical False (different name)") def test_sort_values(): """Test sort_values method""" f_print_header("Test: sort_values") idx = pandasCore.Index([3, 1, 2]) result = idx.sort_values() ctx.assert_list_equal([1, 2, 3], result.tolist(), "sort_values") # Descending result_desc = idx.sort_values(ascending=False) ctx.assert_list_equal([3, 2, 1], result_desc.tolist(), "sort_values descending") def test_contains(): """Test __contains__ method""" .. _example-index-to_frame-33: .. dropdown:: to_frame (test_index.py:690) :class-title: example-dropdown .. code-block:: python :linenos: :lineno-start: 680 :emphasize-lines: 11 result = idx.item() ctx.assert_equal(42, result, "item") def test_to_frame(): """Test to_frame method""" f_print_header("Test: to_frame") idx = pandasCore.Index([1, 2, 3], name="values") frame_data = idx.to_frame() ctx.assert_equal("values", frame_data["column_name"], "to_frame column_name") ctx.assert_list_equal([1, 2, 3], frame_data["values"], "to_frame values") def test_to_series(): """Test to_series method""" f_print_header("Test: to_series") idx = pandasCore.Index([1, 2, 3], name="values") .. _example-index-transpose-34: .. dropdown:: transpose (test_index.py:722) :class-title: example-dropdown .. code-block:: python :linenos: :lineno-start: 712 :emphasize-lines: 11 result = idx.to_flat_index() ctx.assert_list_equal([1, 2, 3], result.tolist(), "to_flat_index") def test_transpose(): """Test transpose method""" f_print_header("Test: transpose") idx = pandasCore.Index([1, 2, 3]) result = idx.transpose() ctx.assert_list_equal([1, 2, 3], result.tolist(), "transpose") def test_repeat(): """Test repeat method""" f_print_header("Test: repeat") idx = pandasCore.Index([1, 2, 3]) result = idx.repeat(2) .. _example-index-asof-35: .. dropdown:: asof (test_index.py:616) :class-title: example-dropdown .. code-block:: python :linenos: :lineno-start: 606 :emphasize-lines: 11 ctx.assert_true(mem > 0, "memory_usage positive") def test_asof(): """Test asof method""" f_print_header("Test: asof") idx = pandasCore.Index([1, 3, 5, 7]) # Exact match result = idx.asof(3) ctx.assert_equal(3, result, "asof exact") # Lookup between values result = idx.asof(4) ctx.assert_equal(3, result, "asof between") def test_asof_locs(): """Test asof_locs method""" f_print_header("Test: asof_locs") .. _example-index-diff-36: .. dropdown:: diff (test_index.py:561) :class-title: example-dropdown .. code-block:: python :linenos: :lineno-start: 551 :emphasize-lines: 11 result = idx.round(2) ctx.assert_list_equal([1, 2, 3], result.tolist(), "round (int64 no-op)") def test_diff(): """Test diff method""" f_print_header("Test: diff") idx = pandasCore.Index([1, 3, 6, 10]) result = idx.diff() ctx.assert_list_equal([0, 2, 3, 4], result.tolist(), "diff") def test_all(): """Test all method""" f_print_header("Test: all") idx_all_true = pandasCore.Index([1, 2, 3]) ctx.assert_true(idx_all_true.all(), "all True") .. _example-index-shift-37: .. dropdown:: shift (test_index.py:538) :class-title: example-dropdown .. code-block:: python :linenos: :lineno-start: 528 :emphasize-lines: 11 ctx.assert_list_equal([1, 2, 3], sorted_idx.tolist(), "sortlevel sorted") ctx.assert_list_equal([1, 2, 0], indexer, "sortlevel indexer") def test_shift(): """Test shift method""" f_print_header("Test: shift") idx = pandasCore.Index([1, 2, 3, 4]) result = idx.shift(2) ctx.assert_list_equal([0, 0, 1, 2], result.tolist(), "shift positive") result_neg = idx.shift(-1) ctx.assert_list_equal([2, 3, 4, 0], result_neg.tolist(), "shift negative") def test_round(): """Test round method (no-op for int64)""" f_print_header("Test: round") .. _example-index-to_flat_index-38: .. dropdown:: to_flat_index (test_index.py:712) :class-title: example-dropdown .. code-block:: python :linenos: :lineno-start: 702 :emphasize-lines: 11 ctx.assert_list_equal([1, 2, 3], series_data["values"], "to_series values") ctx.assert_equal("values", series_data["name"], "to_series name") def test_to_flat_index(): """Test to_flat_index method""" f_print_header("Test: to_flat_index") idx = pandasCore.Index([1, 2, 3]) result = idx.to_flat_index() ctx.assert_list_equal([1, 2, 3], result.tolist(), "to_flat_index") def test_transpose(): """Test transpose method""" f_print_header("Test: transpose") idx = pandasCore.Index([1, 2, 3]) result = idx.transpose() .. _example-index-to_list-39: .. dropdown:: to_list (test_index.py:483) :class-title: example-dropdown .. code-block:: python :linenos: :lineno-start: 473 :emphasize-lines: 11 ctx.assert_true(2 in idx, "__contains__ True") ctx.assert_false(5 in idx, "__contains__ False") def test_to_list(): """Test to_list method""" f_print_header("Test: to_list") idx = pandasCore.Index([1, 2, 3]) result = idx.to_list() ctx.assert_list_equal([1, 2, 3], result, "to_list") # ==================== Phase 2: Reshaping, Conversion & Testing ==================== def test_drop(): """Test drop method""" f_print_header("Test: drop") .. _example-index-to_series-40: .. dropdown:: to_series (test_index.py:701) :class-title: example-dropdown .. code-block:: python :linenos: :lineno-start: 691 :emphasize-lines: 11 ctx.assert_equal("values", frame_data["column_name"], "to_frame column_name") ctx.assert_list_equal([1, 2, 3], frame_data["values"], "to_frame values") def test_to_series(): """Test to_series method""" f_print_header("Test: to_series") idx = pandasCore.Index([1, 2, 3], name="values") series_data = idx.to_series() ctx.assert_list_equal([1, 2, 3], series_data["values"], "to_series values") ctx.assert_equal("values", series_data["name"], "to_series name") def test_to_flat_index(): """Test to_flat_index method""" f_print_header("Test: to_flat_index") idx = pandasCore.Index([1, 2, 3]) .. _example-index-tolist-41: .. dropdown:: tolist (test_index.py:61) :class-title: example-dropdown .. code-block:: python :linenos: :lineno-start: 51 :emphasize-lines: 11 ctx.assert_true(idx_dup.has_duplicates, "has_duplicates") def test_copy(): """Test copy method""" f_print_header("Test: copy") idx = pandasCore.Index([1, 2, 3], name="original") idx_copy = idx.copy() ctx.assert_list_equal(idx.tolist(), idx_copy.tolist(), "copy values") ctx.assert_equal(idx.name, idx_copy.name, "copy name") def test_rename(): """Test rename method""" f_print_header("Test: rename") idx = pandasCore.Index([1, 2, 3], name="old_name") idx_new = idx.rename("new_name") .. _example-index-copy-42: .. dropdown:: copy (test_index.py:59) :class-title: example-dropdown .. code-block:: python :linenos: :lineno-start: 49 :emphasize-lines: 11 idx_dup = pandasCore.Index([1, 2, 2, 3, 3, 3]) ctx.assert_false(idx_dup.is_unique, "is_unique with dups") ctx.assert_true(idx_dup.has_duplicates, "has_duplicates") def test_copy(): """Test copy method""" f_print_header("Test: copy") idx = pandasCore.Index([1, 2, 3], name="original") idx_copy = idx.copy() ctx.assert_list_equal(idx.tolist(), idx_copy.tolist(), "copy values") ctx.assert_equal(idx.name, idx_copy.name, "copy name") def test_rename(): """Test rename method""" f_print_header("Test: rename") idx = pandasCore.Index([1, 2, 3], name="old_name") .. _example-index-infer_objects-43: .. dropdown:: infer_objects (test_index.py:762) :class-title: example-dropdown .. code-block:: python :linenos: :lineno-start: 752 :emphasize-lines: 11 result = idx.putmask([False, True, False, True], 0) ctx.assert_list_equal([1, 0, 3, 0], result.tolist(), "putmask") def test_infer_objects(): """Test infer_objects method""" f_print_header("Test: infer_objects") idx = pandasCore.Index([1, 2, 3]) result = idx.infer_objects() ctx.assert_list_equal([1, 2, 3], result.tolist(), "infer_objects") def test_nlevels(): """Test nlevels property""" f_print_header("Test: nlevels") idx = pandasCore.Index([1, 2, 3]) .. _example-index-view-44: .. dropdown:: view (test_index_methods.py:57) :class-title: example-dropdown .. code-block:: python :linenos: :lineno-start: 47 :emphasize-lines: 11 tests_run += 1 idx = pandasCore.PeriodIndex(['2023-03', '2023-01', '2023-02'], freq='M') result = idx.sort() assert len(result) == 3, f"Expected 3 elements, got {len(result)}" def test_periodindex_view(): """Test PeriodIndex.view""" global tests_run tests_run += 1 idx = pandasCore.PeriodIndex(['2023-01', '2023-02'], freq='M') result = idx.view() assert len(result) == 2, f"Expected 2 elements, got {len(result)}" def test_intervalindex_infer_objects(): """Test IntervalIndex.infer_objects""" global tests_run tests_run += 1 idx = pandasCore.IntervalIndex.from_breaks([0, 1, 2, 3]) result = idx.infer_objects() assert len(result) == 3, f"Expected 3 elements, got {len(result)}" .. _example-index-drop_duplicates-45: .. dropdown:: drop_duplicates (test_index.py:144) :class-title: example-dropdown .. code-block:: python :linenos: :lineno-start: 134 :emphasize-lines: 11 ctx.assert_list_equal([True, True, True], notnull_result, "notnull (int64)") def test_drop_duplicates(): """Test drop_duplicates method""" f_print_header("Test: drop_duplicates") idx = pandasCore.Index([1, 2, 2, 3, 3, 3]) # keep='first' (default) result = idx.drop_duplicates() ctx.assert_list_equal([1, 2, 3], result.tolist(), "drop_duplicates (first)") # keep='last' result_last = idx.drop_duplicates(keep="last") ctx.assert_list_equal([1, 2, 3], result_last.tolist(), "drop_duplicates (last)") def test_duplicated(): """Test duplicated method""" f_print_header("Test: duplicated") .. _example-index-duplicated-46: .. dropdown:: duplicated (test_index.py:158) :class-title: example-dropdown .. code-block:: python :linenos: :lineno-start: 148 :emphasize-lines: 11 result_last = idx.drop_duplicates(keep="last") ctx.assert_list_equal([1, 2, 3], result_last.tolist(), "drop_duplicates (last)") def test_duplicated(): """Test duplicated method""" f_print_header("Test: duplicated") idx = pandasCore.Index([1, 2, 2, 3]) result = list(idx.duplicated()) ctx.assert_list_equal([False, False, True, False], result, "duplicated") def test_unique(): """Test unique method""" f_print_header("Test: unique") idx = pandasCore.Index([1, 2, 2, 3, 3, 3]) result = idx.unique() .. _example-index-isin-47: .. dropdown:: isin (test_index.py:331) :class-title: example-dropdown .. code-block:: python :linenos: :lineno-start: 321 :emphasize-lines: 11 result_set = set(result.tolist()) expected_set = {2, 3} ctx.assert_equal(expected_set, result_set, "join (inner)") def test_isin(): """Test isin method""" f_print_header("Test: isin") idx = pandasCore.Index([1, 2, 3, 4]) result = list(idx.isin([2, 4])) ctx.assert_list_equal([False, True, False, True], result, "isin") def test_get_loc(): """Test get_loc method""" f_print_header("Test: get_loc") idx = pandasCore.Index([10, 20, 30, 40]) .. _example-index-unique-48: .. dropdown:: unique (test_index.py:167) :class-title: example-dropdown .. code-block:: python :linenos: :lineno-start: 157 :emphasize-lines: 11 result = list(idx.duplicated()) ctx.assert_list_equal([False, False, True, False], result, "duplicated") def test_unique(): """Test unique method""" f_print_header("Test: unique") idx = pandasCore.Index([1, 2, 2, 3, 3, 3]) result = idx.unique() ctx.assert_list_equal([1, 2, 3], result.tolist(), "unique") def test_nunique(): """Test nunique method""" f_print_header("Test: nunique") idx = pandasCore.Index([1, 2, 2, 3, 3, 3]) .. _example-index-round-49: .. dropdown:: round (test_index.py:551) :class-title: example-dropdown .. code-block:: python :linenos: :lineno-start: 541 :emphasize-lines: 11 result_neg = idx.shift(-1) ctx.assert_list_equal([2, 3, 4, 0], result_neg.tolist(), "shift negative") def test_round(): """Test round method (no-op for int64)""" f_print_header("Test: round") idx = pandasCore.Index([1, 2, 3]) result = idx.round(2) ctx.assert_list_equal([1, 2, 3], result.tolist(), "round (int64 no-op)") def test_diff(): """Test diff method""" f_print_header("Test: diff") idx = pandasCore.Index([1, 3, 6, 10]) result = idx.diff() .. _example-index-argmax-50: .. dropdown:: argmax (test_index.py:224) :class-title: example-dropdown .. code-block:: python :linenos: :lineno-start: 214 :emphasize-lines: 11 ctx.assert_equal(9, idx.max(), "max") def test_argmin_argmax(): """Test argmin and argmax methods""" f_print_header("Test: argmin/argmax") idx = pandasCore.Index([3, 1, 4, 1, 5, 9, 2, 6]) ctx.assert_equal(1, idx.argmin(), "argmin") # First occurrence of min (1) is at index 1 ctx.assert_equal(5, idx.argmax(), "argmax") # Max (9) is at index 5 def test_argsort(): """Test argsort method""" f_print_header("Test: argsort") idx = pandasCore.Index([3, 1, 2]) result = list(idx.argsort()) ctx.assert_list_equal([1, 2, 0], result, "argsort") .. _example-index-argmin-51: .. dropdown:: argmin (test_index.py:223) :class-title: example-dropdown .. code-block:: python :linenos: :lineno-start: 213 :emphasize-lines: 11 ctx.assert_equal(1, idx.min(), "min") ctx.assert_equal(9, idx.max(), "max") def test_argmin_argmax(): """Test argmin and argmax methods""" f_print_header("Test: argmin/argmax") idx = pandasCore.Index([3, 1, 4, 1, 5, 9, 2, 6]) ctx.assert_equal(1, idx.argmin(), "argmin") # First occurrence of min (1) is at index 1 ctx.assert_equal(5, idx.argmax(), "argmax") # Max (9) is at index 5 def test_argsort(): """Test argsort method""" f_print_header("Test: argsort") idx = pandasCore.Index([3, 1, 2]) result = list(idx.argsort()) .. _example-index-asof_locs-52: .. dropdown:: asof_locs (test_index.py:630) :class-title: example-dropdown .. code-block:: python :linenos: :lineno-start: 620 :emphasize-lines: 11 result = idx.asof(4) ctx.assert_equal(3, result, "asof between") def test_asof_locs(): """Test asof_locs method""" f_print_header("Test: asof_locs") idx = pandasCore.Index([1, 3, 5, 7]) where = pandasCore.Index([2, 4, 6]) result = idx.asof_locs(where) ctx.assert_equal(3, len(result), "asof_locs length") def test_get_slice_bound(): """Test get_slice_bound method""" f_print_header("Test: get_slice_bound") idx = pandasCore.Index([1, 2, 3, 4, 5]) .. _example-index-delete-53: .. dropdown:: delete (test_index.py:425) :class-title: example-dropdown .. code-block:: python :linenos: :lineno-start: 415 :emphasize-lines: 11 result = idx.insert(2, 3) ctx.assert_list_equal([1, 2, 3, 4], result.tolist(), "insert") def test_delete(): """Test delete method""" f_print_header("Test: delete") idx = pandasCore.Index([1, 2, 3, 4]) result = idx.delete(1) ctx.assert_list_equal([1, 3, 4], result.tolist(), "delete") def test_equals(): """Test equals method""" f_print_header("Test: equals") idx1 = pandasCore.Index([1, 2, 3]) idx2 = pandasCore.Index([1, 2, 3]) .. _example-index-factorize-54: .. dropdown:: factorize (test_index.py:199) :class-title: example-dropdown .. code-block:: python :linenos: :lineno-start: 189 :emphasize-lines: 11 ctx.assert_equal(3, result[3], "value_counts 3") ctx.assert_equal(2, result[2], "value_counts 2") ctx.assert_equal(1, result[1], "value_counts 1") def test_factorize(): """Test factorize method""" f_print_header("Test: factorize") idx = pandasCore.Index([3, 1, 2, 3, 1]) codes, uniques = idx.factorize() # Verify codes length matches ctx.assert_equal(5, len(list(codes)), "factorize codes length") # Verify uniques ctx.assert_equal(3, uniques.size, "factorize uniques size") def test_min_max(): """Test min and max methods""" f_print_header("Test: min/max") .. _example-index-format-55: .. dropdown:: format (test_index.py:742) :class-title: example-dropdown .. code-block:: python :linenos: :lineno-start: 732 :emphasize-lines: 11 result = idx.repeat(2) ctx.assert_list_equal([1, 1, 2, 2, 3, 3], result.tolist(), "repeat scalar") def test_format(): """Test format method""" f_print_header("Test: format") idx = pandasCore.Index([1, 2, 3]) result = idx.format() ctx.assert_list_equal(["1", "2", "3"], result, "format") def test_putmask(): """Test putmask method""" f_print_header("Test: putmask") idx = pandasCore.Index([1, 2, 3, 4]) result = idx.putmask([False, True, False, True], 0) .. _example-index-get_indexer-56: .. dropdown:: get_indexer (test_index.py:362) :class-title: example-dropdown .. code-block:: python :linenos: :lineno-start: 352 :emphasize-lines: 11 ctx.tests_run += 1 ctx.tests_passed += 1 def test_get_indexer(): """Test get_indexer method""" f_print_header("Test: get_indexer") idx = pandasCore.Index([1, 2, 3]) target = pandasCore.Index([1, 3, 5]) result = list(idx.get_indexer(target)) ctx.assert_list_equal([0, 2, -1], result, "get_indexer") def test_get_indexer_for(): """Test get_indexer_for method""" f_print_header("Test: get_indexer_for") idx = pandasCore.Index([1, 2, 3]) result = list(idx.get_indexer_for([1, 3])) .. _example-index-get_indexer_for-57: .. dropdown:: get_indexer_for (test_index.py:372) :class-title: example-dropdown .. code-block:: python :linenos: :lineno-start: 362 :emphasize-lines: 11 result = list(idx.get_indexer(target)) ctx.assert_list_equal([0, 2, -1], result, "get_indexer") def test_get_indexer_for(): """Test get_indexer_for method""" f_print_header("Test: get_indexer_for") idx = pandasCore.Index([1, 2, 3]) result = list(idx.get_indexer_for([1, 3])) ctx.assert_list_equal([0, 2], result, "get_indexer_for") def test_get_indexer_non_unique(): """Test get_indexer_non_unique method""" f_print_header("Test: get_indexer_non_unique") idx = pandasCore.Index([1, 2, 2, 3]) target = pandasCore.Index([2, 5]) .. _example-index-get_indexer_non_unique-58: .. dropdown:: get_indexer_non_unique (test_index.py:383) :class-title: example-dropdown .. code-block:: python :linenos: :lineno-start: 373 :emphasize-lines: 11 ctx.assert_list_equal([0, 2], result, "get_indexer_for") def test_get_indexer_non_unique(): """Test get_indexer_non_unique method""" f_print_header("Test: get_indexer_non_unique") idx = pandasCore.Index([1, 2, 2, 3]) target = pandasCore.Index([2, 5]) indexer, missing = idx.get_indexer_non_unique(target) # Should find indices for 2 and mark 5 as missing ctx.assert_true(len(list(indexer)) > 0, "get_indexer_non_unique has indexer") def test_slice_locs(): """Test slice_locs method""" f_print_header("Test: slice_locs") idx = pandasCore.Index([1, 2, 3, 4, 5]) .. _example-index-get_level_values-59: .. dropdown:: get_level_values (test_index.py:505) :class-title: example-dropdown .. code-block:: python :linenos: :lineno-start: 495 :emphasize-lines: 11 result = idx.drop([2, 4]) ctx.assert_list_equal([1, 3, 5], result.tolist(), "drop") def test_get_level_values(): """Test get_level_values method""" f_print_header("Test: get_level_values") idx = pandasCore.Index([1, 2, 3], name="level0") result = idx.get_level_values(0) ctx.assert_list_equal([1, 2, 3], result.tolist(), "get_level_values") def test_reindex(): """Test reindex method""" f_print_header("Test: reindex") idx = pandasCore.Index([1, 2, 3]) target = pandasCore.Index([1, 3, 5]) .. _example-index-get_loc-60: .. dropdown:: get_loc (test_index.py:342) :class-title: example-dropdown .. code-block:: python :linenos: :lineno-start: 332 :emphasize-lines: 11 ctx.assert_list_equal([False, True, False, True], result, "isin") def test_get_loc(): """Test get_loc method""" f_print_header("Test: get_loc") idx = pandasCore.Index([10, 20, 30, 40]) ctx.assert_equal(1, idx.get_loc(20), "get_loc") # Test KeyError for missing key try: idx.get_loc(99) f_print_error("get_loc should raise KeyError for missing key") ctx.tests_run += 1 ctx.tests_failed += 1 except KeyError: f_print_success("get_loc raises KeyError for missing key: PASS") ctx.tests_run += 1 .. _example-index-get_slice_bound-61: .. dropdown:: get_slice_bound (test_index.py:641) :class-title: example-dropdown .. code-block:: python :linenos: :lineno-start: 631 :emphasize-lines: 11 ctx.assert_equal(3, len(result), "asof_locs length") def test_get_slice_bound(): """Test get_slice_bound method""" f_print_header("Test: get_slice_bound") idx = pandasCore.Index([1, 2, 3, 4, 5]) left_bound = idx.get_slice_bound(3, "left") ctx.assert_equal(2, left_bound, "get_slice_bound left") right_bound = idx.get_slice_bound(3, "right") ctx.assert_equal(3, right_bound, "get_slice_bound right") def test_type_checks(): """Test type checking methods""" f_print_header("Test: type checking methods") .. _example-index-holds_integer-62: .. dropdown:: holds_integer (test_index.py:654) :class-title: example-dropdown .. code-block:: python :linenos: :lineno-start: 644 :emphasize-lines: 11 right_bound = idx.get_slice_bound(3, "right") ctx.assert_equal(3, right_bound, "get_slice_bound right") def test_type_checks(): """Test type checking methods""" f_print_header("Test: type checking methods") idx = pandasCore.Index([1, 2, 3]) ctx.assert_true(idx.holds_integer(), "holds_integer") ctx.assert_true(idx.is_integer(), "is_integer") ctx.assert_true(idx.is_numeric(), "is_numeric") ctx.assert_false(idx.is_boolean(), "is_boolean") ctx.assert_false(idx.is_floating(), "is_floating") ctx.assert_false(idx.is_categorical(), "is_categorical") ctx.assert_false(idx.is_interval(), "is_interval") ctx.assert_false(idx.is_object(), "is_object") def test_is_(): .. _example-index-item-63: .. dropdown:: item (test_index.py:680) :class-title: example-dropdown .. code-block:: python :linenos: :lineno-start: 670 :emphasize-lines: 11 # Different objects ctx.assert_false(idx1.is_(idx2), "is_ different objects") def test_item(): """Test item method""" f_print_header("Test: item") idx = pandasCore.Index([42]) result = idx.item() ctx.assert_equal(42, result, "item") def test_to_frame(): """Test to_frame method""" f_print_header("Test: to_frame") idx = pandasCore.Index([1, 2, 3], name="values") frame_data = idx.to_frame() .. _example-index-memory_usage-64: .. dropdown:: memory_usage (test_index.py:604) :class-title: example-dropdown .. code-block:: python :linenos: :lineno-start: 594 :emphasize-lines: 11 ctx.assert_list_equal([0, 1], groups[1], "groupby group 1") ctx.assert_list_equal([2, 3], groups[2], "groupby group 2") def test_memory_usage(): """Test memory_usage method""" f_print_header("Test: memory_usage") idx = pandasCore.Index([1, 2, 3]) mem = idx.memory_usage() ctx.assert_true(mem > 0, "memory_usage positive") def test_asof(): """Test asof method""" f_print_header("Test: asof") idx = pandasCore.Index([1, 3, 5, 7]) .. _example-index-putmask-65: .. dropdown:: putmask (test_index.py:752) :class-title: example-dropdown .. code-block:: python :linenos: :lineno-start: 742 :emphasize-lines: 11 result = idx.format() ctx.assert_list_equal(["1", "2", "3"], result, "format") def test_putmask(): """Test putmask method""" f_print_header("Test: putmask") idx = pandasCore.Index([1, 2, 3, 4]) result = idx.putmask([False, True, False, True], 0) ctx.assert_list_equal([1, 0, 3, 0], result.tolist(), "putmask") def test_infer_objects(): """Test infer_objects method""" f_print_header("Test: infer_objects") idx = pandasCore.Index([1, 2, 3]) result = idx.infer_objects() .. _example-index-repeat-66: .. dropdown:: repeat (test_index.py:732) :class-title: example-dropdown .. code-block:: python :linenos: :lineno-start: 722 :emphasize-lines: 11 result = idx.transpose() ctx.assert_list_equal([1, 2, 3], result.tolist(), "transpose") def test_repeat(): """Test repeat method""" f_print_header("Test: repeat") idx = pandasCore.Index([1, 2, 3]) result = idx.repeat(2) ctx.assert_list_equal([1, 1, 2, 2, 3, 3], result.tolist(), "repeat scalar") def test_format(): """Test format method""" f_print_header("Test: format") idx = pandasCore.Index([1, 2, 3]) result = idx.format() .. _example-index-slice_locs-67: .. dropdown:: slice_locs (test_index.py:394) :class-title: example-dropdown .. code-block:: python :linenos: :lineno-start: 384 :emphasize-lines: 11 # Should find indices for 2 and mark 5 as missing ctx.assert_true(len(list(indexer)) > 0, "get_indexer_non_unique has indexer") def test_slice_locs(): """Test slice_locs method""" f_print_header("Test: slice_locs") idx = pandasCore.Index([1, 2, 3, 4, 5]) start, stop = idx.slice_locs(2, 4) ctx.assert_equal(1, start, "slice_locs start") ctx.assert_equal(4, stop, "slice_locs stop") def test_take(): """Test take method""" f_print_header("Test: take") idx = pandasCore.Index([10, 20, 30, 40]) .. _example-index-sort-68: .. dropdown:: sort (test_index_methods.py:16) :class-title: example-dropdown .. code-block:: python :linenos: :lineno-start: 6 :emphasize-lines: 11 import pandasCore tests_run = 0 def test_index_sort(): """Test Index.sort""" global tests_run tests_run += 1 idx = pandasCore.Index([3, 1, 2]) result = idx.sort() assert len(result) == 3, f"Expected 3 elements, got {len(result)}" def test_timedeltaindex_sortlevel(): """Test TimedeltaIndex.sortlevel""" global tests_run tests_run += 1 idx = pandasCore.TimedeltaIndex(['1 day', '2 days', '1 hour']) result = idx.sortlevel() # Returns tuple of (sorted_index, indexer) assert len(result) == 2, f"Expected tuple of 2, got {len(result)}" .. _example-index-sortlevel-69: .. dropdown:: sortlevel (test_index.py:527) :class-title: example-dropdown .. code-block:: python :linenos: :lineno-start: 517 :emphasize-lines: 11 ctx.assert_list_equal([1, 3, 5], new_idx.tolist(), "reindex new_idx") ctx.assert_list_equal([0, 2, -1], indexer, "reindex indexer") def test_sortlevel(): """Test sortlevel method""" f_print_header("Test: sortlevel") idx = pandasCore.Index([3, 1, 2]) sorted_idx, indexer = idx.sortlevel() ctx.assert_list_equal([1, 2, 3], sorted_idx.tolist(), "sortlevel sorted") ctx.assert_list_equal([1, 2, 0], indexer, "sortlevel indexer") def test_shift(): """Test shift method""" f_print_header("Test: shift") idx = pandasCore.Index([1, 2, 3, 4])