Hannah Tang
Hannah Tang

Reputation: 41

Converting UNIX timestamp to date?

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!

Upvotes: 2

Views: 785

Answers (2)

BenT
BenT

Reputation: 3200

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)

Upvotes: 1

ImportanceOfBeingErnest
ImportanceOfBeingErnest

Reputation: 339705

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.

Upvotes: 4

Related Questions