How to change the datetime format in Pandas
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Pandas provides several ways to change datetime formats: pd.to_datetime() converts strings to datetime objects, dt.strftime() formats datetime objects back to strings, and dt accessor properties extract individual components like year, month, and day. The key distinction is between the internal datetime representation (used for calculations) and the display format (used for output). Always store dates as datetime objects and only format them as strings for display or export.
Converting Strings to Datetime
Specifying the format parameter avoids the overhead of format inference and prevents ambiguous parsing (e.g., is 01/02/03 January 2 or February 1?).
Common String-to-Datetime Conversions
errors="coerce" converts unparseable values to NaT (Not a Time) instead of raising an error.
Formatting Datetime to String
Format Code Reference
| Code | Meaning | Example |
%Y | 4-digit year | 2025 |
%y | 2-digit year | 25 |
%m | Month (zero-padded) | 03 |
%B | Month name | March |
%b | Abbreviated month | Mar |
%d | Day (zero-padded) | 02 |
%H | Hour (24-hour) | 14 |
%I | Hour (12-hour) | 02 |
%M | Minute | 30 |
%S | Second | 45 |
%p | AM/PM | PM |
%A | Day name | Sunday |
%a | Abbreviated day | Sun |
Extracting Date Components
Changing the Datetime Index Format
Handling Timezones
Reading Dates from CSV
Parsing dates during read_csv is faster than converting after loading because it avoids creating intermediate string objects.
Common Pitfalls
- Confusing datetime objects with formatted strings: After
dt.strftime(), the column contains strings, not datetimes. You cannot perform date arithmetic (timedeltaaddition, comparison) on formatted strings. Keep the datetime column for calculations and create a separate formatted column for display. - Ambiguous date formats without explicit
format:pd.to_datetime("01/02/03")is ambiguous — is it January 2, 2003 or February 1, 2003? Always specify theformatparameter for non-ISO date strings to avoid silent misinterpretation. - Using
errors="coerce"without checking for NaT: Coercing bad dates toNaTsilently hides data quality issues. After conversion, checkdf["date"].isna().sum()to see how many values failed to parse. - Formatting the DatetimeIndex and losing datetime functionality: Converting a
DatetimeIndexto strings withstrftime()makes resampling, slicing, and time-based operations impossible. Only format for final output, never for intermediate processing. - Performance with
pd.to_datetimeon large datasets: Without aformatparameter, pandas tries multiple formats for each row. On millions of rows, specifying the format explicitly can be 10x faster.
Summary
- Use
pd.to_datetime()to convert strings to datetime objects — always specifyformatfor non-ISO dates - Use
dt.strftime()to format datetime objects as display strings - Use
dt.year,dt.month,dt.dayaccessors to extract date components - Keep datetime columns as
datetime64for calculations — only convert to strings for output - Use
parse_datesinpd.read_csv()for efficient date parsing during file loading - Set
errors="coerce"to convert bad dates toNaT, but always check for missing values afterward

