使用列表理解将 Numpy 数组值转换为日期时间值 Python
Turning Numpy array values to datetime values with List comprehensions Python
我正在尝试编写一个列表推导式,它通过 ts
数组,然后将其转换为可读的时间戳。但是 dates
列表理解是错误的,我该如何修复它并获得下面的预期输出?
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')]
错误:
dates=[x for x in ts if ts > 0 datetime.utcfromtimestamp(x).strftime('%Y-%m-%d %H:%M:%S')]
^
SyntaxError: invalid syntax
预期输出:
[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]
您将 ts
迭代为 x
以签入 if
您应该检查 x
而不是检查 ts
和 for
的结尾和 if
在检查条件时,您需要在要创建的列表的第一个中写入 datetime.utcfromtimestamp(x).strftime('%Y-%m-%d %H:%M:%S')
。
试试这个:
from datetime import datetime
import numpy as np
ts = np.array([1628997394, 1628997444, 1628997602, 1629006977, 1629007021])
dates=[datetime.utcfromtimestamp(x).strftime('%Y-%m-%d %H:%M:%S') for x in ts if x>0]
输出:
['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']
为了检查运行时两种解决方案,您可以使用 %timeit
并查看何时可以使用 list
完成您的工作 不要使用 numpy
或 pandas
因为使用附加库不好。 (对于这个短数组,使用list
比使用pandas
快10倍)
运行时间:
ts = np.array([1628997394, 1628997444, 1628997602, 1629006977, 1629007021])
%timeit dates=[datetime.utcfromtimestamp(x).strftime('%Y-%m-%d %H:%M:%S') for x in ts if x>0]
# 27.1 µs ± 899 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)
%timeit pd.to_datetime(ts, unit='s', errors='coerce').dropna().astype(str).to_numpy()
# 708 µs ± 128 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
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)
>>>
import numpy as np
import pandas as pd
ts = np.array([1628997394, 1628997444, 1628997602, 1629006977, 1629007021])
ts1 = pd.to_datetime(ts, unit='s', errors= 'coerce')
ts1
解决方案:
DatetimeIndex(['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='datetime64[ns]', freq=None)
我正在尝试编写一个列表推导式,它通过 ts
数组,然后将其转换为可读的时间戳。但是 dates
列表理解是错误的,我该如何修复它并获得下面的预期输出?
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')]
错误:
dates=[x for x in ts if ts > 0 datetime.utcfromtimestamp(x).strftime('%Y-%m-%d %H:%M:%S')]
^
SyntaxError: invalid syntax
预期输出:
[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]
您将 ts
迭代为 x
以签入 if
您应该检查 x
而不是检查 ts
和 for
的结尾和 if
在检查条件时,您需要在要创建的列表的第一个中写入 datetime.utcfromtimestamp(x).strftime('%Y-%m-%d %H:%M:%S')
。
试试这个:
from datetime import datetime
import numpy as np
ts = np.array([1628997394, 1628997444, 1628997602, 1629006977, 1629007021])
dates=[datetime.utcfromtimestamp(x).strftime('%Y-%m-%d %H:%M:%S') for x in ts if x>0]
输出:
['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']
为了检查运行时两种解决方案,您可以使用 %timeit
并查看何时可以使用 list
完成您的工作 不要使用 numpy
或 pandas
因为使用附加库不好。 (对于这个短数组,使用list
比使用pandas
快10倍)
运行时间:
ts = np.array([1628997394, 1628997444, 1628997602, 1629006977, 1629007021])
%timeit dates=[datetime.utcfromtimestamp(x).strftime('%Y-%m-%d %H:%M:%S') for x in ts if x>0]
# 27.1 µs ± 899 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)
%timeit pd.to_datetime(ts, unit='s', errors='coerce').dropna().astype(str).to_numpy()
# 708 µs ± 128 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
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)
>>>
import numpy as np
import pandas as pd
ts = np.array([1628997394, 1628997444, 1628997602, 1629006977, 1629007021])
ts1 = pd.to_datetime(ts, unit='s', errors= 'coerce')
ts1
解决方案:
DatetimeIndex(['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='datetime64[ns]', freq=None)