-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
read_sql should convert Numeric to decimals #20525
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
Comments
|
|
For the reason that @jreback mentions (decimals cannot be stored natively in a pandas dataframe, only as object dtype), I don't think we want to change this by default. But, it could be a good idea to at least enable the user the specify explicitly they want to keep the decimal objects. That would be a welcome contribution I think. |
There is a very old, closed issue that raises some good points about dtypes: If you allow read_sql a parameter for specifying the dtypes of the resulting table then that could help people a lot. Alternatively, add an example to the documentation where It would very useful for large tables. Casting to categories and smaller bit dtypes saves so much memory which in turn decreases data processing run-time significantly. |
I just did a test, and whether I use True or False, NUMERIC columns in SQL tables get typed to float64. I don't understand the logic here. Failing workaround : if I initialize the target dataframe of the import from db, using an emptied copy of an existing dataframe which is typed correctly, the import resets the types of the target dataframe.
I'm currently learning Python, so I'm far from having a level good enough to do it (otherwise I would). Apart from that, thanks to all the developers contributing to this framework, which is a wonder even with these small quirks here and there ^^ |
👍 Please upvote this, I am having a very hard time with Pandas right now because of this issue. |
@mroeschke possibly viable with pyarrow decimal? |
Yeah this in theory should be better supported with pyarrow decimal. The sqlalchemy layer will need to be changed though because I think it immediately does the conversion of decimal to float |
This realize issue has been dormant for two years, but it sure has been giving me a headache today. I'm using DECIMAL with SQLite (without sqlalchemy). I really wish
My solution is to have SQLite |
pd.read_sql
converts columns of typesqlalchemy.Numeric
to floats as the below selection appears to suggest. This seems counter intuitive since in theNUMERIC
andDECIMAL
types are interchangeable. Also theNUMERIC
type has an optionasdecimal
that defaults to true (http://docs.sqlalchemy.org/en/latest/core/type_basics.html#sqlalchemy.types.Numeric). I would expect thatsqlalchemy.Numeric
types get converted todecimal.Decimal
.https://github.com/pandas-dev/pandas/blob/master/pandas/io/sql.py#L1187-L1190
The text was updated successfully, but these errors were encountered: