I have a Pandas df I created from reading an email attachment. When inserting into a SQL Server table I see the following error for Parameter 6 which is Call Length column:
Parameter 6 (""): The supplied value is not a valid instance of data type float. Check the source data for invalid values. An example of an invalid value is data of numeric type with scale greater than precision.
Originally Call Length is a timedelta data type, but I've tried converting to a float. In the SQL Server table is shows as a decimal(18,2) and its shown in seconds
I've tried converting like this:
df['Call Length'] = df['Call Length'].dt.total_seconds().astype(float).round(2)
print(df['Call Length'].dtype) #float64
There are no null or empty rows from what I see, there are only about 70 rows.
I'm then just renaming the columns:
df_selected = df_selected.rename(columns={
'From Name': 'FromName',
'From Number': 'FromNumber',
'To Name': 'ToName',
'To Number': 'ToNumber',
'Call Length': 'CallLength',
'Call Start Time': 'CallStartTime',
'Call Direction': 'CallDirection',
})
and then inserting into the table which is where I see the error
insert_nonqueue = f"""
INSERT INTO marketing.Test(FromName, FromNumber, ToName, ToNumber, Result, CallLength,
CallStartTime, CallDirection)
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
"""
for index, row in df_selected.iterrows():
cursor.execute(insert_nonqueue, row['FromName'], row['FromNumber'], row['ToName'], row['ToNumber'], row['Result'], row['CallLength'], row['CallStartTime'], row['CallDirection'])
conn.commit()
Sample of the Call Length dataframe col before trying to convert it:
0 0 days 00:00:37
1 0 days 00:00:20
2 0 days 00:01:09
3 0 days 00:00:45
4 0 days 00:00:27
Name: Call Length, dtype: timedelta64[ns]
This fixed the issue:
df_selected = df_selected.fillna(value=0)
found the solution thanks to this answer: https://stackoverflow.com/a/53987910/11578869