pandas in depth · lesson 6 of 12
Parts in this lesson
Apd.to_datetime(s, format="%d/%m/%Y")day, month, year, as written here
Bs.dt.day_name()parts of a date
Cpd.Timedelta(days=7)a length of time
Dtz_localize("Asia/Manila")say which clock a time is on

Dates and times

Last updated

Dates look simple until you try to compute with them. Is 03/04/2025 the 3rd of April or the 4th of March? How many days between two sales? What time was a payment made in Manila, if the server says 01:30? In this lesson you'll read dates on purpose, pull parts out of them, do arithmetic with them, and handle time zones.

By the end of this lesson you can

  • Read dates with an explicit format, day first as Filipinos write them
  • Pull out the year, month and weekday with .dt
  • Subtract dates and add a Timedelta
  • Attach the Asia/Manila time zone and convert to UTC

Reading, parts and arithmetic

Three receipt dates, written the Philippine way: day, then month. Run the cells; step 2 explains the last one.

dates.ipynb
  • format="%d/%m/%Y" says exactly how the text is laid out: two-digit day, slash, month, slash, four-digit year. %d, %m and %Y are Python's format codes, listed in the sources. Others you'll meet: %H:%M for hours and minutes, %b for Apr.
  • .dt reaches the parts of each date: day_name(), month, year, day, quarter and more. 3 April 2025 was a Thursday.
  • Subtracting two dates gives a Timedelta, a length of time: 27 days.
  • Adding a Timedelta moves dates: a week later.

Time zones

A payment made at 9:30 in the morning in Batangas is 1:30 in the morning in UTC, the time most servers and many APIs use. The last cell in the notebook above shows both:

  • tz_localize("Asia/Manila") says "this time is on the Philippine clock". It doesn't change the time, only labels it: +0800, eight hours ahead of UTC.
  • tz_convert("UTC") shows the same moment on another clock.

A time with no zone is called naive. Two naive times from different clocks look comparable and aren't. When data comes from a server, assume it's UTC until you find out otherwise, convert it to Asia/Manila, and then take .dt.date or .dt.hour. Otherwise a sale made at 7:00 in the morning in Manila is counted on the day before.

(The Philippines keeps UTC+8 all year today, but it has used daylight saving time in the past, and many countries still do. A named zone like Asia/Manila knows the rules for every date; a fixed +08:00 doesn't.)

Wrong way: 3 April or 4 March?

Step through how the same text gives two different dates:

One date, two readings. A receipt says 03/04/2025. Three numbers. Which is the day and which the month? The text alone can’t say.03/04/2025the text
1 of 3 A receipt says 03/04/2025. Three numbers. Which is the day and which the month? The text alone can’t say.

Now three receipts from April. Run both cells.

receipts.ipynb

Without a format, pandas reads the dates month first, the American way: March, May and November. Every one is wrong, there's no error, and none of them looks odd, because 03, 05 and 11 are all possible months. If one had been 15/04/2025, pandas would have failed on it, and you'd have noticed. Dates up to the 12th are the dangerous ones.

The second cell gives the format, and all three are April. Always give a format for dates written with slashes. If you can't, dayfirst=True tells pandas to try day first, but it still guesses. Better still, ask whoever sends the data to use 2025-04-03: year, month, day, the international standard, ISO 8601. It's never ambiguous, and it sorts correctly as text.

Check what you learned

Dates are written like 25-12-2024. Which format reads them?
A server logs a sale at 2024-12-24 18:00 UTC. What date was it in Manila?
pd.to_datetime with no format reads "07/08/2025". What do you get, with no error?

Sources

Every example on this page was run with Python 3.14, and its output and error messages are copied from it. These pages cover the same ground in more depth.

All sources for Simple Data Analysis