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)
- According to the documentation, doesn't dt.datetime.fromtimestamp need 1 argument? No argument was passed here though.
- 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.
- 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!
mapwhich accepts an array as input. Becausedatetime.datetime.fromtimestampaccepts only a single input, you have to either use the function in a loop, usemapor np.vectorize to process an array. If you are using pandas, you can useapply(datetime.datetime.fromtimestamp)on a dataframe.dt.datetime.fromtimestampinstead ofdt.datetime.fromtimestamp()allows to pass the functiondt.datetime.fromtimestampinto the functionnp.vectorize.