Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUG: lossed millisecond precision of data by "pandas.io.sql.to_sql" #48435

Open
3 tasks done
abcdehc opened this issue Sep 7, 2022 · 0 comments
Open
3 tasks done

BUG: lossed millisecond precision of data by "pandas.io.sql.to_sql" #48435

abcdehc opened this issue Sep 7, 2022 · 0 comments
Labels
Bug IO SQL to_sql, read_sql, read_sql_query Non-Nano datetime64/timedelta64 with non-nanosecond resolution

Comments

@abcdehc
Copy link

abcdehc commented Sep 7, 2022

Pandas version checks

  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • I have confirmed this bug exists on the main branch of pandas.

Reproducible Example

i lossed millisecond precision of data from the following code:


import pandas as pd
import datetime
from sqlalchemy import create_engine

sql_username = 'root'
sql_password = '********'   # your password
sql_schema = 'test_db'      # already exists
sql_table = 'test_tb'       # will be create
sql_engine = create_engine(f"mysql+pymysql://{sql_username}:{sql_password}@127.0.0.1:3306/{sql_schema}?charset=UTF8MB4")
data_in = pd.DataFrame({'time':datetime.datetime.now()},[0])
print('before save:',data_in)
pd.io.sql.to_sql(data_in, sql_table, sql_engine, schema=sql_schema, if_exists='append', index = False)
sql_cmd = "SELECT * FROM {table}".format(table=sql_table)
data_out = pd.read_sql(sql=sql_cmd, con=sql_engine)
print('after save:',data_out)


### output

<details>

```python
before save:                         time
0 2022-09-07 17:42:52.771654
after save:                  time
0 2022-09-07 17:42:53

Issue Description

"pandas.io.sql.to_sql" automatically saves the <class 'datetime.datetime'> as <Datatype 'DATETIME'> in mysql.
But since version:MySQL 5.6,<Datatype 'DATETIME'> in mysql supports milliseconds.
I tried change the column type in mysql by this mysql code:
alter table test_db.test_tb modify column time datetime(6);
Then, "pandas.io.sql.to_sql" will not loss milliseconds precision of data.
I thought "pandas.io.sql.to_sql" should default to use <Datatype 'DATETIME(6)'>, when the data have milliseconds precision.

Expected Behavior

Expected output

before save:                         time
0 2022-09-07 17:42:52.771654
after save:                  time
0 2022-09-07 17:42:53.771654

Installed Versions

INSTALLED VERSIONS

commit : e8093ba
python : 3.10.6.final.0
python-bits : 64
OS : Windows
OS-release : 10
Version : 10.0.22621
machine : AMD64
processor : AMD64 Family 25 Model 68 Stepping 1, AuthenticAMD
byteorder : little
LC_ALL : None
LANG : None
LOCALE : Chinese (Simplified)_China.936

pandas : 1.4.3
numpy : 1.23.2
pytz : 2022.2.1
dateutil : 2.8.2
setuptools : 63.2.0
pip : 22.2.1
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : 4.9.1
html5lib : None
pymysql : 1.0.2
psycopg2 : None
jinja2 : 3.1.2
IPython : 8.4.0
pandas_datareader: None
bs4 : 4.11.1
bottleneck : None
brotli : None
fastparquet : None
fsspec : None
gcsfs : None
markupsafe : 2.1.1
matplotlib : 3.5.3
numba : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : 1.9.0
snappy : None
sqlalchemy : 1.4.40
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
zstandard : None

@abcdehc abcdehc added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Sep 7, 2022
@jbrockmendel jbrockmendel added the IO SQL to_sql, read_sql, read_sql_query label Nov 25, 2022
@jbrockmendel jbrockmendel added the Non-Nano datetime64/timedelta64 with non-nanosecond resolution label Nov 1, 2023
@mroeschke mroeschke removed the Needs Triage Issue that has not been reviewed by a pandas team member label Jul 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug IO SQL to_sql, read_sql, read_sql_query Non-Nano datetime64/timedelta64 with non-nanosecond resolution
Projects
None yet
Development

No branches or pull requests

3 participants