PeriodIndex#
- class PeriodIndex#
Immutable ndarray holding ordinal values indicating regular periods in time.
Example#
import pandasCore as pd
# Create PeriodIndex
idx = pd.PeriodIndex([1, 2, 3], name='my_index')
print(len(idx)) # 3
Attributes#
Attribute |
Description |
Example |
|---|---|---|
|
The day of the period |
|
|
The day of the week (0=Monday, 6=Sunday) |
|
|
The day of the year |
|
|
The day of the week (0=Monday, 6=Sunday) |
|
|
The day of the year |
|
|
The number of days in the month |
|
|
Return the dtype string for this PeriodIndex (e.g., ‘period[D]’) |
|
|
Indicator whether PeriodIndex is empty |
|
|
Get the Timestamp for the end of the period |
|
|
Return the frequency string |
|
|
Return the frequency string for this PeriodIndex |
|
|
Return True if there are duplicate values |
|
|
The hour of the period |
|
|
Return a string of the type inferred from the values |
|
|
Return True if the index contains a full sequence of consecutive pe… |
|
|
Indicator for whether the period is in a leap year |
|
|
Return if the index is monotonic decreasing |
|
|
Return if the index is monotonic increasing |
|
|
Indicator for whether the period is at the end of the month |
|
|
Indicator for whether the period is at the start of the month |
|
|
Indicator for whether the period is at the end of a quarter |
|
|
Indicator for whether the period is at the start of a quarter |
|
|
Return if the index has unique values |
|
|
Indicator for whether the period is at the end of a year |
|
|
Indicator for whether the period is at the start of a year |
|
|
The minute of the period |
|
|
The month of the period |
|
|
Return the name of the PeriodIndex |
|
|
Return the number of dimensions |
|
|
Return the number of levels |
|
|
The quarter of the period |
|
|
The quarter-year of the period |
|
|
The second of the period |
|
|
Return a tuple of the shape of the underlying data |
|
|
Return the number of elements in the PeriodIndex |
|
|
Get the Timestamp for the start of the period |
|
|
Vectorized string functions for Index. |
|
|
The week of the year |
|
|
The week of the year (alias for week) |
|
|
The year of the period |
Construction#
Method |
Description |
Example |
|---|---|---|
|
Immutable ndarray holding ordinal values indicating regular periods… |
Indexing / Selection#
Method |
Description |
Example |
|---|---|---|
|
||
|
Return new PeriodIndex with elements at specified positions |
|
|
Replace values where the condition is False. |
Data Manipulation#
Method |
Description |
Example |
|---|---|---|
|
Make new PeriodIndex with passed list of labels deleted. |
|
|
Return index with requested level(s) removed. |
|
|
Make new PeriodIndex inserting new item at location |
|
|
Conform Index to new index with optional filling logic |
|
|
Alter PeriodIndex name. |
|
|
Set PeriodIndex name |
Missing Data#
Method |
Description |
Example |
|---|---|---|
|
Return PeriodIndex without NA values |
|
|
Fill NA/NaT values with 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 mean of the PeriodIndex values. |
|
|
Return the minimum value of the Index. |
|
|
Return number of unique elements in the index |
|
|
Return a dict containing counts of unique values |
Aggregation#
Method |
Description |
Example |
|---|---|---|
|
Group the index labels by a given array of values |
|
|
Map values using an input mapping or function. |
Comparison#
Method |
Description |
Example |
|---|---|---|
|
||
|
||
|
||
|
||
|
||
|
||
|
Determine if two PeriodIndex objects are equal |
Sorting#
Method |
Description |
Example |
|---|---|---|
|
Return the indices that would sort the index |
|
|
Find indices where elements should be inserted to maintain order |
|
|
Return a sorted copy of the index |
Reshaping#
Method |
Description |
Example |
|---|---|---|
|
Create a DataFrame with a column containing the Index |
|
|
Return the transpose (returns self for 1D) |
Combining#
Method |
Description |
Example |
|---|---|---|
|
Append another PeriodIndex to this one |
|
|
Join two PeriodIndex objects |
Time Series#
Method |
Description |
Example |
|---|---|---|
|
Convert the PeriodIndex to the specified frequency. |
|
|
Return the label from the index, or, if not present, the previous one |
|
|
Compute the difference between consecutive elements |
|
|
Shift index by desired number of periods. |
|
|
Cast to DatetimeArray/Index of timestamps. |
I/O#
Method |
Description |
Example |
|---|---|---|
|
Identity method (PeriodIndex is already 1D) |
|
|
Return a list of Period objects |
|
|
Return the values as a NumPy array. |
|
|
Create a Series with both index and values equal to the index keys |
|
|
Return a list of Period objects |
Conversion#
Method |
Description |
Example |
|---|---|---|
|
Cast to a specified dtype. |
|
|
Make a copy of this PeriodIndex. |
|
|
Attempt to infer better dtype (no-op for PeriodIndex) |
|
|
Return a view on the index |
Iteration#
Method |
Description |
Example |
|---|---|---|
|
||
|
||
|
Set Operations#
Method |
Description |
Example |
|---|---|---|
|
Return a new PeriodIndex with elements not in other |
|
|
Return PeriodIndex with duplicate values removed |
|
|
Indicate duplicate index values |
|
|
Form the intersection of two PeriodIndex objects |
|
|
Check if values are in the PeriodIndex |
|
|
Return the symmetric difference with another PeriodIndex |
|
|
Form the union of two PeriodIndex objects |
|
|
Return unique values in the index |
Datetime Methods#
Method |
Description |
Example |
|---|---|---|
|
Round PeriodIndex values. |
|
|
Convert to string using specified date_format. |
Other Methods#
Method |
Description |
Example |
|---|---|---|
|
||
|
||
|
||
|
Return whether all elements are truthy |
|
|
Return whether any element is truthy |
|
|
Return the index of the maximum value |
|
|
Return the index of the minimum value |
|
|
Return the locations (indices) of labels in the index |
|
|
Make new PeriodIndex with element at position deleted |
|
|
Encode the object as an enumerated type or categorical variable |
|
|
Render a string representation of the Index |
|
|
Compute indexer and mask for new index given the current index |
|
|
Get indexer for target values |
|
|
Get indexer for non-unique targets |
|
|
Return an Index of values for requested level |
|
|
Get integer location for requested label |
|
|
Get slice bound for a given label |
|
|
Check if the index holds integers (always False for PeriodIndex) |
|
|
Determine if two PeriodIndex objects are identical |
|
|
Return the first element as a scalar (only for single-element index) |
|
|
Memory usage of the values |
|
|
Return a new PeriodIndex with values put where mask is True. |
|
|
Return a flattened array of the index values. |
|
|
Repeat elements of a PeriodIndex |
|
|
Compute slice locations for input labels. |
|
|
Compute slice locations for input labels |
|
|
Return a sorted copy of the index |
|
|
Sort MultiIndex at the requested level |
Code Examples#
The following examples are extracted from the test suite.
str (test_timedelta_period_str.py:141)
131def test_periodindex_str_call_with_data():
132 """Test PeriodIndex.str(data) call."""
133 print("Testing PeriodIndex.str(data) call...")
134
135 # Create a PeriodIndex
136 pi = pandasCore.PeriodIndex(['2020-01', '2020-02'], freq='M')
137
138 # Call PeriodIndex.str with the index as argument
139 try:
140 str_accessor = pandasCore.PeriodIndex.str(pi)
141 print(f" PeriodIndex.str(pi) returned: {type(str_accessor)}")
142 print(" PASSED: PeriodIndex.str(data) call works")
143 except Exception as e:
144 # Expected to raise error since PeriodIndex doesn't contain strings
145 print(f" Error (expected for non-string data): {e}")
146 print(" PASSED: Appropriate error raised for non-string Index")
147
148# =============================================================================
149# Main test function
150# =============================================================================
__init__ (test_index_methods.py:48)
38 global tests_run
39 tests_run += 1
40 idx = pandasCore.TimedeltaIndex(['1 day', '2 days'])
41 result = idx.transpose()
42 assert len(result) == 2, f"Expected 2 elements, got {len(result)}"
43
44def test_periodindex_sort():
45 """Test PeriodIndex.sort"""
46 global tests_run
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)}"