简体   繁体   English

用python pandas读取LabVIEW TDMS文件

[英]LabVIEW TDMS file read with python pandas

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

For the benefit of the community , posting sample code base i have used to efficiently read *.tdms file into pandas dataframe.为了社区的利益,发布示例代码库,我用来有效地将 *.tdms 文件读入 Pandas 数据帧。 After multiple trials simplified the code for ease of use and documentation.经过多次试验,简化了代码以方便使用和记录。

#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)

Suggestions and improvements are welcome.欢迎提出建议和改进。

For clarity, i would further simplify the answer by Sundar to:为清楚起见,我将 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)

That will read the different groups of the tdms into pandas dataframes.这会将不同组的 tdms 读入 Pandas 数据帧。

This worked for me:这对我有用:

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)

The npTDMS version 1.1.0 at least didn't have any object method for TdmsFile objects that was used in the previous examples here. npTDMS 1.1.0 版至少没有任何用于此处前面示例中使用的 TdmsFile 对象的object方法。

Combination of answers given by Joris and ax7ster -- for npTMDS v1.3.1. 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)

This reads all the groups in the TDMS file and doesn't require group names to be known beforehand.这会读取 TDMS 文件中的所有组,并且不需要事先知道组名。

It also possible to convert the whole TDMS file into one DataFrame, see example below.也可以将整个 TDMS 文件转换为一个 DataFrame,请参见下面的示例。

from nptdms import TdmsFile

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

df = tdms_file.as_dataframe()

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

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