Como gerar array datetime para meses em Python usando UliEngineering
Você pode gerar facilmente um array datetime64 do NumPy para um intervalo de meses usando a biblioteca Python UliEngineering:
generate_months.py
import numpy as np
from UliEngineering.Utils.Date import *
# Gerar array datetime para Janeiro-Junho 2024
months = generate_months("2024-01-01", "2024-06-30")
print(f"Months Jan-Jun 2024: {len(months)}")
print(f"First month: {months[0]}")
print(f"Last month: {months[-1]}")
# Gerar array datetime para um ano completo
months = generate_months("2024-01-01", "2024-12-31")
print(f"\nMonths in a year: {months}")Exemplo de saída
generate_months_output.txt
Months Jan-Jun 2024: 6
First month: 2024-01-01
Last month: 2024-06-01
Months in a year: ['2024-01-01' '2024-02-01' '2024-03-01' '2024-04-01' '2024-05-01'
'2024-06-01' '2024-07-01' '2024-08-01' '2024-09-01' '2024-10-01' '2024-11-01'
'2024-12-01']A função generate_months() retorna um array datetime64 do NumPy contendo o primeiro dia de cada mês desde a data inicial (inclusive) até a data final (inclusive).
Posts relacionados
- Como gerar array datetime para dias em Python usando UliEngineering
- Como gerar array datetime para anos em Python usando UliEngineering
- Como extrair meses de arrays datetime64 do NumPy em Python usando 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