繁体   English   中英

csv中的上采样浮点第二系列(使用熊猫)?

[英]Upsample floating point second series in csv (with Pandas)?

我知道 Pandas 可以进行重采样,也可以用于具有浮点数时间戳索引的数据: Pandas - Resampling and Interpolation with time float64

但是,我不确定如何将其应用于我的问题 - 我的数据有一个时间戳列,它是一个浮点数,表示秒; 这是test.csv

Time[s], Channel 0
0.000000000000000, -0.736680805683136
0.000008000000000, -0.726485192775726
0.000016000000000, -0.721387386322021
0.000024000000000, -0.711191773414612
0.000032000000000, -0.700996160507202
0.000040000000000, -0.690800547599792
0.000048000000000, -0.670409321784973
0.000056000000000, -0.655115902423859
0.000064000000000, -0.629626870155334
0.000072000000000, -0.604137837886810

我可以使用以下test.py将其读入熊猫:

#!/usr/bin/env python3

import sys, os
import pandas as pd

df_data = pd.read_csv("test.csv")
print(df_data)

该数据以 125 KHz 采样,这意味着采样周期为 1/125e3 = 8e-06,或 8 μs(这在时间戳中可见,因为它们是统一的)。

现在,我想将其重新采样到 500 kHz - 即速率的四倍 - 但是,没有线性插值; 也就是说,每个样本只会在新的采样周期重复,8/4=2 μs; 所以我想要一个输出:

0.000000000000000, -0.736680805683136
0.000002000000000, -0.736680805683136
0.000004000000000, -0.736680805683136
0.000006000000000, -0.736680805683136
0.000008000000000, -0.726485192775726
...

我可以使用 Pandas 获得这个,如果可以,如何获得? 并且更完整 - 我如何在“无插值”与“线性插值”之间进行选择?

好的,看起来我找到了如何在没有插值的情况下做到这一点,但我仍然不确定如何使用插值来做到这一点。

无论如何,解决方案代码是:

#!/usr/bin/env python3

import sys, os
import pandas as pd

df_data = pd.read_csv("test.csv")
print(df_data)
df_data.index = pd.to_timedelta(df_data.iloc[:,0], unit='s')
print(df_data)
df_resampled = df_data.resample('2us').pad()
print("-----")
print(df_resampled)
df_resampled['Time[s]'] = df_resampled.index.total_seconds()
print("--***--")
print(df_resampled)

所以,诀窍是——首先你需要添加一个“timedelta”索引; 请注意, 链接的帖子复制了原始索引(使用df.index = pd.to_timedelta(df.index, unit='ms') ); 但在这里我们已经在第 0 列中获得了计时数据 - 因此我们将其解释为秒,并将其存储为“timedelta”索引,使用df_data.index = pd.to_timedelta(df_data.iloc[:,0], unit='s')

第二个棘手的事情是df_data.resample('2us')实际上返回一个TimedeltaIndexResampler [freq=<2 * Micros>, axis=0, closed=left, label=left, convention=start, base=0] ,我无法弄清楚如何从中获取表格数据。 但看起来,只要你调用.pad()方法, 文档就会澄清:“使用 pad 方法填充 NaN 值”,然后我得到表格数据。

最后,我有一个带有正确时间戳和数据的 timedelta 索引 - 但我仍然需要将 timedelta 索引转换回浮点秒,这可以通过df_resampled['Time[s]'] = df_resampled.index.total_seconds()

脚本的输出是:

$ python3 test.py
    Time[s]   Channel 0
0  0.000000   -0.736681
1  0.000008   -0.726485
2  0.000016   -0.721387
3  0.000024   -0.711192
4  0.000032   -0.700996
5  0.000040   -0.690801
6  0.000048   -0.670409
7  0.000056   -0.655116
8  0.000064   -0.629627
9  0.000072   -0.604138
                  Time[s]   Channel 0
