Skip to content
Advertisement

how do i remove rows from numpy array based on date?

i have a number of arrays with the following format:

JavaScript

how do i remove the rows where the datetime > 2021-05-06 09:20 and < 2021-05-06 09:40 ?

I have tried with np.delete:

JavaScript

and np.where:

JavaScript

but always get the error:

SyntaxError: leading zeros in decimal integer literals are not permitted; use an 0o prefix for octal integers

Edit in response to comments:

for ease I have created the datetime objects with

JavaScript

and incorporated this into Ben Grossman’s solution:

JavaScript

which gives a type error ‘>’ not supported between instances of ‘str’ and ‘datetime.datetime’. I understand this as column 0 is a string, so I tried

JavaScript

which just replicates raindata[10].

Ben also asked for code which replicates the array, I’m sorry but the array is not generated by code, it is data imported from a PGSQL table, which is why i copied the first 11 rows as an example.

Advertisement

Answer

This has been solved using numpy:-

set the date range:

JavaScript

create a mask:

JavaScript

create the new array:

JavaScript

job done :)

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement