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/Manilatime 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.
format="%d/%m/%Y"says exactly how the text is laid out: two-digit day, slash, month, slash, four-digit year.%d,%mand%Yare Python's format codes, listed in the sources. Others you'll meet:%H:%Mfor hours and minutes,%bforApr..dtreaches the parts of each date:day_name(),month,year,day,quarterand more. 3 April 2025 was a Thursday.- Subtracting two dates gives a
Timedelta, a length of time: 27 days. - Adding a
Timedeltamoves 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:
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.
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
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.
- pandas: Time series / date functionality: to_datetime, Timedelta, the .dt accessor and time zones
- pandas: pandas.to_datetime: format, dayfirst, and how pandas guesses
- The Python Standard Library: datetime, format codes: %d, %m, %Y and the rest
- The Python Standard Library: zoneinfo: time zone names like Asia/Manila