创建每天一个 datetime64 的 numpy 数组并标记月初

numpy_datetime64_array.py
startdate = np.datetime64('2022-01-01T00:00:00.000000')
 # 10000 天。此数组包含 0(第 0 天)、1(第 1 天)等
day_offsets = np.arange(10000, dtype=np.int64)
usec_per_day = int(1e6) * 86400 # 每天 86.4k 秒,每秒 1e6 微秒
# 计算从第一天开始的微秒偏移
usec_offsets = day_offsets * usec_per_day
# 计算时间戳
timestamps = startdate + usec_offsets

# 在布尔数组中标记月初
is_start_of_month = np.zeros_like(timestamps, dtype=bool)
for index, timestamp in np.ndenumerate(timestamps):
    is_start_of_month[index[0]] = timestamp.astype(datetime).day == 1

使用此方法,timestamps 将是每天的开始

numpy_datetime64_output.txt
array(['2022-01-01T00:00:00.000000', '2022-01-02T00:00:00.000000',
       '2022-01-03T00:00:00.000000', ..., '2101-12-30T00:00:00.000000',
       '2101-12-31T00:00:00.000000', '2102-01-01T00:00:00.000000'],
      dtype='datetime64[us]')

Check out similar posts by category: Data Science, Python