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.
Related posts
- How to check if datetime is first day of week in Python using UliEngineering
- How to check if datetime is month change in Python using UliEngineering
- How to extract years from NumPy datetime64 arrays in Python using UliEngineering
If this post helped you, please consider buying me a coffee or donating via PayPal to support research & publishing of new posts on TechOverflow