2

I am following a tutorial by sentdex regarding converting UNIX timestamps to matplotlib-recognised dates. I'm still new to this and while I understand the examples given in the documentation since they are pretty simple, I do not understand why np.vectorize is used in this case:

date_convert = np.vectorize(dt.datetime.fromtimestamp)
date = date_convert(date)
  1. According to the documentation, doesn't dt.datetime.fromtimestamp need 1 argument? No argument was passed here though.
  2. What is the point of calling np.vectorize on dt.datetime.fromtimestamp though? Perhaps I still don't quite grasp the concept of this function.
  3. How come date_convert can accept an argument on the second line when it wasn't defined as a function?

Any help would be greatly appreciated! Thank you for the patience!

2
  • 1
    np.vectorize takes a callable function or method and converts it to a new function similar to using map which accepts an array as input. Because datetime.datetime.fromtimestamp accepts only a single input, you have to either use the function in a loop, use map or np.vectorize to process an array. If you are using pandas, you can use apply(datetime.datetime.fromtimestamp) on a dataframe. Commented Aug 6, 2019 at 19:24
  • Note that functions in Python are first-class - they can be passed around, even to other functions. Using dt.datetime.fromtimestamp instead of dt.datetime.fromtimestamp() allows to pass the function dt.datetime.fromtimestamp into the function np.vectorize. Commented Aug 6, 2019 at 19:37

2 Answers 2

4

To make this more transparent, let's create our own vectorizer.

import datetime as dt

data = [1326244364, 1326844366, 1327245361]

def myfromtimestampfunction(timestamp):
    return dt.datetime.fromtimestamp(timestamp)

def myvectorizer(input_func):

    def output_func(array_of_numbers):
        return [input_func(a) for a in array_of_numbers]

    return output_func

date_convert =  myvectorizer(myfromtimestampfunction)
dates = date_convert(data)

print(dates)

myvectorizer takes in a function, and outputs a function. The input function can only take single numbers as input. The output function can take an iterable.

With this it may be easier to understand that

  1. You do not call the input function, you just pass it to the vectorizer.
  2. myfromtimestampfunction(data) would fail, because data is a list, but myfromtimestampfunction expects a single number.
  3. date_convert is a function, namely the output_func from the vectorizer. It hence can take an argument as input.
Sign up to request clarification or add additional context in comments.

Comments

1

What np.vectorize does is basically make the datetime functions usable on arrays instead of single values.

If you use dt.datetime.fromtimestamp you are right that it only takes 1 argument but it also only works on 1 value. So the following example which has 2 dates will bring up an error.

import datetime as dt
import numpy as np

dates = np.array([13444,9000009])
dt.datetime.fromtimestamp(dates)

TypeError: only size-1 arrays can be converted to Python scalars

However using np.vectorize creates a new function called date_convert that does the same thing as dt.datetime.fromtimestamp but allows multiple values like in an array.

date_convert = np.vectorize(dt.datetime.fromtimestamp)

dates = np.array([13444,9000009])
date_convert(dates)

#returns:
#array([datetime.datetime(1969, 12, 31, 20, 44, 4),
#       datetime.datetime(1970, 4, 14, 21, 0, 9)], dtype=object)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.