How to check if datetime is month change in Python using UliEngineering

You can easily detect month changes in a datetime array using the UliEngineering Python library:

is_month_change.py
import numpy as np
from UliEngineering.Utils.Date import *

# Check NumPy datetime64 array
dates = np.array(['2024-01-31', '2024-02-01', '2024-02-28', '2024-03-01'], dtype='datetime64[D]')
result = is_month_change(dates)
print(f"Month change detection: {result}")

# Check consecutive days
dates = np.array(['2024-01-01', '2024-01-02', '2024-01-03'], dtype='datetime64[D]')
result = is_month_change(dates)
print(f"No month change: {result}")

Example output

is_month_change_output.txt
Month change detection: [ True  True False  True]
No month change: [False False False]

The is_month_change() function returns True for the first element and for each position where the month changes compared to the previous element.


Check out similar posts by category: Python, NumPy