根据开始的unix时期向数据添加时间戳

时间:2019-01-15 12:10:24

标签: python pandas

我有一些加速度计数据以10Hz采样,在传输开始时我只收到unix时间戳,而其余的读数却没有任何时间戳。

1540987236
0   8604,2152,14660
1   8588,2168,14712
2   8558,2180,14674
3   8586,2220,14676
4   8586,2216,14712
5   8632,2230,14656
6   8592,2182,14716
7   8592,2172,14644
8   8588,2190,14672
9   8608,2226,14682
10  8618,2210,14620
11  8616,2204,14580
12  8590,2194,14658
13  8580,2240,14714
14  8588,2234,14710
15  8608,2226,14682
16  8618,2210,14620
17  8616,2204,14580
18  8590,2194,14658
19  8580,2240,14714
20  8588,2234,14710
21  8608,2226,14682
22  8618,2210,14620
23  8616,2204,14580
24  8590,2194,14658
25  8580,2240,14714
26  8588,2234,14710
27  8608,2226,14682
28  8618,2210,14620
29  8616,2204,14580
30  8590,2194,14658
31  8580,2240,14714
32  8588,2234,14710

我在这里想要做的是在单独的一列中将1540987236添加到前9个读数中,将unix时间戳增加1秒1540987237并将此新时间戳添加到接下来的10个读数中以此类推以获取3000个读数(5分钟的加速度计数据)。

我该如何在熊猫中实现这一目标?我基本上需要一个序列,该序列具有准确的10个时间戳(以微秒为单位),平均分布在2秒之间。

2 个答案:

答案 0 :(得分:0)

我认为您需要用10进行整数除法并添加unix时间戳:

#default RangeIndex
df['dates'] = df.index // 10 + 1540987236

#general index
df['dates'] = np.arange(len(df.index)) // 10 + 1540987236

答案 1 :(得分:0)

使用datetime,您可以编写如下内容:

#!/usr/bin/python3    
import pandas as pd
import datetime
import numpy as np

start = datetime.datetime.fromtimestamp(1540987236)
delta = datetime.timedelta(seconds=1)

df = pd.DataFrame({'a': np.random.randn(50)})

df['timestamp'] = [(i // 10 * delta + start).timestamp()
                   for i in range(len(df['a']))]
df['timestamp_readable'] = [str(i // 10 * delta + start)
                            for i in range(len(df['a']))]

df将如下所示:

               a     timestamp   timestamp_readable
0  -0.600697  1.540987e+09  2018-10-31 13:00:36
1  -1.480723  1.540987e+09  2018-10-31 13:00:36
2   0.662514  1.540987e+09  2018-10-31 13:00:36
3   0.006153  1.540987e+09  2018-10-31 13:00:36
4  -1.305551  1.540987e+09  2018-10-31 13:00:36
5   0.204650  1.540987e+09  2018-10-31 13:00:36
6   0.264038  1.540987e+09  2018-10-31 13:00:36
7  -1.182569  1.540987e+09  2018-10-31 13:00:36
8  -1.339297  1.540987e+09  2018-10-31 13:00:36
9  -0.079093  1.540987e+09  2018-10-31 13:00:36
10 -0.804870  1.540987e+09  2018-10-31 13:00:37
11  0.238080  1.540987e+09  2018-10-31 13:00:37
12  0.647706  1.540987e+09  2018-10-31 13:00:37
13  0.578003  1.540987e+09  2018-10-31 13:00:37
14  0.972857  1.540987e+09  2018-10-31 13:00:37
15  0.414544  1.540987e+09  2018-10-31 13:00:37
16  0.127489  1.540987e+09  2018-10-31 13:00:37
17  0.942130  1.540987e+09  2018-10-31 13:00:37
18 -0.871946  1.540987e+09  2018-10-31 13:00:37
19  0.347854  1.540987e+09  2018-10-31 13:00:37
20  0.986753  1.540987e+09  2018-10-31 13:00:38
21 -0.489212  1.540987e+09  2018-10-31 13:00:38
22 -0.814278  1.540987e+09  2018-10-31 13:00:38
23  0.396447  1.540987e+09  2018-10-31 13:00:38
24  0.642809  1.540987e+09  2018-10-31 13:00:38
25 -0.209376  1.540987e+09  2018-10-31 13:00:38
26  0.884612  1.540987e+09  2018-10-31 13:00:38
27 -0.565565  1.540987e+09  2018-10-31 13:00:38
28 -1.460945  1.540987e+09  2018-10-31 13:00:38
29 -0.541121  1.540987e+09  2018-10-31 13:00:38
30 -0.125047  1.540987e+09  2018-10-31 13:00:39
31 -0.421019  1.540987e+09  2018-10-31 13:00:39
32 -0.333257  1.540987e+09  2018-10-31 13:00:39
...