How to extract months from NumPy datetime64 arrays in Python using UliEngineering
You can easily extract the month component from NumPy datetime64 arrays using the UliEngineering Python library:
extract_months.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 months
months = extract_months(dates)
print(f"Dates: {dates}")
print(f"Months: {months}")Example output
extract_months_output.txt
Dates: ['2024-01-15' '2024-06-20' '2024-12-31']
Months: [ 1 6 12]The extract_months() function returns a NumPy array containing the month number (1-12) 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