Skip to content

'MySQLConverter' object has no attribute '_timestamp_to_mysql' error with datetime64[ns], MySQL and MySQL-connector #7936

Closed
@femtotrader

Description

@femtotrader

Hello,

This code

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import pandas as pd
import numpy as np
import sqlalchemy

def main():
    N = 5
    df = pd.DataFrame({'Date':pd.date_range('2000-12-29', freq='D', periods=N), 'Open': range(N)})
    scheme = 'mysql+mysqlconnector'

    config = {
        'host'    : 'localhost',
        'database': 'test',
        'user'    : 'root',
        'password': '123456',
        'port': 3306
    }

    db_uri = "{scheme}://{user}:{password}@{host}:{port}/{database}"
    db_uri = db_uri.format(
        scheme = scheme,
        user = config['user'],
        password = config['password'],
        port = config['port'],
        host = config['host'],
        database = config['database']
    )
    engine = sqlalchemy.create_engine(db_uri)

    df["Date"] = df["Date"] + np.timedelta64(1, 'us') # to test resolution issue

    df = df.set_index("Date")

    print(df)
    print(df.dtypes)
    print(type(df.index), df.index.dtype)
    print(type(df.index[0]))

    df.to_sql("orcl", engine, flavor="mysql", if_exists="replace")

if __name__ == '__main__':
    main()

raises following error:

sqlalchemy.exc.ProgrammingError: (ProgrammingError) Failed processing pyformat-parameters; 'MySQLConverter' object has no attribute '_timestamp_to_mysql' u'INSERT INTO orcl (`Date`, `Open`) VALUES (%(Date)s, %(Open)s)' ({'Date': Timestamp('2000-12-29 00:00:00.000001'), 'Open': 0}, {'Date': Timestamp('2000-12-30 00:00:00.000001'), 'Open': 1}, {'Date': Timestamp('2000-12-31 00:00:00.000001'), 'Open': 2}, {'Date': Timestamp('2001-01-01 00:00:00.000001'), 'Open': 3}, {'Date': Timestamp('2001-01-02 00:00:00.000001'), 'Open': 4})

I try to pass my own converter see

But I didn't have success

I think it should be nice if datetime64[ns] where automatically stored as BIGINT in database.

I'm aware that I can convert df column (or index) with datetime64[ns] (or Pandas Timestamp) data to integer data before calling to_sql but I don't consider this as a clean way of doing.

In [1]: import pandas as pd

In [2]: pd.__version__
Out[2]: '0.14.1'

In [3]: import sqlalchemy

In [4]: sqlalchemy.__version__
Out[4]: '0.8.4'

In [5]: import numpy as np

In [6]: np.__version__
Out[6]: '1.8.1'

In [7]: import mysql.connector

In [8]: mysql.connector.__version__
Out[8]: '1.1.6'

Metadata

Metadata

Assignees

No one assigned

    Labels

    IO SQLto_sql, read_sql, read_sql_query

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions