Home > Blockchain >  I want to change a number with 5 decimal places in the form "yyyy-mm-dd hh:mm:ss."
I want to change a number with 5 decimal places in the form "yyyy-mm-dd hh:mm:ss."

Time:07-19

I want to change the data 20220718.20154 to 2022-07-18 20:15:40 in dataframe. So I wrote the code below.

import pandas as pd

pd.options.display.float_format = '{:.6f}'.format
df = pd.read_excel(filepath)
df["date"] = pd.to_datetime(df["date"].astype(str), format='%Y%m%d.%H%M%S', errors='coerce')
print(df["date"])

but, this outputs

0     2022-07-18 10:34:02
1     2022-07-18 10:03:04
2     2022-07-18 20:15:04
... 

CodePudding user response:

Use Series.str.ljust for add 0 if less like 15 characters from left side:

df["date"] = pd.to_datetime(df["date"].astype(str).str.ljust(15, '0'), format='%Y%m%d.%H%M%S', errors='coerce')
  • Related