3

How can I read a standard labVIEW generated TDMS file using python?

SeanJ
  • 1,203
  • 2
  • 19
  • 39
Sundar N
  • 316
  • 4
  • 17
  • 1
    Please make this into a question posing the problem (e.g. 'how do I read a tdms file into a pandas dataframe?', perhaps mentioning some of the problems that may be encountered), then post your solution as an answer, then (assuming the solution works) people will be happy to upvote it. – nekomatic Jan 02 '18 at 08:52
  • Edited the problem - Solution appropriately. – Sundar N Jan 02 '18 at 11:52

4 Answers4

4

For the benefit of the community , posting sample code base i have used to efficiently read *.tdms file into pandas dataframe. 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.

Sundar N
  • 316
  • 4
  • 17
  • numpy and pandas are unused in the code snippet. Also why did not you prefer matplotlib over bokeh as it is more common. – abunickabhi Jun 29 '22 at 05:32
2

For clarity, i would further simplify the answer by Sundar to:

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.

Joris
  • 1,158
  • 1
  • 16
  • 25
1

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

It also possible to convert the whole TDMS file into one DataFrame, see example below.

from nptdms import TdmsFile

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

df = tdms_file.as_dataframe()
DakotaD
  • 371
  • 3
  • 18
0

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.

ax7ster
  • 26
  • 3