Format Pandas timestamps, date indexes and Series as text. Practise directives, missing dates, timezones and fiscal-period labels.
Show Table of Contents ↓import pandas as pd
dates = pd.date_range(start="2019-10-20", end="2020-02-14", freq="ME")
formatted = dates.strftime("%d-%b-%Y")
print(formatted)
assert len(formatted) == 4
assert dates[0] == pd.Timestamp("2019-10-31")Expected output
Index(['31-Oct-2019', '30-Nov-2019', '31-Dec-2019', '31-Jan-2020'], dtype='str')iso = dates.strftime("%Y-%m-%d")
print(iso.tolist())
assert iso.tolist() == ["2019-10-31", "2019-11-30", "2019-12-31", "2020-01-31"]Expected output
['2019-10-31', '2019-11-30', '2019-12-31', '2020-01-31']| Directive | Meaning |
|---|---|
%a / %A | Abbreviated / full weekday name; locale dependent. |
%w | Weekday number, Sunday 0 to Saturday 6. |
%W / %U | Week number with Monday / Sunday first; days before the first such weekday are week 00. |
%d / %j | Day of month / day of year. |
%b / %B / %m | Abbreviated month / full month / numeric month. |
%y / %Y | Two-digit / full calendar year. |
%H / %I | 24-hour / 12-hour clock. |
%M / %S | Minute / second. |
%p | AM/PM; locale dependent. |
%f | Microseconds, not a fiscal year for datetime values. |
%z / %Z | UTC offset / timezone name. |
%x / %X / %c | Locale-specific date / time / combined display. |
The original fiscal-year and quarter entries need object-specific treatment: Period.strftime() supports %q, %f and %F; DatetimeIndex/Series datetime formatting does not provide the same fiscal-period semantics. Use the period example below. Directive support can also vary by operating system; prefer explicit numeric directives for exchange formats.
stamp = pd.Timestamp("2026-09-05 14:35:20.123456")
print(stamp.strftime("%Y-%m-%d %H:%M:%S.%f"))
print("Weekday/day of year:", stamp.strftime("%w %j"))
print("Week numbers:", stamp.strftime("%W %U"))
assert stamp.strftime("%H:%M:%S") == "14:35:20"Expected output
2026-09-05 14:35:20.123456
Weekday/day of year: 6 248
Week numbers: 35 35table = pd.DataFrame({"date_text": ["2026-09-05", "invalid", None]})
table["date"] = pd.to_datetime(table["date_text"], format="%Y-%m-%d", errors="coerce")
table["display_date"] = table["date"].dt.strftime("%d/%m/%Y").astype("string")
print(table)
assert table["date"].isna().sum() == 2
assert table["display_date"].iloc[0] == "05/09/2026"Expected output
date_text date display_date
0 2026-09-05 2026-09-05 05/09/2026
1 invalid NaT <NA>
2 NaN NaT <NA>display = table["display_date"].fillna("Needs review")
print(display.tolist())
assert display.tolist() == ["05/09/2026", "Needs review", "Needs review"]Expected output
['05/09/2026', 'Needs review', 'Needs review']utc = pd.Timestamp("2026-09-05 09:00:00", tz="UTC")
local = utc.tz_convert("Asia/Kolkata")
print(local.strftime("%Y-%m-%d %H:%M %z %Z"))
assert local.strftime("%H:%M %z") == "14:30 +0530"Expected output
2026-09-05 14:30 +0530 ISTperiod = pd.Period("2026-05", freq="Q-MAR")
print(period.strftime("Fiscal %F, short year %f, quarter %q"))
assert period.strftime("%F Q%q") == "2027 Q1"Expected output
Fiscal 2027, short year 27, quarter 1report_dates = pd.date_range("2026-09-05", periods=2, freq="D")
labels = report_dates.strftime("%Y%m%d")
print(labels.tolist())
assert labels.tolist() == ["20260905", "20260906"]
assert report_dates[0] == pd.Timestamp("2026-09-05")Expected output
['20260905', '20260906']Open in Google Colab View on GitHub
Run examples in order, inspect formatted dates and try the exercise. Save a copy to keep edits.
Reference: Pandas strftime() documentation.
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.