Header Ads Widget

⚡ Premium Tools Hub • EXE Apps + Full Python Source Code
Lite • Pro • Bundle Packs • Instant Download

NumPy Dates and Times Explained – Python datetime64, timedelta Tutorial

NumPy – Basics of Dates and Times 

Working with dates and times is essential in data analysis, finance, science, and machine learning.

Time-based data is used in:

  • Stock market analysis
  • Weather forecasting
  • Sensor data tracking
  • Event logging
  • Time series prediction

Using NumPy, we can efficiently handle dates and times using datetime64 and timedelta64.


Why Use NumPy for Dates and Times?

NumPy provides:

  • Fast time calculations
  • Easy vectorized operations
  • Efficient time series handling
  • High-performance date arithmetic

Import NumPy

import numpy as np

1. Creating a Date

NumPy uses datetime64 to represent dates.

import numpy as np

date = np.datetime64('2026-01-01')

print(date)

Output

2026-01-01

Explanation

datetime64 stores date in a compact numerical format.


2. Creating Multiple Dates

import numpy as np

dates = np.array([
'2026-01-01',
'2026-01-02',
'2026-01-03'
], dtype='datetime64')

print(dates)

Explanation

We can store multiple dates in a NumPy array.


3. Generating Date Ranges

import numpy as np

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

print(dates)

Explanation

This generates daily dates for January 2026.


4. Date Arithmetic (Adding Days)

import numpy as np

date = np.datetime64('2026-01-01')

new_date = date + np.timedelta64(10, 'D')

print(new_date)

Output

2026-01-11

Explanation

We added 10 days to the original date.


5. Subtracting Dates

import numpy as np

date1 = np.datetime64('2026-01-10')

date2 = np.datetime64('2026-01-01')

diff = date1 - date2

print(diff)

Output

9 days

Explanation

NumPy returns the difference in days.


6. Working with Time (Hours, Minutes, Seconds)

import numpy as np

time = np.datetime64('2026-01-01T12:30')

print(time)

Output

2026-01-01T12:30

Explanation

We can include time along with date.


7. Time Differences in Hours

import numpy as np

t1 = np.datetime64('2026-01-01T10:00')
t2 = np.datetime64('2026-01-01T15:00')

diff = t2 - t1

print(diff)

Output

5 hours

Explanation

NumPy automatically calculates time difference in hours.


8. Using Timedelta (Time Intervals)

import numpy as np

delta = np.timedelta64(3, 'D')

print(delta)

Output

3 days

Explanation

timedelta64 represents a duration of time.


9. Adding Time Intervals

import numpy as np

date = np.datetime64('2026-01-01')

new_date = date + np.timedelta64(7, 'D')

print(new_date)

Output

2026-01-08

10. Creating Monthly Date Ranges

import numpy as np

months = np.arange(
'2026-01',
'2026-06',
dtype='datetime64[M]'
)

print(months)

Explanation

This generates monthly intervals.


11. Finding Number of Days Between Dates

import numpy as np

start = np.datetime64('2026-01-01')
end = np.datetime64('2026-02-01')

print(end - start)

Output

31 days

Explanation

Useful in financial and time-based analysis.


12. Real-World Example: Event Tracking

import numpy as np

events = np.array([
'2026-01-01',
'2026-01-05',
'2026-01-10'
], dtype='datetime64')

intervals = np.diff(events)

print(intervals)

Explanation

np.diff() finds time gaps between events.


Real-World Applications

1. Finance

  • Stock price tracking
  • Trading analysis
  • Investment planning

2. Data Science

  • Time series forecasting
  • Trend analysis
  • Data logging

3. IoT Systems

  • Sensor time tracking
  • Device monitoring
  • Event scheduling

4. Healthcare

  • Patient monitoring
  • Medical record tracking
  • Treatment timelines

5. Business Analytics

  • Sales tracking
  • Performance analysis
  • Scheduling systems

Common NumPy Date Functions

FunctionPurpose
np.datetime64Create date/time
np.timedelta64Time duration
np.arange()Date ranges
np.diff()Time differences
+ / -Date arithmetic

Why Use NumPy for Dates and Times?

Using NumPy provides:

  • Fast time calculations
  • Easy vectorized operations
  • Efficient date handling
  • Support for large datasets

Combined with Python, it becomes powerful for time series analysis and real-world applications.


Summary

NumPy date and time features include:

np.datetime64()
np.timedelta64()
np.arange()
np.diff()

They are essential for working with time-based data efficiently.


Conclusion

Understanding dates and times in NumPy is crucial for data science, finance, and engineering applications. NumPy provides powerful tools like datetime64 and timedelta64 to simplify time-based calculations and analysis.




Post a Comment

0 Comments