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

SQL: datetime writing: add tests + issue with pymysql #7082

Merged

Conversation

jorisvandenbossche
Copy link
Member

There were apparantly not yet tests for writing datetime data. So added a test case for both query/table.

For MySQL using the pymysql driver, writing a datetime column is not working ... It gives following error:

In [65]: import pymysql
In [66]: engine_mysql = sqlalchemy.create_engine('mysql+pymysql://root@localhost/pandas_nosetest')
In [67]: df = pd.DataFrame({'A': pd.date_range('2013-01-01 09:00:00', periods=3),
    ...:                    'B': np.arange(3.0)})
In [68]: df.to_sql('test_datetime', engine_mysql, if_exists='replace')
Traceback (most recent call last):
  File "<ipython-input-68-95961c7ce232>", line 1, in <module>
    df.to_sql('test_datetime', engine_mysql, if_exists='replace')
...
  File "C:\Anaconda\envs\devel\lib\site-packages\pymysql\converters.py", line 24, in escape_item
    encoder = encoders[type(val)]

KeyError: <class 'pandas.tslib.Timestamp'>

The data are feeded to the database using df.itertuples() and then np.asscalar(), and apparantly this gives data of the Timestamp class. And for some reason the other drivers can handle this (because it is a subclass of datetime.datetime?), and pymysql not. Which is not good...

@jorisvandenbossche jorisvandenbossche added this to the 0.14.0 milestone May 8, 2014
@jreback
Copy link
Contributor

jreback commented May 8, 2014

does this handle NaT / None conversions?

@rockg
Copy link
Contributor

rockg commented May 8, 2014

I don't know if we need a new item, but pandas is good at handling timezone aware dates, but mysql has no knowledge of them. I don't know if we can lay down some suggestions/code for dealing with that data. For example, an IS_DST field along with a MySQL DATETIME field. It's unfortunate that the capability of MySQL is so limiting in this respect.

@jorisvandenbossche
Copy link
Member Author

No, not yet tested NaT/None conversions (and there are certainly things not working with that, see #2754).
@rockg Indeed we should see what we do with timezones, but here, even just a simple datetime isn't even working with mysql (but not due to mysql, but pymysql I think). For timezones, I think we should first ensure it works (does not error, maybe by storing it as utc), and then later maybe see to add options to store it in some other way. If you have concrete ideas of proposals for that, certainly let that know. Although I think eg storing it as a seperate column is up to the user to do this themselves.

@jorisvandenbossche
Copy link
Member Author

Merging this, and I will open a new issue for the issues with MySQL/pymysql

- not working for pymysql (skipping for now)
jorisvandenbossche added a commit that referenced this pull request May 11, 2014
SQL: datetime writing: add tests + issue with pymysql
@jorisvandenbossche jorisvandenbossche merged commit f2f132b into pandas-dev:master May 11, 2014
@jorisvandenbossche jorisvandenbossche deleted the sql-datetime branch May 11, 2014 10:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
IO SQL to_sql, read_sql, read_sql_query
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants