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

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

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

# Check NumPy datetime64 array
dates = np.array(['2023-12-31', '2024-01-01', '2024-12-31', '2025-01-01'], dtype='datetime64[D]')
result = is_year_change(dates)
print(f"Year change detection: {result}")

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

Example output

is_year_change_output.txt
Year change detection: [ True  True False  True]
No year change: [False False False]

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


Check out similar posts by category: Python, NumPy