Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python pandas string handling categorical data from SQL database

I have a large dataset i need to read into a pandas dataframe.

It contains alot of categorical data consiting of some rather long string.

Trying to use the pandas read_sql_query method I can't seems to specify what columns should be treated as categorical data.

This means i get memory issues.

I have a background in R where i can specify things like, string as factor. Meaning you can have long strings with a small memory footprint since they are indexed as integers in R. Can't i do the same in Python/Pandas?

I would like to do it as i read the data from the database! not after. Converting string to category in pandas is easy once you have it in a dataframe, but that is not what I'm looking for.

I understand that i could simply encode the data in the database but I would like to avoid that.

like image 731
CodeMonkey Avatar asked Jul 27 '26 23:07

CodeMonkey


1 Answers

I'm afraid currently encoding on the DB side (this can be done using JOIN with a mapping table) is the only viable option.

There were a few similar feature requests:

  • https://github.com/pandas-dev/pandas/issues/17862
  • https://github.com/pandas-dev/pandas/issues/13049
  • https://github.com/pandas-dev/pandas/issues/6798
  • https://github.com/pandas-dev/pandas/issues/17560

Reading data in chunks and converting each chunk to category dtype might be tricky as one might need to join categories from all chunks.

like image 100
MaxU - stop WAR against UA Avatar answered Jul 29 '26 13:07

MaxU - stop WAR against UA



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!