Issue
I would like to convert the timestamp column to datetime format in a pandas dataframe. I used:
df['My_Datetime'] = pd.to_datetime(df['My_Datetime'])
df looks like:
ID Timestamp
0 AM12C 2002-11-30 00:00:00
2 BX900 2008-06-30 00:00:00
3 C1200 2012-06-30 00:00:00
...
and it caught error:
ValueError: mixed datetimes and integers in passed array
I removed the duplicates:
df.drop_duplicates(keep=False, inplace=True)
and tried again, but no luck.
Solution
I expect adding errors='coerce'
will help:
import sys
import pandas as pd
from io import StringIO
from datetime import datetime
data = StringIO("""ID;Timestamp
AM12C;2002-11-30 00:00:00
BX900;2008-06-30 00:00:00
C1200;2012-06-30 00:00:00
Z9999;1000
""")
df = pd.read_csv(data, sep=";")
df['Timestamp'] = pd.to_datetime(df['Timestamp'], errors='coerce')
print(df.info())
Result:
ID Timestamp
0 AM12C 2002-11-30
1 BX900 2008-06-30
2 C1200 2012-06-30
3 Z9999 NaT
Answered By - René
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.