我希望通过指定以下输入来创建一个Python列表,其中包含具有毫秒分辨率的等距时间戳的范围:
dd/mm/yyyy hh:MM:ss.sss格式开始日期。dd/mm/yyyy hh:MM:ss.sss格式结束日期。怎么做呢?
发布于 2022-07-05 07:47:02
您可以使用Pandas .date_range()函数(请参阅docs 这里),因为它通过指定start、end和freq返回等距时间点的范围。参见下面的示例(注意,开始日期和结束日期最初是字符串):
# Import the required libraries
from datetime import datetime
import pandas as pd
# Select your inputs
start_date_string = "14/10/2021 17:37:19.000" # String
end_date_string = "14/10/2021 17:38:20.000" # String
frequency = 64 # In Hz
# Convert the strings of dates into the Python datetime format
start_datetime = datetime.strptime(start_date_string, '%d/%m/%Y %H:%M:%S.%f')
end_datetime = datetime.strptime(end_date_string, '%d/%m/%Y %H:%M:%S.%f')
# Create a range of dates
index = pd.date_range(start = start_datetime, end = end_datetime,
freq="{}".format(1000/frequency)+"L")这个答案的关键是freq函数中的.date_range()参数,因为它选择返回数据的频率(参见这里获取频率别名列表)。由于我们的频率是以赫兹指定的,所以需要做一些精细的调整,但这可以用freq="{}".format(1000/frequency)+"L"完美地完成。
其中index是我们所需的输出:
DatetimeIndex([ '2021-10-14 17:37:19', '2021-10-14 17:37:19.015625',
'2021-10-14 17:37:19.031250', '2021-10-14 17:37:19.046875',
'2021-10-14 17:37:19.062500', '2021-10-14 17:37:19.078125',
'2021-10-14 17:37:19.093750', '2021-10-14 17:37:19.109375',
'2021-10-14 17:37:19.125000', '2021-10-14 17:37:19.140625',
...
'2021-10-14 17:38:19.859375', '2021-10-14 17:38:19.875000',
'2021-10-14 17:38:19.890625', '2021-10-14 17:38:19.906250',
'2021-10-14 17:38:19.921875', '2021-10-14 17:38:19.937500',
'2021-10-14 17:38:19.953125', '2021-10-14 17:38:19.968750',
'2021-10-14 17:38:19.984375', '2021-10-14 17:38:20'],
dtype='datetime64[ns]', length=3905, freq='15625U')从这里开始,您可以使用index.tolist()继续将其转换为列表。
https://stackoverflow.com/questions/72865760
复制相似问题