tony selcuk
tony selcuk

Reputation: 687

Filtering out date array in Numpy Python

dates, and values are corelated. I am trying to code a function where it filters out all the values in the values array after the given_date. Since the given_date is '2017-09-15 11:40:30', The first two indexes in dates '2017-09-15 07:11:00' ,'2017-09-15 11:25:30' comes before the given_date so the first tow indexes in values is filtered out. Resulting in the Expected Output. How would I be able to do that?

given_date= '2017-09-15 11:40:30'
dates= np.array(['2017-09-15 07:11:00' ,'2017-09-15 11:25:30', '2017-09-15 12:11:10', '2021-04-07 22:43:12', '2021-04-08 00:49:18'])
values= np.array([10,30,44,55,60])

Expected Output:

[44,55,60]

Upvotes: 0

Views: 353

Answers (1)

ForceBru
ForceBru

Reputation: 44838

These dates are actually strings, so you should treat them as dates:

import numpy as np

given_date = np.datetime64('2017-09-15 11:40:30')
dates = np.array([
    '2017-09-15 07:11:00' ,'2017-09-15 11:25:30', '2017-09-15 12:11:10', '2021-04-07 22:43:12', '2021-04-08 00:49:18'
], np.datetime64)
values = np.array([10,30,44,55,60])

# Use comparison operators like you would with numbers
print(values[dates > given_date]) 

Upvotes: 3

Related Questions