Skip to content

Timestamp sub second resolution not supported with MySQL #7938

@femtotrader

Description

@femtotrader

Hello,

Timestamp sub second resolution doesn't seem to be supported with mysqldb.

See:

#!/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+mysqldb'

    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()

doesn't raise any error but when I queried database I noticed that I have only second resolution timestamp (no microsecond, in fact no fractional seconds).

$ mysql -u root mysql -p
Enter password:
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 40
Server version: 5.5.38-0ubuntu0.14.04.1 (Ubuntu)

Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> use test;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> select * from orcl;
+---------------------+------+
| Date                | Open |
+---------------------+------+
| 2000-12-29 00:00:00 |    0 |
| 2000-12-30 00:00:00 |    1 |
| 2000-12-31 00:00:00 |    2 |
| 2001-01-01 00:00:00 |    3 |
| 2001-01-02 00:00:00 |    4 |
+---------------------+------+
5 rows in set (0.00 sec)

mysql> show columns from orcl;
+-------+------------+------+-----+---------+-------+
| Field | Type       | Null | Key | Default | Extra |
+-------+------------+------+-----+---------+-------+
| Date  | datetime   | YES  | MUL | NULL    |       |
| Open  | bigint(20) | YES  |     | NULL    |       |
+-------+------------+------+-----+---------+-------+
2 rows in set (0.00 sec)

Maybe storing datetime64 into integer value could be the most portable solution over database, driver...

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions