如何使用 UliEngineering 在 Python 中从 NumPy datetime64 数组提取年份

你可以使用 UliEngineering Python 库轻松从 NumPy datetime64 数组中提取年份分量:

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

# 创建 datetime64 数组
dates = np.array(['2024-01-15', '2023-06-20', '2025-12-31'], dtype='datetime64[D]')

# 提取年份
years = extract_years(dates)

print(f"Dates: {dates}")
print(f"Years: {years}")

示例输出

extract_years_output.txt
Dates: ['2024-01-15' '2023-06-20' '2025-12-31']
Years: [2024 2023 2025]

extract_years() 函数返回一个 NumPy 数组,包含输入数组中每个 datetime64 值对应的年份。


Check out similar posts by category: Python, NumPy