【问题标题】:LabVIEW TDMS file read with python pandas使用 python pandas 读取 LabVIEW TDMS 文件
【发布时间】:2018-06-11 10:09:38
【问题描述】:

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

【问题讨论】:

  • 请把它变成一个提出问题的问题(例如,'如何将 tdms 文件读入 pandas 数据帧?',也许会提到一些可能遇到的问题),然后发布您的解决方案作为答案,那么(假设解决方案有效)人们会很乐意支持它。
  • 编辑了问题 - 适当的解决方案。

标签: python-3.x pandas labview


【解决方案1】:

为了社区的利益,发布我用来有效地将 *.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)

欢迎提出建议和改进。

【讨论】:

    【解决方案2】:

    为清楚起见,我将 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 数据帧。

    【讨论】:

      【解决方案3】:

      这对我有用:

      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 对象的方法,这是在前面的示例中使用的。

      【讨论】:

        【解决方案4】:

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

        【讨论】:

          猜你喜欢
          • 2014-05-22
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-05-16
          • 1970-01-01
          • 2022-09-23
          • 2018-02-03
          • 1970-01-01
          相关资源
          最近更新 更多