You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
ilossedmillisecondprecisionofdatafromthefollowingcode:
importpandasaspdimportdatetimefromsqlalchemyimportcreate_enginesql_username='root'sql_password='********'# your passwordsql_schema='test_db'# already existssql_table='test_tb'# will be createsql_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>```pythonbeforesave: time02022-09-0717:42:52.771654aftersave: time02022-09-0717: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.
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
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
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
The text was updated successfully, but these errors were encountered: