Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 11 additions & 12 deletions src/scyjava/_convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import logging
import math
from bisect import insort
from importlib.util import find_spec
from pathlib import Path
from typing import Any, Callable, Dict, List, NamedTuple

Expand Down Expand Up @@ -677,7 +678,7 @@ def _stock_py_converters() -> List:
priority=Priority.VERY_LOW,
),
]
if _import_pandas(required=False):
if find_spec("pandas"):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a great trick, thanks!

converters.append(
Converter(
name="org.scijava.table.Table -> pandas.DataFrame",
Expand Down Expand Up @@ -716,7 +717,7 @@ def _stock_py_converters() -> List:
),
]
)
if _import_numpy(required=False):
if find_spec("numpy"):
converters.append(
Converter(
name="primitive array -> numpy.ndarray",
Expand Down Expand Up @@ -803,16 +804,15 @@ def _jarray_shape(jarr):
return shape


def _import_numpy(required=True):
def _import_numpy():
try:
import numpy as np

return np
except ImportError as e:
if required:
msg = "The NumPy library is missing (https://numpy.org/). "
msg += "Please install it before using this function."
raise RuntimeError(msg) from e
msg = "The NumPy library is missing (https://numpy.org/). "
msg += "Please install it before using this function."
raise RuntimeError(msg) from e


######################################
Expand All @@ -838,16 +838,15 @@ def _convert_table(obj: Any):
return None


def _import_pandas(required=True):
def _import_pandas():
try:
import pandas as pd

return pd
except ImportError as e:
if required:
msg = "The Pandas library is missing (http://pandas.pydata.org/). "
msg += "Please install it before using this function."
raise RuntimeError(msg) from e
msg = "The Pandas library is missing (http://pandas.pydata.org/). "
msg += "Please install it before using this function."
raise RuntimeError(msg) from e


def _table_to_pandas(table):
Expand Down
Loading