DatetimeIndex#
- class DatetimeIndex#
Immutable ndarray-like of datetime64 data.
Example#
import pandasCore as pd
# Create DatetimeIndex
idx = pd.DatetimeIndex([1, 2, 3], name='my_index')
print(len(idx)) # 3
Attributes#
Attribute |
Description |
Example |
|---|---|---|
|
The day of the datetime |
|
|
The day of the week with Monday=0, Sunday=6 |
|
|
The ordinal day of the year |
|
|
The day of the week with Monday=0, Sunday=6 |
|
|
The ordinal day of the year |
|
|
Return the dtype object of the underlying data |
|
|
Indicator whether DatetimeIndex is empty |
|
|
Return the frequency object |
|
|
Return if the index has duplicate values |
|
|
The hours of the datetime |
|
|
Boolean indicator if the date belongs to a leap year |
|
|
Return if the index is monotonic decreasing |
|
|
Return if the index is monotonic increasing |
|
|
Indicator for whether the date is the last day of the month |
|
|
Indicator for whether the date is the first day of the month |
|
|
Indicator for whether the date is the last day of a quarter |
|
|
Indicator for whether the date is the first day of a quarter |
|
|
Return if the index has unique values |
|
|
Indicator for whether the date is the last day of the year |
|
|
Indicator for whether the date is the first day of the year |
|
|
The microseconds of the datetime |
|
|
The minutes of the datetime |
|
|
The month of the datetime |
|
|
Return the name of the DatetimeIndex |
|
|
The nanoseconds of the datetime |
|
|
Number of dimensions of the underlying data |
|
|
Number of levels in this index |
|
|
The quarter of the date |
|
|
The seconds of the datetime |
|
|
Return a tuple of the shape of the underlying data |
|
|
Return the number of elements in the DatetimeIndex |
|
|
Vectorized string functions for Index. |
|
|
Return timezone, if any |
|
|
The day of the week with Monday=0, Sunday=6 |
|
|
The year of the datetime |
Construction#
Method |
Description |
Example |
|---|---|---|
|
Create an empty DatetimeIndex |
Indexing / Selection#
Method |
Description |
Example |
|---|---|---|
|
||
|
Return a new DatetimeIndex of the values selected by the indices. |
|
|
Replace values where the condition is False. |
Data Manipulation#
Method |
Description |
Example |
|---|---|---|
|
Make new DatetimeIndex with passed list of labels deleted. |
|
|
Return index with requested level(s) removed. |
|
|
Insert new item at specified position |
|
|
Conform DatetimeIndex to new index. |
|
|
Alter DatetimeIndex name. |
|
|
Set DatetimeIndex name. |
Missing Data#
Method |
Description |
Example |
|---|---|---|
|
Return DatetimeIndex without NA/NaT values. |
|
|
Fill NA/NaT values with the specified value. |
|
|
Detect missing values (NaT) |
|
|
Alias for isna |
|
|
Detect existing (non-missing) values |
|
|
Alias for notna |
Statistics#
Method |
Description |
Example |
|---|---|---|
|
Return the maximum value of the Index. |
|
|
Return the mean value of the Index as nanoseconds. |
|
|
Return the minimum value of the Index. |
|
|
Return number of unique elements |
|
|
Return the standard deviation as nanoseconds. |
|
|
Return a Series containing counts of unique values. |
Aggregation#
Method |
Description |
Example |
|---|---|---|
|
Group the index labels by a mapping |
|
|
Map values using an input mapping or function. |
Comparison#
Method |
Description |
Example |
|---|---|---|
|
Return self==value. |
|
|
Return self>=value. |
|
|
Return self>value. |
|
|
Return self<=value. |
|
|
Return self<value. |
|
|
Return self!=value. |
|
|
Determine if two DatetimeIndex 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 (identity for 1D) |
Combining#
Method |
Description |
Example |
|---|---|---|
|
Append another DatetimeIndex to this one |
|
|
Compute join of this index with other. |
Time Series#
Method |
Description |
Example |
|---|---|---|
|
Return the label from the index, or NaT |
|
|
Compute differences between consecutive elements |
|
|
Shift index by desired number of periods. |
|
|
Cast to PeriodArray/PeriodIndex at a particular frequency. |
|
|
Convert tz-aware DatetimeIndex from one timezone to another. |
|
|
Localize tz-naive DatetimeIndex to tz-aware DatetimeIndex. |
I/O#
Method |
Description |
Example |
|---|---|---|
|
Identity method; returns a copy |
|
|
Convert DatetimeIndex to Julian Date. |
|
|
Return a list of the DatetimeIndex values as nanoseconds |
|
|
Convert to a NumPy array. |
|
|
Return DatetimeIndex as object ndarray of datetime.datetime objects. |
|
|
Create a Series with the Index values. |
|
|
Return a list of the DatetimeIndex values as nanoseconds |
Conversion#
Method |
Description |
Example |
|---|---|---|
|
Cast to a specified dtype. |
|
|
Make a copy of this DatetimeIndex. |
|
|
Attempt to infer better dtype for object columns. |
|
|
Return a view on the index values. |
Iteration#
Method |
Description |
Example |
|---|---|---|
|
||
|
||
|
Set Operations#
Method |
Description |
Example |
|---|---|---|
|
Return a new DatetimeIndex with elements not in other. |
|
|
Return DatetimeIndex with duplicate values removed. |
|
|
Indicate duplicate index values |
|
|
Form the intersection of two DatetimeIndex objects. |
|
|
Check if values are in the DatetimeIndex. |
|
|
Compute the symmetric difference of two Index objects. |
|
|
Form the union of two DatetimeIndex objects. |
|
|
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. |
|
|
Convert times to midnight. |
|
|
Perform round operation on the data to the specified freq. |
|
|
Convert to string array using specified date_format. |
Other Methods#
Method |
Description |
Example |
|---|---|---|
|
Return hash(self). |
|
|
||
|
||
|
Return whether all elements are truthy (non-NaT). |
|
|
Return whether any element is truthy (non-NaT). |
|
|
Return int position of the largest value in the Index. |
|
|
Return int position of the smallest value in the Index. |
|
|
Convert to a new unit resolution. |
|
|
Return the locations (indices) for asof queries. |
|
|
Return the day names of the DatetimeIndex with specified locale. |
|
|
Make new DatetimeIndex 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 and missing indices for non-unique target |
|
|
Return an Index of values for requested level |
|
|
Get integer location for requested label |
|
|
Get slice bound |
|
|
Return if the index holds integer values |
|
|
Determine if two DatetimeIndex objects are identical |
|
|
Return index locations of values at particular time of day. |
|
|
Return index locations of values between particular times of day. |
|
|
Return a DataFrame containing ISO year, week number, and weekday. |
|
|
Return the first element of the data as a scalar |
|
|
Memory usage of the Index values |
|
|
Return the month names of the DatetimeIndex with specified locale. |
|
|
Return a new DatetimeIndex with values put where mask is True. |
|
|
Return a flattened array of the index values. |
|
|
Repeat elements of a DatetimeIndex. |
|
|
Compute slice locations for input labels. |
|
|
Compute slice locations for input labels. |
|
|
Snap time stamps to nearest occurring frequency. |
|
|
Return a sorted copy of the index. |
|
|
Sort Index by level. |
Code Examples#
The following examples are extracted from the test suite.
str (test_datetimeindex_str.py:72)
62def test_datetimeindex_str_call_with_data():
63 """Test DatetimeIndex.str(data) call."""
64 print("Testing DatetimeIndex.str(data) call...")
65
66 # Create a DatetimeIndex
67 dti = pandasCore.DatetimeIndex(['2020-01-01', '2020-12-31'])
68
69 # Call DatetimeIndex.str with the index as argument
70 try:
71 str_accessor = pandasCore.DatetimeIndex.str(dti)
72 print(f" DatetimeIndex.str(dti) returned: {type(str_accessor)}")
73 print(" PASSED: DatetimeIndex.str(data) call works")
74 except Exception as e:
75 # Expected to raise error since DatetimeIndex doesn't contain strings
76 print(f" Error (expected for non-string data): {e}")
77 print(" PASSED: Appropriate error raised for non-string Index")
78
79def f_main():
80 """Main test function."""
81 print("=" * 60)
__init__ (test_datetimeindex.py:51)
41TS_2020_01_04 = TS_2020_01_01 + 3 * NS_PER_DAY
42TS_2020_01_05 = TS_2020_01_01 + 4 * NS_PER_DAY
43
44
45def test_constructor_and_properties():
46 """Test DatetimeIndex constructor and basic properties"""
47 f_print_header("Test: Constructor and Properties")
48
49 # Basic construction
50 data = [TS_2020_01_01, TS_2020_01_02, TS_2020_01_03]
51 dti = pandasCore.DatetimeIndex(data, name="test_dti")
52
53 ctx.assert_equal(3, dti.size, "size")
54 ctx.assert_equal("test_dti", dti.name, "name")
55 ctx.assert_equal("datetime64[ns]", dti.dtype, "dtype")
56 ctx.assert_false(dti.empty, "empty")
57 ctx.assert_true(dti.is_unique, "is_unique")
58 ctx.assert_equal(1, dti.ndim, "ndim")
59 ctx.assert_equal((3,), dti.shape, "shape")
60 ctx.assert_equal(1, dti.nlevels, "nlevels")