Home > Software engineering >  Why can't I vectorize Python's datetime.utcfromtimestamp()?
Why can't I vectorize Python's datetime.utcfromtimestamp()?

Time:02-09

I have an array of Unix timestamps. I can run datetime.utcfromtimestamp on each entry individually, but I cannot run them all from a numpy array. Why does that happen?

MWE

import numpy as np
from datetime import datetime

times = np.array((1524967210, 1524967211, 1524967212))

# Works
for i in times:
    print(datetime.utcfromtimestamp(times[i]))

# Does not work
print(datetime.utcfromtimestamp(times[:]))

The second example issues: TypeError: only integer scalar arrays can be converted to a scalar index rather than an array of times. A Unix timestamp conversion seems straightforward to implement as a vectorized operation (see source code here), so why does it not work here? Is there something I can do to alter this which will enable vectorized operation?

CodePudding user response:

You can use np.vectorize() to create a vecorized version of the function which does what you want:

import numpy as np
from datetime import datetime

times = np.array((1524967210, 1524967211, 1524967212))
func = np.vectorize(datetime.utcfromtimestamp)

print(func(times[:]))
# or
print(func(times))

CodePudding user response:

times is a Numpy array of the numpy module and datetime.utcfromtimestamp is a function of the datetime module. datetime does not support Numpy array and it does not actually have any dependency with Numpy. In fact, because datetime is a [built-in module] of Python 3, having a dependency with Numpy would result in Numpy being a built-in module too! Numpy is an independent module developped by different people and there is no plan to add it in built-in modules yet. While np.vectorize could be used, it does not truly vectorise a function (it inefficiently calls the CPython function using a loop internally). It is provided for convenience and not speed.

The standard way to manipulate date/time in Numpy is using the datetime64 type.

  •  Tags:  
  • Related