
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Python Pandas - Return an Index of formatted strings specified by date format
To return an Index of formatted strings specified by date format, use the DateTimeIndex.strftime() method in Pandas.
At first, import the required libraries −
import pandas as pd
Create a DatetimeIndex with period 7 and frequency as D i.e. days −
datetimeindex = pd.date_range('2021-10-30 02:30:50', periods=7, tz='Australia/Adelaide', freq='2D')
Display DateTimeIndex −
print("DateTimeIndex...\n", datetimeindex)
Formatted −
print("\nFormat with different directives...\n", datetimeindex.strftime('%b. %d, %Y was a %A'))
Example
Following is the code −
import pandas as pd # DatetimeIndex with period 7 and frequency as D i.e. days # The timezone is Australia/Adelaide datetimeindex = pd.date_range('2021-10-30 02:30:50', periods=7, tz='Australia/Adelaide', freq='2D') # display DateTimeIndex print("DateTimeIndex...\n", datetimeindex) # display DateTimeIndex frequency print("\nDateTimeIndex frequency...\n", datetimeindex.freq) # display the result print("\nFormat with different directives...\n", datetimeindex.strftime('%b. %d, %Y was a %A'))
Output
This will produce the following code −
DateTimeIndex... DatetimeIndex(['2021-10-30 02:30:50+10:30', '2021-11-01 02:30:50+10:30', '2021-11-03 02:30:50+10:30', '2021-11-05 02:30:50+10:30', '2021-11-07 02:30:50+10:30', '2021-11-09 02:30:50+10:30', '2021-11-11 02:30:50+10:30'], dtype='datetime64[ns, Australia/Adelaide]', freq='2D') DateTimeIndex frequency... <2 * Days> Format with different directives... Index(['Oct. 30, 2021 was a Saturday', 'Nov. 01, 2021 was a Monday', 'Nov. 03, 2021 was a Wednesday', 'Nov. 05, 2021 was a Friday', 'Nov. 07, 2021 was a Sunday', 'Nov. 09, 2021 was a Tuesday', 'Nov. 11, 2021 was a Thursday'], dtype='object')
- Related Articles
- Python Pandas - Return the Transpose of the index
- Python Pandas - Return Index without NaN values
- Python Pandas - Return a new Index of the values selected by the indices
- Python - Return an array representing the data in the Pandas Index
- Python - Return the minimum value of the Pandas Index
- Python - Return the maximum value of the Pandas Index
- Python Pandas - Return a list of the Index values
- Python Pandas - Return a sorted copy of the index
- Python Pandas - Fill NaN values with the specified value in an Index object
- Formatted string literals (f-strings) in Python?
- Python Pandas - Return unique values in the index
- Python Pandas - Return Index with duplicate values removed
- Python Pandas - Return the memory usage of the Index values
- Python Pandas - Return the midpoint of each Interval in the IntervalArray as an Index
- Python Pandas - Repeat elements of an Index

Advertisements