上传时间,同时保持python中的原始点值

时间:2018-04-09 15:53:00

标签: python signals sampling resampling

我有一个任意大小的不均匀间隔时间(例如7),见下文,我想上传(50)并保留原始点。

ls = ['2016-01-30 12:10:00', 
  '2016-01-30 12:23:35', 
  '2016-01-30 12:24:14', 
  '2016-01-30 12:24:51', 
  '2016-01-30 12:25:00', 
  '2016-01-30 12:26:49', 
  '2016-01-30 12:27:36']

在存储新时间戳的哪些点之间基本无关紧要,但优选地根据时间间隔。因此,时间间隔越大,新生成的时间戳就越多,例如

new_ls = ['2016-01-30 12:10:00',
      x,
      x,
      x,
      x,
  '2016-01-30 12:23:35',
      x,
      x,
  '2016-01-30 12:24:14',
  '2016-01-30 12:24:51',
  '2016-01-30 12:25:00',
      x,
  '2016-01-30 12:26:49',
  '2016-01-30 12:27:36']

结果列表也可能是不均匀的。 提前致谢

1 个答案:

答案 0 :(得分:1)

首先将您的列表转换为pd.DatetimeIndex,然后在开始和结束之间找到理想的时间戳分布:

import pandas as pd
import numpy as np

ls = pd.to_datetime(['2016-01-30 12:10:00', 
                     '2016-01-30 12:23:35', 
                     '2016-01-30 12:24:14', 
                     '2016-01-30 12:24:51', 
                     '2016-01-30 12:25:00', 
                     '2016-01-30 12:26:49', 
                     '2016-01-30 12:27:36'])

n = 50
dt = (ls.max() - ls.min())/n
ls_temp = pd.date_range(start=ls.min(), end=ls.max(), freq=dt)

然后找出放置原始测量值的位置,例如通过替换具有最小绝对差值的条目:

idx = np.abs(ls[:,None] - ls_temp[None, :]).argmin(axis=1)
ls_temp = pd.Series(ls_temp)
ls_temp[idx] = pd.Series(ls)

请注意,在某些边缘情况下(其中一个索引被两个值或类似值替换),此策略可能会失败。

最后,您可以将其转换回格式,例如:

ls = list(map("{:%Y-%m-%d %H:%M:%S}".format, ls_temp.tolist()))

输出:

['2016-01-30 12:10:00',
 '2016-01-30 12:10:21',
 '2016-01-30 12:10:42',
 '2016-01-30 12:11:03',
 '2016-01-30 12:11:24',
 '2016-01-30 12:11:45',
 '2016-01-30 12:12:06',
 '2016-01-30 12:12:27',
 '2016-01-30 12:12:48',
 '2016-01-30 12:13:10',
 '2016-01-30 12:13:31',
 '2016-01-30 12:13:52',
 '2016-01-30 12:14:13',
 '2016-01-30 12:14:34',
 '2016-01-30 12:14:55',
 '2016-01-30 12:15:16',
 '2016-01-30 12:15:37',
 '2016-01-30 12:15:59',
 '2016-01-30 12:16:20',
 '2016-01-30 12:16:41',
 '2016-01-30 12:17:02',
 '2016-01-30 12:17:23',
 '2016-01-30 12:17:44',
 '2016-01-30 12:18:05',
 '2016-01-30 12:18:26',
 '2016-01-30 12:18:48',
 '2016-01-30 12:19:09',
 '2016-01-30 12:19:30',
 '2016-01-30 12:19:51',
 '2016-01-30 12:20:12',
 '2016-01-30 12:20:33',
 '2016-01-30 12:20:54',
 '2016-01-30 12:21:15',
 '2016-01-30 12:21:36',
 '2016-01-30 12:21:58',
 '2016-01-30 12:22:19',
 '2016-01-30 12:22:40',
 '2016-01-30 12:23:01',
 '2016-01-30 12:23:22',
 '2016-01-30 12:23:35',
 '2016-01-30 12:24:14',
 '2016-01-30 12:24:25',
 '2016-01-30 12:24:51',
 '2016-01-30 12:25:00',
 '2016-01-30 12:25:29',
 '2016-01-30 12:25:50',
 '2016-01-30 12:26:11',
 '2016-01-30 12:26:32',
 '2016-01-30 12:26:49',
 '2016-01-30 12:27:14',
 '2016-01-30 12:27:36']

但是,如果要重新采样与这些时间戳关联的数据,最好将其保留为pd.Series并跳过最后一步。最后,你可能想要使用pd.DataFrame.resample