Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

NumPy - Working with Time Deltas



Working with Time Deltas in NumPy

Time deltas represent the difference between two datetime objects and are useful when working with time-based data.

In NumPy, time deltas are handled using the timedelta64 data type, which allows you to perform various operations involving differences between dates and times.

This chapter will explore how to work with time deltas in NumPy, including creating time deltas, performing arithmetic operations, and using them to manipulate datetime objects.

Creating Time Deltas with NumPy

In NumPy, time deltas are represented by the timedelta64 data type. You can create time deltas by specifying a duration and a unit (e.g., days, hours, minutes).

Time deltas can be used to calculate the difference between two datetime objects or to add/subtract from a datetime.

Example

In this example, we will create a time delta of 5 days and 10 hours −

import numpy as np

# Create a time delta of 5 days and 10 hours
time_delta = np.timedelta64(5, 'D') + np.timedelta64(10, 'h')

print("Time Delta:", time_delta)

The output will show the time delta as a combination of days and hours −

Time Delta: 130 hours

Adding Time Deltas to DateTimes

Time deltas can be added or subtracted from datetime objects to adjust the date or time. This is useful when you want to shift a timestamp by a certain duration, for example, adding days or subtracting hours from a specific date.

Example

In this example, we will add a time delta of 5 days and 10 hours to a specific date −

import numpy as np

# Create a datetime object
start_date = np.datetime64('2024-01-01')

# Create a time delta of 5 days and 10 hours
time_delta = np.timedelta64(5, 'D') + np.timedelta64(10, 'h')

# Add the time delta to the start date
new_date = start_date + time_delta

print("New Date:", new_date)

The output will show the new date after adding the time delta −

New Date: 2024-01-06T10

Subtracting Time Deltas from DateTimes

Just as you can add time deltas to datetime objects, you can also subtract them to shift a datetime backward. Subtracting time deltas is often used to calculate past dates or times.

Example

In this example, we will subtract a time delta of 5 days and 10 hours from a specific date −

import numpy as np

# Create a datetime object
start_date = np.datetime64('2024-01-06T10:00')

# Create a time delta of 5 days and 10 hours
time_delta = np.timedelta64(5, 'D') + np.timedelta64(10, 'h')

# Subtract the time delta from the start date
new_date = start_date - time_delta

print("New Date after Subtraction:", new_date)

The output will show the new date after subtracting the time delta −

New Date after Subtraction: 2024-01-01T00:00

Comparing DateTimes with Time Deltas

You can compare datetime objects that have been modified with time deltas. This allows you to check whether one datetime is before or after another, or if two datetime objects are the same, given a time delta.

Example

In this example, we will compare two datetime objects with different time deltas to determine if one is earlier than the other −

import numpy as np

# Create two datetime objects
date1 = np.datetime64('2024-01-01')
date2 = np.datetime64('2024-01-05')

# Create a time delta of 5 days
time_delta = np.timedelta64(5, 'D')

# Check if date1 is earlier than date2 after adding the time delta
new_date1 = date1 + time_delta
print("Is date1 + time_delta earlier than date2?", new_date1 < date2)

The output will indicate whether the new date1 is earlier than date2 −

Is date1 + time_delta earlier than date2? False

Time Delta Arithmetic

Time deltas support arithmetic operations such as addition, subtraction, multiplication, and division. You can scale time deltas by multiplying them by an integer, or divide them by a scalar to get smaller time units.

Example

In this example, we will multiply a time delta by 2 and divide it by 2 to see the resulting time deltas −

import numpy as np

# Create a time delta of 5 days
time_delta = np.timedelta64(5, 'D')

# Multiply the time delta by 2
double_delta = time_delta * 2

# Divide the time delta by 2
half_delta = time_delta / 2

print("Original Time Delta:", time_delta)
print("Time Delta * 2:", double_delta)
print("Time Delta / 2:", half_delta)

The output will show the time delta after multiplication and division −

Original Time Delta: 5 days
Time Delta * 2: 10 days
Time Delta / 2: 2 days
Advertisements