/Curriculum/IntermediateData Types

32. NumPy Date & TimeIntermediate

Handle timestamps and durations with datetime64 and timedelta64.

12 mins

Concept Overview

NumPy provides high-performance fixed-width `datetime64` and `timedelta64` types to support efficient date ranges and time-series arithmetic.

Hardware Mental Model

Think of `datetime64` as a precise stopwatch starting from January 1, 1970 (Unix Epoch), capable of measuring dates in days, hours, or nanoseconds.

Key Concepts (1)Click snippet to load in editor

Specify precision inside brackets: `datetime64[D]` for Day precision, `datetime64[s]` for seconds, `datetime64[ns]` for nanoseconds.

dates = np.arange('2026-01-01', '2026-01-10', dtype='datetime64[D]')

⚠ Common Mistakes & Pitfalls

Avoid these frequent beginner syntax and logic traps
❌ Incorrect:np.datetime64('17-08-2026') # Fails to parse invalid format
✓ Correct:np.datetime64('2026-08-17') # ISO 8601 YYYY-MM-DD

NumPy datetime64 strictly follows the ISO 8601 standard (`YYYY-MM-DD`).

Pro Tip: Always write dates in `YYYY-MM-DD` ISO format.

📌 Quick Revision

Core takeaway points from this topic
`np.datetime64('YYYY-MM-DD')`: Represents point in time.
`np.timedelta64(N, unit)`: Represents duration (e.g. `10, 'D'`).
`np.arange(start, stop, dtype='datetime64[D]')`: Creates date ranges.
Editor: 32. NumPy Date & Time
Python ● Ready
Initializing Python IDE...
Ctrl+Enter
Execution Result
Click "RUN CODE" to execute and inspect array state

🎯 Try It Yourself: Practice Challenge

Hands-on Mode

Solve the objective below using NumPy vectorized syntax

Objective:Create a Date Object

Create `d = np.datetime64('2026-01-01')` and print it.

Expected Output Target:2026-01-01
Practice Workspace
Python ● Ready
Initializing Python IDE...
Ctrl+Enter
Execution Result
Click "RUN CODE" to execute and inspect array state