简体   繁体   English

从二进制序列到时间序列频率

[英]From binary sequence to time series frequency

I have data-set which contains data from presence sensors which are stored as binary sequence in a frequency of 1 hour. 我有一个数据集,其中包含来自存在传感器的数据,这些数据以二进制序列的形式存储在1小时的频率中。 Sensor checks the presence every 2 minutes and stores it in binary sequence every hour(0 for no presence detected, 1 for presence detected), which means one data-point has 30 digits. 传感器每2分钟检查一次存在状态,并每小时以二进制顺序存储它(0表示未检测到存在,1表示检测到存在),这意味着一个数据点有30位数字。 Data-point for one sensor for one hour looks like this 001111110111100000001000000100. 一个传感器一小时的数据点如下所示:001111110111100000001000000100。

I would like to create dataframe which has frequency of 2 minutes and for each sensor, column which indicates presence or no presence (1 or 0). 我想创建一个频率为2分钟的数据框,对于每个传感器,该列表示存在或不存在(1或0)。

Any ideas how to do it? 有什么想法怎么做?

MACAddress          f8:f0:05:d0:ee:29              f8:f0:05:d0:f1:0b                f8:f0:05:d0:f1:1d   
2019-04-02 09:00:00 100100000000000000000000000000 100000000000001111111111010111   111111110111111111111110110000
2019-04-02 09:00:00 001110110000000000000000011111 111110000000000000111111110100 111010110011111111111011111111

Thanks 谢谢

Here is my proposal, hope it helps: 这是我的建议,希望能对您有所帮助:

Build some sample data: 建立一些样本数据:

dd = {'f8:f0:05:d0:ee:29': ['100100000000000000000000000000', '001110110000000000000000011111'],
      'f8:f0:05:d0:f1:0b': ['100000000000001111111111010111', '111110000000000000111111110100'],
      'f8:f0:05:d0:f1:1d': ['111111110111111111111110110000', '001110110000000000000000011111']}
df = pd.DataFrame(dd, index=['2019-04-02 09:00:00', '2019-04-02 10:00:00'])
df.index = pd.to_datetime(df.index)

Solution: 解:

expanded = df.apply(lambda series: series.apply(lambda x: pd.Series(list(x))).stack())
expanded.index = expanded.index.get_level_values(0) + (expanded.index.get_level_values(1)*2).astype('timedelta64[m]')

I don't see a way to do it using the vectorized functions, which means you will have to do it using something normally to be avoided, iterating manually over the rows using df.iterrows . 我没有看到使用矢量化函数来完成此操作的方法,这意味着您必须使用通常可以避免的操作来完成此操作,请使用df.iterrows在行上手动进行df.iterrows

import pandas as pd

def get_subtimes(df):
    previous = df.index[0] - (df.index[1] - df.index[0])
    for index, row in df.iterrows():
        yield pd.DataFrame(row.apply(lambda x: list(map(int, x))).to_dict(),
                           index=pd.date_range(previous, index, freq="2T")[1:])
        previous = index


df = pd.DataFrame([["100100000000000000000000000000", "100000000000001111111111010111", "111111110111111111111110110000"],
                   ["001110110000000000000000011111", "111110000000000000111111110100", "111010110011111111111011111111"]],
                  columns=["f8:f0:05:d0:ee:29", "f8:f0:05:d0:f1:0b", "f8:f0:05:d0:f1:1d"],
                  index=pd.to_datetime(["2019-04-02 09:00:00", "2019-04-02 10:00:00"]))

print(pd.concat(get_subtimes(df)))

Result: 结果:

                     f8:f0:05:d0:ee:29  f8:f0:05:d0:f1:0b  f8:f0:05:d0:f1:1d
2019-04-02 08:02:00                  1                  1                  1
2019-04-02 08:04:00                  0                  0                  1
2019-04-02 08:06:00                  0                  0                  1
2019-04-02 08:08:00                  1                  0                  1
2019-04-02 08:10:00                  0                  0                  1
2019-04-02 08:12:00                  0                  0                  1
2019-04-02 08:14:00                  0                  0                  1
2019-04-02 08:16:00                  0                  0                  1
2019-04-02 08:18:00                  0                  0                  0
2019-04-02 08:20:00                  0                  0                  1
2019-04-02 08:22:00                  0                  0                  1
2019-04-02 08:24:00                  0                  0                  1
2019-04-02 08:26:00                  0                  0                  1
2019-04-02 08:28:00                  0                  0                  1
2019-04-02 08:30:00                  0                  1                  1
2019-04-02 08:32:00                  0                  1                  1
2019-04-02 08:34:00                  0                  1                  1
2019-04-02 08:36:00                  0                  1                  1
2019-04-02 08:38:00                  0                  1                  1
2019-04-02 08:40:00                  0                  1                  1
2019-04-02 08:42:00                  0                  1                  1
2019-04-02 08:44:00                  0                  1                  1
2019-04-02 08:46:00                  0                  1                  1
2019-04-02 08:48:00                  0                  1                  0
2019-04-02 08:50:00                  0                  0                  1
2019-04-02 08:52:00                  0                  1                  1
2019-04-02 08:54:00                  0                  0                  0
2019-04-02 08:56:00                  0                  1                  0
2019-04-02 08:58:00                  0                  1                  0
2019-04-02 09:00:00                  0                  1                  0
2019-04-02 09:02:00                  0                  1                  1
2019-04-02 09:04:00                  0                  1                  1
2019-04-02 09:06:00                  1                  1                  1
2019-04-02 09:08:00                  1                  1                  0
2019-04-02 09:10:00                  1                  1                  1
2019-04-02 09:12:00                  0                  0                  0
2019-04-02 09:14:00                  1                  0                  1
2019-04-02 09:16:00                  1                  0                  1
2019-04-02 09:18:00                  0                  0                  0
2019-04-02 09:20:00                  0                  0                  0
2019-04-02 09:22:00                  0                  0                  1
2019-04-02 09:24:00                  0                  0                  1
2019-04-02 09:26:00                  0                  0                  1
2019-04-02 09:28:00                  0                  0                  1
2019-04-02 09:30:00                  0                  0                  1
2019-04-02 09:32:00                  0                  0                  1
2019-04-02 09:34:00                  0                  0                  1
2019-04-02 09:36:00                  0                  0                  1
2019-04-02 09:38:00                  0                  1                  1
2019-04-02 09:40:00                  0                  1                  1
2019-04-02 09:42:00                  0                  1                  1
2019-04-02 09:44:00                  0                  1                  0
2019-04-02 09:46:00                  0                  1                  1
2019-04-02 09:48:00                  0                  1                  1
2019-04-02 09:50:00                  0                  1                  1
2019-04-02 09:52:00                  1                  1                  1
2019-04-02 09:54:00                  1                  0                  1
2019-04-02 09:56:00                  1                  1                  1
2019-04-02 09:58:00                  1                  0                  1
2019-04-02 10:00:00                  1                  0                  1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM