简体   繁体   English

python pandas 0.14.1中的sqlalchemy的pandas parse_dates列通配符?

[英]python pandas parse_dates column wildcard for sqlalchemy in pandas 0.14.1?

I'm using the sqlalchemy that allows sql queries for the recently released 0.14.1 version of pandas. 我使用sqlalchemy ,允许SQL查询最近发布的0.14.1版本的熊猫。

import pandas as pd
from dateutil import parser
from sqlalchemy import create_engine
import datetime

a=[['Datetime', 'Now Date', 'numbers', 'mixed'], ['1/2/2014', datetime.datetime.now(),6, 'z1'], ['1/3/2014', datetime.datetime.now(), 3, 'z1']]
df = pd.DataFrame(a[1:],columns=a[0])
df['Datetime']=df['Datetime'].map(lambda x: parser.parse(x))

engine=create_engine('sqlite:///:memory:')
df.to_sql('db_table',engine, index=False)
df_new=pd.read_sql_query("SELECT * FROM db_table ",engine)

>>> df.dtypes
Datetime    datetime64[ns]
Now Date    datetime64[ns]
numbers              int64
mixed               object
dtype: object

>>> df_new.dtypes
Datetime    object
Now Date    object
numbers      int64
mixed       object
dtype: object

As you can see, my original datetime format is lost when feeding it into the engine. 如您所见,将原始datetime格式输入引擎后会丢失。 But pandas gives you a way to get it back by parsing. 但是,pandas提供了一种通过解析将其恢复的方法。

df_new=pd.read_sql_query("SELECT * FROM db_table ",engine, parse_dates=['Datetime','Now Date'])

>>> df_new.dtypes
Datetime    datetime64[ns]
Now Date    datetime64[ns]
numbers              int64
mixed               object
dtype: object

The problem is i'm feeding different kinds of datetimes into the engine with different column names, I can't manually specify each column name. 问题是我用不同的列名将不同类型的datetimes输入到引擎中,我无法手动指定每个列名。 I have too many things to parse and it is constantly changing. 我要解析的东西太多了,而且它还在不断变化。 I'm looking for a solution that is the equivalent of something like this: 我正在寻找一种类似于以下内容的解决方案:

df_new=pd.read_sql_query("SELECT * FROM db_table ",engine, parse_dates=['*Date*'])

SQLite has no date or datetime type. SQLite没有日期或日期时间类型。 So the datetime values are stored as strings and when fetching a query they also come back as strings. 因此,datetime值存储为字符串,并且在获取查询时它们也以字符串形式返回。
But there are some different options here to deal with this: 但是这里有一些不同的选项可以解决这个问题:

  • Use read_sql_table instead of read_sql_query (if you only need to do "SELECT * FROM ..." or certain columns, and you need no where clause). 使用read_sql_table代替read_sql_query (如果您只需要执行“ SELECT * FROM ...”或某些列,并且不需要where子句)。 This will use the information in the table schema and detect it are datetime columns and convert them (sqlalchemy does this): 这将使用表模式中的信息并将其检测为datetime列并将其转换(sqlalchemy这样做):

     In [13]: df_new2 = pd.read_sql_table("db_table",engine) In [15]: df_new2.dtypes Out[15]: Datetime datetime64[ns] Now Date datetime64[ns] numbers int64 mixed object dtype: object 
  • You can specify sqlite3.PARSE_DECLTYPES (see docs or this question: How to read datetime back from sqlite as a datetime instead of string in Python? ) when using a sqlite connection: 使用sqlite连接时,可以指定sqlite3.PARSE_DECLTYPES (请参阅文档或以下问题: 如何从sqlite读取datetime作为日期时间而不是Python中的字符串? ):

     In [33]: con = sqlite3.connect(':memory:', detect_types=sqlite3.PARSE_DECLTYPES) In [34]: df.to_sql('db_table', con, index=False) In [35]: df_new = pd.read_sql_query("SELECT * FROM db_table",con) In [36]: df_new.dtypes Out[36]: Datetime datetime64[ns] Now Date datetime64[ns] numbers int64 mixed object dtype: object 

    This does not seem to work very nice with sqlalchemy ( http://docs.sqlalchemy.org/en/rel_0_9/dialects/sqlite.html#compatibility-with-sqlite3-native-date-and-datetime-types ) 这似乎不适用于sqlalchemy( http://docs.sqlalchemy.org/en/rel_0_9/dialects/sqlite.html#compatibility-with-sqlite3-native-date-and-datetime-types

  • You can do the parsing afterwards, to do this automatically on all columns that contain 'Date': 您可以随后进行解析,以自动在所有包含“日期”的列上执行此操作:

     In [45]: date_cols = [col for col in df.columns if 'Date' in col] In [47]: for col in date_cols: ....: df[col] = pd.to_datetime(df[col]) ....: 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM