Search code examples
pythonarraysnumpydatetimelist-comprehension

Turning Numpy array values to datetime values with List comprehensions Python


I am trying to write a list comprehension where it goes through the ts array and then turn it into readable timestamps. However the dates list comprehension is faulty how would I be able to fix it and get the Expected Output below?

from datetime import datetime
import numpy as np 

ts = np.array([1628997394, 1628997444, 1628997602, 1629006977, 1629007021])
# if you encounter a "year is out of range" error the timestamp
# may be in milliseconds, try `ts /= 1000` in that case
dates=[x for x in ts if ts > 0 datetime.utcfromtimestamp(x).strftime('%Y-%m-%d %H:%M:%S')]

Error:

    dates=[x for x in ts if ts > 0 datetime.utcfromtimestamp(x).strftime('%Y-%m-%d %H:%M:%S')]
                                          ^
SyntaxError: invalid syntax

Expected Output:

[2021-08-15 03:16:34 , 2021-08-15 03:17:24, 2021-08-15 03:20:02 , 2021-08-15 05:56:17 , 2021-08-15 05:57:01]

Solution

  • It could be done more efficiently with pandas:

    >>> import pandas as pd
    >>> ts = np.array([1628997394, 1628997444, 1628997602, 1629006977, 1629007021])
    >>> pd.to_datetime(ts, unit='s', errors='coerce').dropna().astype(str).to_numpy()
    array(['2021-08-15 03:16:34', '2021-08-15 03:17:24',
           '2021-08-15 03:20:02', '2021-08-15 05:56:17',
           '2021-08-15 05:57:01'], dtype=object)
    >>>