NumPy-Array mit einem datetime64 pro Tag erstellen und Monatsanfang markieren
English
Deutsch
numpy_datetime64_array.py
startdate = np.datetime64('2022-01-01T00:00:00.000000')
# 10000 Tage. Dieses Array enthält 0 (Tag 0), 1 (Tag 1), usw.
day_offsets = np.arange(10000, dtype=np.int64)
usec_per_day = int(1e6) * 86400 # 86.4k sec per day, 1e6 microseconds per second
# Mikrosekunden-Offset vom ersten Tag berechnen
usec_offsets = day_offsets * usec_per_day
# Zeitstempel berechnen
timestamps = startdate + usec_offsets
# Monatsanfang in booleschem Array markieren
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 == 1Mit dieser Methode ist timestamps der Beginn jedes Tages
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
If this post helped you, please consider buying me a coffee or donating via PayPal to support research & publishing of new posts on TechOverflow