How to extract day of week from NumPy datetime64 arrays in Python using UliEngineering
You can easily extract the day of week component from NumPy datetime64 arrays using the UliEngineering Python library:
extract_day_of_week.py
import numpy as np
from UliEngineering.Utils.Date import *
# Create a datetime64 array
dates = np.array(['2024-01-15', '2024-06-20', '2024-12-31'], dtype='datetime64[D]')
# Extract day of week (0 = Monday, 6 = Sunday)
days = extract_day_of_week(dates)
print(f"Dates: {dates}")
print(f"Day of week (0=Mon, 6=Sun): {days}")Example output
extract_day_of_week_output.txt
Dates: ['2024-01-15' '2024-06-20' '2024-12-31']
Day of week (0=Mon, 6=Sun): [0 3 2]The extract_day_of_week() function returns a NumPy array containing the day of week (0-6, where 0 is Monday and 6 is Sunday) for each datetime64 value in the input array.
If this post helped you, please consider buying me a coffee or donating via PayPal to support research & publishing of new posts on TechOverflow