Home > front end >  ValueError: mixed datetimes and integers in passed array
ValueError: mixed datetimes and integers in passed array

Time:07-11

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.

CodePudding user response:

To get this error, you would need to already have converted to datetime, then replaced one value with an integer:

s = pd.Series(['2002-11-30 00:00:00', '2008-06-30 00:00:00', 0])
s = pd.to_datetime(s)
# works fine until here

s.iloc[-1] = 0
pd.to_datetime(s)
# now raises an error

output ValueError: mixed datetimes and integers in passed array

So, don't convert to datetime multiple times, and make sure to understand where this integer is coming from after the first conversion to datetime.

CodePudding user response:

It 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
  • Related