Loads the dataset using the specified backend. Notice that the available backends are 'pandas', 'polars', 'pyarrow' and they all have a `read_csv` function (pyarrow has it via pyarrow.csv). Therefore we can dynamically load the library using `importlib.import_module` and then call
(d, return_type)
| 377 | |
| 378 | |
| 379 | def _get_dataset(d, return_type): |
| 380 | """ |
| 381 | Loads the dataset using the specified backend. |
| 382 | |
| 383 | Notice that the available backends are 'pandas', 'polars', 'pyarrow' and they all have |
| 384 | a `read_csv` function (pyarrow has it via pyarrow.csv). Therefore we can dynamically |
| 385 | load the library using `importlib.import_module` and then call |
| 386 | `backend.read_csv(filepath)`. |
| 387 | |
| 388 | Parameters |
| 389 | ---------- |
| 390 | d: str |
| 391 | Name of the dataset to load. |
| 392 | |
| 393 | return_type: {'pandas', 'polars', 'pyarrow', 'modin', 'cudf'} |
| 394 | Type of the resulting dataframe |
| 395 | |
| 396 | Returns |
| 397 | ------- |
| 398 | Dataframe of `return_type` type |
| 399 | """ |
| 400 | filepath = os.path.join( |
| 401 | os.path.dirname(os.path.dirname(__file__)), |
| 402 | "package_data", |
| 403 | "datasets", |
| 404 | d + ".csv.gz", |
| 405 | ) |
| 406 | |
| 407 | if return_type not in AVAILABLE_BACKENDS: |
| 408 | msg = ( |
| 409 | f"Unsupported return_type. Found {return_type}, expected one " |
| 410 | f"of {AVAILABLE_BACKENDS}" |
| 411 | ) |
| 412 | raise NotImplementedError(msg) |
| 413 | |
| 414 | try: |
| 415 | if return_type == "pyarrow": |
| 416 | module_to_load = "pyarrow.csv" |
| 417 | elif return_type == "modin": |
| 418 | module_to_load = "modin.pandas" |
| 419 | else: |
| 420 | module_to_load = return_type |
| 421 | backend = import_module(module_to_load) |
| 422 | except ModuleNotFoundError: |
| 423 | msg = f"return_type={return_type}, but {return_type} is not installed" |
| 424 | raise ModuleNotFoundError(msg) |
| 425 | |
| 426 | try: |
| 427 | return backend.read_csv(filepath) |
| 428 | except Exception as e: |
| 429 | msg = f"Unable to read '{d}' dataset due to: {e}" |
| 430 | raise Exception(msg).with_traceback(e.__traceback__) |
no outgoing calls
no test coverage detected