NumPy: Date and Time
Last updated: 2026-08-26
1. What You'll Learn
- ❶ Creating datetime64 arrays
- ❷ Date arithmetic and durations
- ❸ Business day functions
- ❹ Extracting date components (year, month, day)
2. Key Concepts
PYTHON
import numpy as np
# Creating datetime arrays
dates = np.array(['2024-01-01', '2024-02-01', '2024-03-01'], dtype='datetime64')
print(dates) # ['2024-01-01' '2024-02-01' '2024-03-01']
# Date arithmetic
print(dates + np.arange(3)) # add days
# Date range
range_dates = np.arange('2024-01-01', '2024-01-10', dtype='datetime64')
print(range_dates)
# Extract components
print(dates.astype('datetime64[Y]')) # year
print(dates.astype('datetime64[M]')) # month
TEXT
📖 Display only
> **Output:** Run NumPy 2.x in your local Python environment to see the ndarray output. The Piston server does not have NumPy pre-installed — install it locally (`pip install numpy`) and compare. Actual values may vary by NumPy version and random seed.
▶ Example: Creating date ranges (Difficulty ⭐)
PYTHON
import numpy as np
# Daily range
days = np.arange('2024-01-01', '2024-01-11', dtype='datetime64')
print("Daily:", days)
# Monthly range
months = np.arange('2024-01', '2024-07', dtype='datetime64[M]')
print("Monthly:", months)
# Hourly range
hours = np.arange('2024-01-01', '2024-01-02', dtype='datetime64[h]')
print("Hourly:", hours)
Output:
TEXT 📖 Display onlyDaily: ['2024-01-01' '2024-01-02' '2024-01-03' '2024-01-04' '2024-01-05' '2024-01-06' '2024-01-07' '2024-01-08' '2024-01-09' '2024-01-10'] Monthly: ['2024-01' '2024-02' '2024-03' '2024-04' '2024-05' '2024-06'] Hourly: ['2024-01-01T00' '2024-01-01T01' '2024-01-01T02' '2024-01-01T03' '2024-01-01T04' '2024-01-01T05' '2024-01-01T06' '2024-01-01T07' '2024-01-01T08' '2024-01-01T09' '2024-01-01T10' '2024-01-01T11' '2024-01-01T12' '2024-01-01T13' '2024-01-01T14' '2024-01-01T15' '2024-01-01T16' '2024-01-01T17' '2024-01-01T18' '2024-01-01T19' '2024-01-01T20' '2024-01-01T21' '2024-01-01T22' '2024-01-01T23']
▶ Example: Date arithmetic (Difficulty ⭐⭐)
PYTHON
import numpy as np
start = np.array('2024-01-15', dtype='datetime64')
# Add days
print("Start:", start)
print("+7 days:", start + 7)
print("+30 days:", start + 30)
print("-5 days:", start - 5)
# Difference between dates
d1 = np.array('2024-01-01', dtype='datetime64')
d2 = np.array('2024-12-31', dtype='datetime64')
delta = d2 - d1
print("Days in 2024:", delta)
# Extract components
date = np.array('2024-07-04', dtype='datetime64')
print("Year:", date.astype('datetime64[Y]'))
print("Month:", date.astype('datetime64[M]'))
Output:
TEXT 📖 Display onlyStart: 2024-01-15 +7 days: 2024-01-22 +30 days: 2024-02-14 -5 days: 2024-01-10 Days in 2024: 365 days Year: 2024 Month: 2024-07
▶ Example: Business day calculations (Difficulty ⭐⭐)
PYTHON
import numpy as np
start = np.datetime64('2024-01-01')
end = np.datetime64('2024-01-31')
# Count business days
bd = np.busday_count(start, end)
print(f"Business days Jan 2024: {bd}")
# Find next business day
print("Next business day after 2024-01-05:", np.busday_offset('2024-01-05', 0, roll='forward'))
# Business day range
biz_days = np.busday_offset('2024-01-01', np.arange(10))
print("First 10 business days of 2024:", biz_days)
Output:
TEXT 📖 Display onlyBusiness days Jan 2024: 22 Next business day after 2024-01-05: 2024-01-08 First 10 business days of 2024: ['2024-01-01' '2024-01-02' '2024-01-03' '2024-01-04' '2024-01-05' '2024-01-08' '2024-01-09' '2024-01-10' '2024-01-11' '2024-01-12']
Q What's the difference between datetime64 and Python's datetime?
A datetime64 is a fixed-width 64-bit integer representing time, making it fast and memory-efficient. Python's datetime is a full object with more flexibility but higher overhead.
Q What's the resolution of datetime64?
A It depends on the unit —
datetime64[Y] (year), datetime64[M] (month), datetime64[D] (day), datetime64[h] (hour), datetime64[ms] (millisecond), datetime64[ns] (nanosecond).Q How do I compute business days?
A Use
np.busday_count(start, end) to count business days between two dates, and np.busday_offset to find the nearest business day.❓ FAQ
Q What is the most important thing to remember?
A NumPy operations are vectorized — avoid Python loops for better performance.
Q Where can I learn more?
A Check the official NumPy documentation at numpy.org for detailed references and advanced topics.
Q Does this work with NumPy 2.x?
A Yes — all examples are compatible with NumPy 2.x. Some older APIs (like np.random.seed) are still supported but the modern alternatives are recommended.
📖 Summary
- datetime64 arrays store dates/times as 64-bit integers — fast and compact
np.arangewith datetime64 dtype creates date ranges- Arithmetic: add/subtract integers to shift by the datetime unit
astypeextracts components: year, month, day, etc.- Business day functions:
busday_count,busday_offset
📝 Exercises
-
Beginner (Difficulty ⭐): Create an array of all dates in January 2024. Print the first 5 and last 5 dates.
-
Intermediate (Difficulty ⭐⭐): Create a date range from 2024-01-01 to 2024-12-31. Extract the month from each date and count how many dates fall in each month.
-
Advanced (Difficulty ⭐⭐⭐): Compute the number of business days between 2024-01-01 and 2024-12-31. Compare with the total number of calendar days.