pythonreadlinesread-data

TDMS file read data in python


During the project, I had to deal with a TDMS file. I'm asking because the file could not be read immediately.

My Goal: Perform analysis by converting TDMS file into DataFrame format

First attempt, -Perform TdmsFile open using npTdms package. -Converts to read_data() to execute pd.DataFrame command.

from nptdms import TdmsFile, TdmsWriter, ChannelObject, GroupObject
filenameS = "ex.tdms"
tdms_file = TdmsFile(filenameS)

[enter image description here][1]

So I succeeded in loading using TdmsFile.open() command.

But I tried read_data() on the second attempt and checked the error.

csv_merge = pd.DataFrame()
for i in tdms_file.group_channels('Analog Data'):
temp = i.read_data()
csv_merge = pd.concat([csv_merge, pd.DataFrame({i.channel: temp})], axis=1)

enter image description here

Last attempt,

tdms_file = TdmsFile.open(filenameS)
df = tdms_file.as_dataframe()

enter image description here


Solution

  • Here you go =^..^=

    from nptdms import TdmsFile
    import pandas as pd
    
    # load file
    tdms_file = TdmsFile('20200609_130131.69.tdms')
    # show groups
    groups_data = tdms_file.groups()
    print(groups_data)
    # show channels
    channels_data = tdms_file['group name'].channels()
    print(channels_data)
    # show data in channel
    selected_data = tdms_file['group name']['channel name']
    print(selected_data.data)
    
    # load into df
    df = pd.DataFrame(data=selected_data.data)
    

    df output:

    0  0.000000
    1  0.111111
    2  0.222222
    3  0.333333
    4  0.444444
    5  0.555556
    6  0.666667
    7  0.777778
    8  0.888889
    9  1.000000