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
Adding pd.DataFrame as a posssible output of image_to_data (madmaze#160)
* Adding pd.DataFrame as a posssible output of image_to_data
**Motivation**
Since most data scientist use pandas.DataFrame in a daily basis, I find it a nice have to add it as a possible output for `image_to_data`.
**New dependencies**
No new dependencies.
This feature is only available if pandas is already installed (to avoid requiring it), see `pandas_installed` (inspired by `numpy_installed`).
**Implementation**
It is done by only changing function `image_to_data` so it is not perfectly clean and require calling `StringIO`. But I did it this way to avoid changing `run_and_get_output`.
*New code:*
Add a conditional import to pandas
```
from io import StringIO
pandas_installed = find_loader('pandas') is not None
if pandas_installed:
import pandas as pd
```
Add DATAFRAME to output format
```
class Output:
STRING = "string"
BYTES = "bytes"
DICT = "dict"
DATAFRAME = "data.frame"
```
Add a call to pandas.read_csv going through `StingIO ` to retrieve a data.frame in `image_to_data`
```
elif pandas_installed and output_type == Output.DATAFRAME:
args.append(True)
return pd.read_csv(StringIO(run_and_get_output(*args)), sep="\t")
```
* New line at the end
* Improve version according to comments
Added an explicit error in case of pandas not installed
0 commit comments