
- NumPy - Home
- NumPy - Introduction
- NumPy - Environment
- NumPy Arrays
- NumPy - Ndarray Object
- NumPy - Data Types
- NumPy Creating and Manipulating Arrays
- NumPy - Array Creation Routines
- NumPy - Array Manipulation
- NumPy - Array from Existing Data
- NumPy - Array From Numerical Ranges
- NumPy - Iterating Over Array
- NumPy - Reshaping Arrays
- NumPy - Concatenating Arrays
- NumPy - Stacking Arrays
- NumPy - Splitting Arrays
- NumPy - Flattening Arrays
- NumPy - Transposing Arrays
- NumPy Indexing & Slicing
- NumPy - Indexing & Slicing
- NumPy - Indexing
- NumPy - Slicing
- NumPy - Advanced Indexing
- NumPy - Fancy Indexing
- NumPy - Field Access
- NumPy - Slicing with Boolean Arrays
- NumPy Array Attributes & Operations
- NumPy - Array Attributes
- NumPy - Array Shape
- NumPy - Array Size
- NumPy - Array Strides
- NumPy - Array Itemsize
- NumPy - Broadcasting
- NumPy - Arithmetic Operations
- NumPy - Array Addition
- NumPy - Array Subtraction
- NumPy - Array Multiplication
- NumPy - Array Division
- NumPy Advanced Array Operations
- NumPy - Swapping Axes of Arrays
- NumPy - Byte Swapping
- NumPy - Copies & Views
- NumPy - Element-wise Array Comparisons
- NumPy - Filtering Arrays
- NumPy - Joining Arrays
- NumPy - Sort, Search & Counting Functions
- NumPy - Searching Arrays
- NumPy - Union of Arrays
- NumPy - Finding Unique Rows
- NumPy - Creating Datetime Arrays
- NumPy - Binary Operators
- NumPy - String Functions
- NumPy - Matrix Library
- NumPy - Linear Algebra
- NumPy - Matplotlib
- NumPy - Histogram Using Matplotlib
- NumPy Sorting and Advanced Manipulation
- NumPy - Sorting Arrays
- NumPy - Sorting along an axis
- NumPy - Sorting with Fancy Indexing
- NumPy - Structured Arrays
- NumPy - Creating Structured Arrays
- NumPy - Manipulating Structured Arrays
- NumPy - Record Arrays
- Numpy - Loading Arrays
- Numpy - Saving Arrays
- NumPy - Append Values to an Array
- NumPy - Swap Columns of Array
- NumPy - Insert Axes to an Array
- NumPy Handling Missing Data
- NumPy - Handling Missing Data
- NumPy - Identifying Missing Values
- NumPy - Removing Missing Data
- NumPy - Imputing Missing Data
- NumPy Performance Optimization
- NumPy - Performance Optimization with Arrays
- NumPy - Vectorization with Arrays
- NumPy - Memory Layout of Arrays
- Numpy Linear Algebra
- NumPy - Linear Algebra
- NumPy - Matrix Library
- NumPy - Matrix Addition
- NumPy - Matrix Subtraction
- NumPy - Matrix Multiplication
- NumPy - Element-wise Matrix Operations
- NumPy - Dot Product
- NumPy - Matrix Inversion
- NumPy - Determinant Calculation
- NumPy - Eigenvalues
- NumPy - Eigenvectors
- NumPy - Singular Value Decomposition
- NumPy - Solving Linear Equations
- NumPy - Matrix Norms
- NumPy Element-wise Matrix Operations
- NumPy - Sum
- NumPy - Mean
- NumPy - Median
- NumPy - Min
- NumPy - Max
- NumPy Set Operations
- NumPy - Unique Elements
- NumPy - Intersection
- NumPy - Union
- NumPy - Difference
- NumPy Random Number Generation
- NumPy - Random Generator
- NumPy - Permutations & Shuffling
- NumPy - Uniform distribution
- NumPy - Normal distribution
- NumPy - Binomial distribution
- NumPy - Poisson distribution
- NumPy - Exponential distribution
- NumPy - Rayleigh Distribution
- NumPy - Logistic Distribution
- NumPy - Pareto Distribution
- NumPy - Visualize Distributions With Sea born
- NumPy - Matplotlib
- NumPy - Multinomial Distribution
- NumPy - Chi Square Distribution
- NumPy - Zipf Distribution
- NumPy File Input & Output
- NumPy - I/O with NumPy
- NumPy - Reading Data from Files
- NumPy - Writing Data to Files
- NumPy - File Formats Supported
- NumPy Mathematical Functions
- NumPy - Mathematical Functions
- NumPy - Trigonometric functions
- NumPy - Exponential Functions
- NumPy - Logarithmic Functions
- NumPy - Hyperbolic functions
- NumPy - Rounding functions
- NumPy Fourier Transforms
- NumPy - Discrete Fourier Transform (DFT)
- NumPy - Fast Fourier Transform (FFT)
- NumPy - Inverse Fourier Transform
- NumPy - Fourier Series and Transforms
- NumPy - Signal Processing Applications
- NumPy - Convolution
- NumPy Polynomials
- NumPy - Polynomial Representation
- NumPy - Polynomial Operations
- NumPy - Finding Roots of Polynomials
- NumPy - Evaluating Polynomials
- NumPy Statistics
- NumPy - Statistical Functions
- NumPy - Descriptive Statistics
- NumPy Datetime
- NumPy - Basics of Date and Time
- NumPy - Representing Date & Time
- NumPy - Date & Time Arithmetic
- NumPy - Indexing with Datetime
- NumPy - Time Zone Handling
- NumPy - Time Series Analysis
- NumPy - Working with Time Deltas
- NumPy - Handling Leap Seconds
- NumPy - Vectorized Operations with Datetimes
- NumPy ufunc
- NumPy - ufunc Introduction
- NumPy - Creating Universal Functions (ufunc)
- NumPy - Arithmetic Universal Function (ufunc)
- NumPy - Rounding Decimal ufunc
- NumPy - Logarithmic Universal Function (ufunc)
- NumPy - Summation Universal Function (ufunc)
- NumPy - Product Universal Function (ufunc)
- NumPy - Difference Universal Function (ufunc)
- NumPy - Finding LCM with ufunc
- NumPy - ufunc Finding GCD
- NumPy - ufunc Trigonometric
- NumPy - Hyperbolic ufunc
- NumPy - Set Operations ufunc
- NumPy Useful Resources
- NumPy - Quick Guide
- NumPy - Cheatsheet
- NumPy - Useful Resources
- NumPy - Discussion
- NumPy Compiler
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