Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
Python Pandas - Return an Index of formatted strings specified by date format
The DateTimeIndex.strftime() method in Pandas returns an Index of formatted strings based on a specified date format. This method is useful for converting datetime objects into human-readable string representations.
Syntax
DateTimeIndex.strftime(date_format)
Parameters:
- date_format ? A string specifying the format using strftime directives
Creating a DateTimeIndex
First, let's create a DateTimeIndex with timezone information ?
import pandas as pd
# Create DatetimeIndex with period 7 and frequency as 2 days
datetimeindex = pd.date_range('2021-10-30 02:30:50', periods=7, tz='Australia/Adelaide', freq='2D')
print("DateTimeIndex...")
print(datetimeindex)
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')
Using strftime() to Format Dates
Now let's format these dates using various strftime directives ?
import pandas as pd
# Create DatetimeIndex
datetimeindex = pd.date_range('2021-10-30 02:30:50', periods=7, tz='Australia/Adelaide', freq='2D')
# Format with different directives
formatted_dates = datetimeindex.strftime('%b. %d, %Y was a %A')
print("Formatted dates:")
print(formatted_dates)
Formatted dates:
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')
Common Format Directives
Here are different format patterns you can use with strftime() ?
import pandas as pd
datetimeindex = pd.date_range('2021-10-30', periods=3, freq='D')
print("ISO format:", datetimeindex.strftime('%Y-%m-%d'))
print("US format:", datetimeindex.strftime('%m/%d/%Y'))
print("Full format:", datetimeindex.strftime('%A, %B %d, %Y'))
print("Short format:", datetimeindex.strftime('%a, %b %d'))
ISO format: Index(['2021-10-30', '2021-10-31', '2021-11-01'], dtype='object')
US format: Index(['10/30/2021', '10/31/2021', '11/01/2021'], dtype='object')
Full format: Index(['Saturday, October 30, 2021', 'Sunday, October 31, 2021',
'Monday, November 01, 2021'],
dtype='object')
Short format: Index(['Sat, Oct 30', 'Sun, Oct 31', 'Mon, Nov 01'], dtype='object')
Key Format Codes
| Code | Description | Example |
|---|---|---|
| %Y | 4-digit year | 2021 |
| %m | Month as number | 10 |
| %B | Full month name | October |
| %b | Abbreviated month | Oct |
| %d | Day of month | 30 |
| %A | Full weekday name | Saturday |
Conclusion
The strftime() method provides flexible date formatting for DateTimeIndex objects. Use format directives like %Y, %m, %d to create custom date string representations that suit your specific needs.
Advertisements
