Home > front end >  I have a csv file name timestamp.csv,in which first column is of Time.I need to change the format of
I have a csv file name timestamp.csv,in which first column is of Time.I need to change the format of

Time:08-02

The format of time in the csv is something like this 2022-05-12 10:38:21 594.666 but I need to change it to 2022-05-12 10:38:21.594666. I need to replace all values to this expression %Y-%m-%d %H:%M:%S %f I am stuck here

import pandas as pd
a = pd.read_csv("timestamp.csv")
df=pd.DataFrame(a,columns=['Time'])
df=df.replace([','],'')

this code doesn't change my format.

I want this 10:38:21 594.666 format to change into 10:38:21.594666 in all rows of time

CodePudding user response:

One way would be to break it up into two easy to process pieces, and then bring them back together:

Given:

                     timestamp
0  2022-05-12 10:38:21 594.666

Doing:

# Split into two cols:
df[['timestamp', 'ms']] = df.timestamp.str.split(' (?=\S $)', expand=True)

# Process the timestamp:
df.timestamp = pd.to_datetime(df.timestamp)

# Process the Milliseconds:
df.ms = pd.to_timedelta(df.ms.astype(float), unit='ms')

# Combine them again:
df.timestamp = df['timestamp']   df['ms']

# Drop our helper column:
df = df.drop('ms', axis=1)
print(df)

Output:

                   timestamp
0 2022-05-12 10:38:21.594666

CodePudding user response:

Seeing as the time columns is not official time format, I would just treat it all as string and then use string replace. It's not pretty but it works.

import pandas as pd

# input filename
filename = "testdata.txt"

# explicitly force column datatypes to string
col_types = {
    "Time": str,
    "othercolumn1": str,
    "othercolumn2": str,
    "etc": str
}
# read csv file
df = pd.read_csv(filename, sep=',', dtype=col_types)

# it ain't pretty but it works
df['Time'] = df['Time'].str.replace('.', '', regex=False)
df['Time'] = df['Time'].str.replace(' ', '.', regex=False) # replace ALL spaces
df['Time'] = df['Time'].str.replace('.', ' ', 1, regex=False) # replace FIRST dot only

# csv write new output
df.to_csv("output.txt", sep=',', header=True, index=False)
  • Related