-
Notifications
You must be signed in to change notification settings - Fork 0
/
Get_datetime_index.py
executable file
·52 lines (27 loc) · 1.27 KB
/
Get_datetime_index.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import numpy as np
import datetime
from datetime import timedelta
def Get_datetime_index(datetime_array, specific_datetime) :
"""
Finds the index of the datetime closest to a specific datetime.
Parameters :
datetime_array (array) : One dimensional array filled with datetimes.
specific_datetime (datetime) : Specific datetime which must be matched as closely as possible.
Returns :
datetime_index (int) : Index of the datetime closest to specific_datetime.
Author : Élise Comeau
Created : August 4th 2021
Last modified : August 4th 2021
"""
min_datetime_difference = abs( datetime_array[0] - specific_datetime )
min_datetime = datetime_array[0]
amount_of_datetimes = datetime_array.shape[0]
if ( amount_of_datetimes > 1 ) :
for datetime in datetime_array :
datetime_difference = abs( datetime - specific_datetime )
if ( datetime_difference < min_datetime_difference ) :
min_datetime_difference = datetime_difference
min_datetime = datetime
datetime_indexes = np.where(datetime_array == min_datetime)
datetime_index = int( datetime_indexes[0][0] )
return datetime_index