【问题标题】:Append data from TDMS file附加来自 TDMS 文件的数据
【发布时间】:2020-11-25 16:14:59
【问题描述】:

我有一个文件夹 TDMS 文件(也可以是 Excel)。 这些存储在 5 MB 包中,但都包含相同的数据结构。 不幸的是,行中没有绝对时间,时间戳以以下格式存储在“TimeStamp”列中有点神秘

“2020 年 11 月 17 日星期二 19:20:15”

但是现在我想加载每个文件并在同一个图表中一个接一个地绘制它们。 对于一个文件这是没有问题的,因为我只是简单的使用文件的索引作为x轴,但是如果我加载几个文件,每个文件中的索引是相同的,并且数据重叠。

有没有人知道如何将所有数据写入 DataFrame,但具有连续的时间戳,以便可以一个接一个地绘制数据,或者我也可以指定我想要的时间段看到数据了吗?

我的第一种方法如下。 如果有人可以使用 CSV 文件 (pandas.read.csv) 而不是 npTDMS 模块上传示例,那将同样有帮助!

https://nptdms.readthedocs.io/en/stable/

import pandas as pd
import matplotlib.pyplot as plt

from nptdms import TdmsFile



tdms_file = TdmsFile.read("Datei1.tdms")
tdms_groups = tdms_file.groups()
tdms_Variables_1 = tdms_file.group_channels(tdms_groups[0])

MessageData_channel_1 = tdms_file.object('Data', 'Position')
MessageData_data_1 = MessageData_channel_1.data
    
#MessageData_channel_2 = tdms_file.object('Data', 'Timestamp')
#MessageData_data_2 = MessageData_channel_2.data

df_y = pd.DataFrame(data=MessageData_data_1).append(df_y)
    
plt.plot(df_y)

【问题讨论】:

    标签: python pandas dataframe timestamp


    【解决方案1】:

    这是一个 CSV 示例。它首先会在./data/ 文件夹中创建一堆看起来与您的文件相似的文件。然后它将读回这些文件(使用glob 查找它们)。它使用pandas.concat将数据框组合成1,然后解析日期。

    import glob
    import random
    import pandas
    import matplotlib.pyplot as plt
    
    # Create a bunch of test files that look like your data (NOTE: my files aren't 5MB, but 100 rows)
    df = pandas.DataFrame([{"value": random.randint(50, 100)} for _ in range(1000)])
    df["timestamp"] = pandas.date_range(
        start="17/11/2020", periods=1000, freq="H"
    ).strftime(r"%a. %d.%m.%Y %H:%M:%S")
    chunks = [df.iloc[i : i + 100] for i in range(0, len(df) - 100 + 1, 100)]
    for index, chunk in enumerate(chunks):
        chunk[["timestamp", "value"]].to_csv(f"./data/data_{index}.csv", index=False)
    
    # ===============
    
    # Read the files back into a dataframe
    dataframe_list = []
    for file in glob.glob("./data/data_*.csv"):
        df = pandas.read_csv(file)
        dataframe_list.append(df)
    
    # Combine all individual dataframes into 1
    df = pandas.concat(dataframe_list)
    
    # Set the time file correctly
    df["timestamp"] = pandas.to_datetime(df["timestamp"], format=r"%a. %d.%m.%Y %H:%M:%S")
    
    # Use the timestamp as the index for the dataframe, and make sure it's sorted
    df = df.set_index("timestamp").sort_index()
    
    # Create the plot
    plt.plot(df)
    

    【讨论】:

      【解决方案2】:

      @Gijs 沃本

      非常感谢!它工作得很好,它将为我节省很多工作! 作为一名机械工程师,您不会经常编写这样的代码,所以如果其他学科的人可以帮助您,我很高兴。

      这是基本结构,我是如何直接使用 TDMS-Files 完成的,因为我后来读到 npTDMS 模块提供了将数据直接作为数据帧读取的可能性,这是我以前不知道的

      import pandas as pd
      from nptdms import TdmsFile
      from nptdms import tdms
      import os,glob
      
      
      file_names=glob.glob('*.tdms')
          
      tdms_file = TdmsFile.read(file_names[0])
      
      # Read the files back into a dataframe
      dataframe_list = []
      for file in glob.glob("*.tdms"):
          tdms_file = TdmsFile.read(file)
          df = tdms_file['Sheet1'].as_dataframe()
          dataframe_list.append(df)
          
      df_all = pd.concat(dataframe_list)    
      
      # Set the time file correctly
      df["Timestamp"] = pd.to_datetime(df["Timestamp"], format=r"%a. %d.%m.%Y %H:%M:%S")
      
      # Use the timestamp as the index for the dataframe, and make sure it's sorted
      df = df.set_index("Timestamp").sort_index()
      
      # Create the plot
      plt.plot(df)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-05-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-04-26
        • 1970-01-01
        相关资源
        最近更新 更多