[Python] Is there a function to extract datetime from CSV without parsing it to a string?

Discussão em 'Python' iniciado por Stack, Janeiro 24, 2025.

  1. Stack

    Stack Membro Participativo

    I have raw measurement data in the form of a CSV file RAW CSV data format. Now I have a code that averages the milliseconds to the second. and then I need to apply a time correction of 1 hour 10 minutes and 25 seconds to the original time. For example:

    10:23:02.067
    10:23:02.166
    10:23:02.266
    10:23:02.366
    10:23:02.466
    10:23:02.566
    10:23:02.666
    10:23:02.766
    10:23:02.866
    10:23:02.966


    All the values in the rows (of the millisecond) should be averaged to 10:23:02 and then you apply the time correction and then you get 9:12:37. But initially the time is like :

    23:02.0
    23:02.1
    23:02.2
    23:02.3
    23:02.4
    23:02.5
    23:02.6
    23:02.7
    23:02.8
    23:02.9


    When I open the CSV in Excel the code thinks that 23 is the hour, 02 is the minute and 0 is the second and final result is like : 22:49:35

    Could anyone please help? The code is as follows:

    import pandas as pd
    import numpy as np
    import os
    from datetime import timedelta
    from tkinter import Tk, filedialog, messagebox`

    def upload_files(prompt_message):
    root = Tk()
    root.withdraw() # Hide the root window
    file_paths = filedialog.askopenfilenames(title=prompt_message) # Allow multiple file selection
    return file_paths

    def create_new_filename(file_path):
    base_name = os.path.basename(file_path) # Extract the file name with extension
    file_name, file_extension = os.path.splitext(base_name) # Split name and extension
    new_file_name = f"{file_name}_Processed{file_extension}" # Append suffix to the name
    return new_file_name

    def safe_interp(x, xp, fp):
    y = np.interp(x, xp, fp, left=0, right=0)
    y[x < xp.min()] = 0
    y[x > xp.max()] = 0
    return y


    def process_time_column(df, time_column):
    df[time_column] = df[time_column].astype(str)
    df['Time'] = df[time_column].str.slice(0, 8) # Extract 'HH:MM:SS.fff' only
    return df


    def average_by_second(df):
    numeric_columns = df.select_dtypes(include=['float64', 'int64']).columns
    df_grouped = df.groupby('Time')[numeric_columns].mean().reset_index()
    # Merge back non-numeric columns without averaging, keeping the first occurrence for each unique 'Time'
    df_final = pd.merge(df_grouped, df.drop(columns=numeric_columns).drop_duplicates('Time'), on='Time', how='left')
    return df_final.round(5)

    def apply_time_correction(df):
    time_correction = timedelta(hours=1, minutes=10, seconds=25)
    df['corrected_time'] = (pd.to_datetime(df['Time'], format='%I:%M:%S') - time_correction).dt.time
    return df

    Continue reading...

Compartilhe esta Página