Time[s]
00:00:00         0.000000   -0.736681
00:00:00.000008  0.000008   -0.726485
00:00:00.000016  0.000016   -0.721387
00:00:00.000024  0.000024   -0.711192
00:00:00.000032  0.000032   -0.700996
00:00:00.000040  0.000040   -0.690801
00:00:00.000048  0.000048   -0.670409
00:00:00.000056  0.000056   -0.655116
00:00:00.000064  0.000064   -0.629627
00:00:00.000072  0.000072   -0.604138
-----
                  Time[s]   Channel 0
Time[s]
00:00:00         0.000000   -0.736681
00:00:00.000002  0.000000   -0.736681
00:00:00.000004  0.000000   -0.736681
00:00:00.000006  0.000000   -0.736681
00:00:00.000008  0.000008   -0.726485
00:00:00.000010  0.000008   -0.726485
00:00:00.000012  0.000008   -0.726485
00:00:00.000014  0.000008   -0.726485
00:00:00.000016  0.000016   -0.721387
00:00:00.000018  0.000016   -0.721387
00:00:00.000020  0.000016   -0.721387
00:00:00.000022  0.000016   -0.721387
00:00:00.000024  0.000024   -0.711192
00:00:00.000026  0.000024   -0.711192
00:00:00.000028  0.000024   -0.711192
00:00:00.000030  0.000024   -0.711192
00:00:00.000032  0.000032   -0.700996
00:00:00.000034  0.000032   -0.700996
00:00:00.000036  0.000032   -0.700996
00:00:00.000038  0.000032   -0.700996
00:00:00.000040  0.000040   -0.690801
00:00:00.000042  0.000040   -0.690801
00:00:00.000044  0.000040   -0.690801
00:00:00.000046  0.000040   -0.690801
00:00:00.000048  0.000048   -0.670409
00:00:00.000050  0.000048   -0.670409
00:00:00.000052  0.000048   -0.670409
00:00:00.000054  0.000048   -0.670409
00:00:00.000056  0.000056   -0.655116
00:00:00.000058  0.000056   -0.655116
00:00:00.000060  0.000056   -0.655116
00:00:00.000062  0.000056   -0.655116
00:00:00.000064  0.000064   -0.629627
00:00:00.000066  0.000064   -0.629627
00:00:00.000068  0.000064   -0.629627
00:00:00.000070  0.000064   -0.629627
00:00:00.000072  0.000072   -0.604138
--***--
                  Time[s]   Channel 0
Time[s]
00:00:00         0.000000   -0.736681
00:00:00.000002  0.000002   -0.736681
00:00:00.000004  0.000004   -0.736681
00:00:00.000006  0.000006   -0.736681
00:00:00.000008  0.000008   -0.726485
00:00:00.000010  0.000010   -0.726485
00:00:00.000012  0.000012   -0.726485
00:00:00.000014  0.000014   -0.726485
00:00:00.000016  0.000016   -0.721387
00:00:00.000018  0.000018   -0.721387
00:00:00.000020  0.000020   -0.721387
00:00:00.000022  0.000022   -0.721387
00:00:00.000024  0.000024   -0.711192
00:00:00.000026  0.000026   -0.711192
00:00:00.000028  0.000028   -0.711192
00:00:00.000030  0.000030   -0.711192
00:00:00.000032  0.000032   -0.700996
00:00:00.000034  0.000034   -0.700996
00:00:00.000036  0.000036   -0.700996
00:00:00.000038  0.000038   -0.700996
00:00:00.000040  0.000040   -0.690801
00:00:00.000042  0.000042   -0.690801
00:00:00.000044  0.000044   -0.690801
00:00:00.000046  0.000046   -0.690801
00:00:00.000048  0.000048   -0.670409
00:00:00.000050  0.000050   -0.670409
00:00:00.000052  0.000052   -0.670409
00:00:00.000054  0.000054   -0.670409
00:00:00.000056  0.000056   -0.655116
00:00:00.000058  0.000058   -0.655116
00:00:00.000060  0.000060   -0.655116
00:00:00.000062  0.000062   -0.655116
00:00:00.000064  0.000064   -0.629627
00:00:00.000066  0.000066   -0.629627
00:00:00.000068  0.000068   -0.629627
00:00:00.000070  0.000070   -0.629627
00:00:00.000072  0.000072   -0.604138

暂无
暂无

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

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