TimedeltaIndex#
- class TimedeltaIndex#
Immutable ndarray-like of timedelta64 data.
Example#
import pandasCore as pd
# Create TimedeltaIndex
idx = pd.TimedeltaIndex([1, 2, 3], name='my_index')
print(len(idx)) # 3
Attributes#
Attribute |
Description |
Example |
|---|---|---|
|
Return a DataFrame of the components (days, hours, minutes, etc.) |
|
|
Number of days for each element |
|
|
Return the dtype object of the underlying data |
|
|
Indicator whether TimedeltaIndex is empty |
|
|
Return if the index has duplicate values |
|
|
Return if the index is monotonic decreasing |
|
|
Return if the index is monotonic increasing |
|
|
Return if the index has unique values |
|
|
Number of microseconds (0-999999) for each element |
|
|
Return the name of the TimedeltaIndex |
|
|
Number of nanoseconds (0-999) for each element |
|
|
Number of dimensions of the underlying data |
|
|
Number of levels in this index |
|
|
Number of seconds (0-86399) for each element |
|
|
Return a tuple of the shape of the underlying data |
|
|
Return the number of elements in the TimedeltaIndex |
|
|
Vectorized string functions for Index. |
Construction#
Method |
Description |
Example |
|---|---|---|
|
Create an empty TimedeltaIndex |
Indexing / Selection#
Method |
Description |
Example |
|---|---|---|
|
||
|
Take elements at specified positions. |
|
|
Replace values where the condition is False. |
Data Manipulation#
Method |
Description |
Example |
|---|---|---|
|
Drop specified labels from index |
|
|
Return index with requested level(s) removed. |
|
|
Insert value at position |
|
|
Create index with target’s values (move/add/delete values as necess… |
|
|
Alter TimedeltaIndex name. |
|
|
Set TimedeltaIndex name |
Missing Data#
Method |
Description |
Example |
|---|---|---|
|
Return index with NA values removed. |
|
|
Fill NA/NaT 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. |
|
|
Return the mean of the values. |
|
|
Return the median of the values. |
|
|
Return the minimum value. |
|
|
Return number of unique elements |
|
|
Return standard deviation. |
|
|
Return the sum of the values. |
|
|
Return a dict containing counts of unique values. |
Aggregation#
Method |
Description |
Example |
|---|---|---|
|
Group the index labels by a key function. |
|
|
Map values using an input mapping or function. |
Comparison#
Method |
Description |
Example |
|---|---|---|
|
||
|
||
|
||
|
||
|
||
|
||
|
Check if this index equals another |
Sorting#
Method |
Description |
Example |
|---|---|---|
|
Return indices that would sort the index |
|
|
Find indices where elements should be inserted |
|
|
Sort values |
Reshaping#
Method |
Description |
Example |
|---|---|---|
|
Create a DataFrame with a column containing the Index. |
|
|
Return the transpose |
Combining#
Method |
Description |
Example |
|---|---|---|
|
Append another TimedeltaIndex |
|
|
Join with another index |
Time Series#
Method |
Description |
Example |
|---|---|---|
|
Return the label at or before the given value |
|
|
Compute difference between consecutive elements |
|
|
Shift index by desired number of periods. |
I/O#
Method |
Description |
Example |
|---|---|---|
|
Convert a MultiIndex to an Index of tuples |
|
|
Return a list of the TimedeltaIndex values as nanoseconds |
|
|
A NumPy ndarray representing the values in this Index. |
|
|
Return Timedelta representation for each element. |
|
|
Create a Series with both index and values equal to the index keys. |
|
|
Return a list of the TimedeltaIndex values as nanoseconds |
Conversion#
Method |
Description |
Example |
|---|---|---|
|
Cast to a specified dtype. |
|
|
Make a copy of this TimedeltaIndex. |
|
|
Attempt to infer better dtypes for object columns. |
|
|
Return a view of the index. |
Iteration#
Method |
Description |
Example |
|---|---|---|
|
||
|
||
|
Set Operations#
Method |
Description |
Example |
|---|---|---|
|
Return elements in self but not in other |
|
|
Return index with duplicate values removed. |
|
|
Return boolean array marking duplicates |
|
|
Form intersection with another index |
|
|
Check membership for each value |
|
|
Compute symmetric difference of two Index objects. |
|
|
Form union with another index |
|
|
Return unique values in the index. |
Datetime Methods#
Method |
Description |
Example |
|---|---|---|
|
Perform ceil operation on the data to the specified freq. |
|
|
Perform floor operation on the data to the specified freq. |
|
|
Perform round operation on the data to the specified freq. |
Other Methods#
Method |
Description |
Example |
|---|---|---|
|
||
|
||
|
||
|
Return whether all elements are truthy (non-zero) |
|
|
Return whether any element is truthy (non-zero) |
|
|
Return the position of the maximum value. |
|
|
Return the position of the minimum value. |
|
|
Convert to a new unit resolution. |
|
|
Return the locations (indices) of labels in the index. |
|
|
Delete element at specified position |
|
|
Encode the index as an enumerated type |
|
|
Format index as vector of strings |
|
|
Compute indexer for target index |
|
|
Get indexer for a list of values |
|
|
Compute indexer and mask for new index given the current index. |
|
|
Return an Index of values for requested level. |
|
|
Get integer location for a label |
|
|
Get slice bound for label |
|
|
Check if index holds integer values |
|
|
Check if two indexes are identical (values + name) |
|
|
Return the first element as a scalar |
|
|
Return memory usage in bytes |
|
|
Return a new TimedeltaIndex with values put where mask is True. |
|
|
Return a flattened array of the index values. |
|
|
Repeat elements of the index |
|
|
Slice the index |
|
|
Compute slice indexer for input labels. |
|
|
Compute slice locations for input labels. |
|
|
Return a sorted copy of the index. |
|
|
Sort index by level |
|
|
Return total duration of each element expressed in seconds. |
Code Examples#
The following examples are extracted from the test suite.
str (test_timedelta_period_str.py:76)
66def test_timedeltaindex_str_call_with_data():
67 """Test TimedeltaIndex.str(data) call."""
68 print("Testing TimedeltaIndex.str(data) call...")
69
70 # Create a TimedeltaIndex
71 tdi = pandasCore.TimedeltaIndex(['1 days', '2 days'])
72
73 # Call TimedeltaIndex.str with the index as argument
74 try:
75 str_accessor = pandasCore.TimedeltaIndex.str(tdi)
76 print(f" TimedeltaIndex.str(tdi) returned: {type(str_accessor)}")
77 print(" PASSED: TimedeltaIndex.str(data) call works")
78 except Exception as e:
79 # Expected to raise error since TimedeltaIndex doesn't contain strings
80 print(f" Error (expected for non-string data): {e}")
81 print(" PASSED: Appropriate error raised for non-string Index")
82
83# =============================================================================
84# PeriodIndex.str tests
85# =============================================================================
__init__ (test_index_methods.py:23)
13 global tests_run
14 tests_run += 1
15 idx = pandasCore.Index([3, 1, 2])
16 result = idx.sort()
17 assert len(result) == 3, f"Expected 3 elements, got {len(result)}"
18
19def test_timedeltaindex_sortlevel():
20 """Test TimedeltaIndex.sortlevel"""
21 global tests_run
22 tests_run += 1
23 idx = pandasCore.TimedeltaIndex(['1 day', '2 days', '1 hour'])
24 result = idx.sortlevel()
25 # Returns tuple of (sorted_index, indexer)
26 assert len(result) == 2, f"Expected tuple of 2, got {len(result)}"
27
28def test_timedeltaindex_to_flat_index():
29 """Test TimedeltaIndex.to_flat_index"""
30 global tests_run
31 tests_run += 1
32 idx = pandasCore.TimedeltaIndex(['1 day', '2 days'])
33 result = idx.to_flat_index()