Home>

Currently, I am creating a function that preprocesses data by python, but I am in trouble because an error occurs.

【Thing I want to do】
I want to extract the first 8 digits from the ID column and convert it to a date type.

・ ID column
→ 16-digit number (objevt type)

[Created function]
def preprocessing (result):
df = result.copy ()

df ["date"] = df ["ID"]. map (lambda x: str (x) [0: 8])

df ["date"] = pd.to_datetime (df ["date"], format ='% Y year% m month% d day')

return df

[Error occurring]
ValueError: time data '20191228' does not match format'% Y year% m month% d day' (match)

I intended to investigate various things, but I couldn't find the cause at all.

  • Answer # 1

    pd.to_datetime (df ["date"], format ='% Y year% m month% d day')IsOctober 2, 1992It is a process to convert a format like to Timestamp, but it does not match that format20191228Is the valuedf ["date"]It means that it was there.
    YYYYMMDDCan be converted to Timestamp without specifying the format, so please modify it as follows.

    --pd.to_datetime (df ["date"], format ='% Y year% m month% d day')
    + pd.to_datetime (df ["date"])