使用 python pandas 读取 LabVIEW TDMS 文件

LabVIEW TDMS file read with python pandas

如何使用 python 读取标准 labVIEW 生成的 TDMS 文件?

为了社区的利益,发布示例代码库,我曾使用它有效地将 *.tdms 文件读入 pandas 数据帧。经过多次试验简化了代码以方便使用和文档。

#import required libraries
from nptdms import TdmsFile
import numpy as np
import pandas as pd

#bokeh plots
from bokeh.plotting import figure, output_file, show
from bokeh.io import output_notebook

#load the tdms file
tdms_file = TdmsFile("/Volumes/Data/dummy/sample.tdms")

#split all the tdms grouped channels to a separate dataframe

#tdms_file.as_dataframe()
for group in tdms_file.groups():
    grp1_data = tdms_file.object('grp1').as_dataframe()
    grp2_data = tdms_file.object('grp2').as_dataframe()

#plot the data on bokeh plots
# Use Bokeh chart to make plot
p = bokeh.charts.Line(grp1_data, x='time', y='values', color='parameter', xlabel='time (h)', ylabel='values')

# Display it
bokeh.io.show(p)

欢迎提出建议和改进。

为清楚起见,我将 Sundar 的回答进一步简化为:

from nptdms import TdmsFile

tdms_file = TdmsFile(r"path_to_.tdms")

for group in tdms_file.groups():
    df = tdms_file.object(group).as_dataframe()

    print(df.head())
    print(df.keys())
    print(df.shape)

这会将 tdms 的不同组读入 pandas 数据帧。

这对我有用:

import pandas as pd
from nptdms import TdmsFile

tdms_file = TdmsFile("path/to/tdms_file.tdms")

df = tdms_file['group'].as_dataframe()

print(df.head())
print(df.keys())
print(df.shape)

npTDMS 版本 1.1.0 至少没有任何 object 用于 TdmsFile 对象的方法,这在前面的示例中使用过。

Joris 和 ax7ster 给出的答案组合 -- 对于 npTMDS v1.3.1。

import nptdms
from nptdms import TdmsFile

print(nptdms.__version__)

fn = 'foo.tdms'
tdms_file = TdmsFile(fn)

for group in tdms_file.groups():
    df = group.as_dataframe()
  
    print(group.name)
    print(df.head())
    print(df.keys())
    print(df.shape)

这会读取 TDMS 文件中的所有组,并且不需要事先知道组名。

也可以将整个TDMS文件转换成一个DataFrame,见下面的例子。

from nptdms import TdmsFile

fn = 'foo.tdms'
tdms_file = TdmsFile(fn)

df = tdms_file.as_dataframe()