Skip to content

Commit 3eb1974

Browse files
committed
Fix pandas typechecking in util
1 parent abd00b6 commit 3eb1974

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

python/hopsworks_common/util.py

+11-4
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
from hopsworks_common import client
4343
from hopsworks_common.client.exceptions import FeatureStoreException, JobException
4444
from hopsworks_common.constants import MODEL, PREDICTOR, Default
45+
from hopsworks_common.core.constants import HAS_PANDAS
4546
from hopsworks_common.git_file_status import GitFileStatus
4647
from six import string_types
4748

@@ -72,7 +73,9 @@ def convert(self, obj):
7273
import base64
7374

7475
import numpy as np
75-
import pandas as pd
76+
77+
if HAS_PANDAS:
78+
import pandas as pd
7679

7780
def encode_binary(x):
7881
return base64.encodebytes(x).decode("ascii")
@@ -85,7 +88,9 @@ def encode_binary(x):
8588
else:
8689
return obj.tolist(), True
8790

88-
if isinstance(obj, (pd.Timestamp, datetime.date)):
91+
if isinstance(obj, datetime.date) or (
92+
HAS_PANDAS and isinstance(obj, pd.Timestamp)
93+
):
8994
return obj.isoformat(), True
9095
if isinstance(obj, bytes) or isinstance(obj, bytearray):
9196
return encode_binary(obj), True
@@ -512,14 +517,16 @@ def _handle_tensor_input(input_tensor):
512517

513518

514519
def _handle_dataframe_input(input_ex):
515-
if isinstance(input_ex, pd.DataFrame):
520+
if HAS_PANDAS:
521+
import pandas as pd
522+
if HAS_PANDAS and isinstance(input_ex, pd.DataFrame):
516523
if not input_ex.empty:
517524
return input_ex.iloc[0].tolist()
518525
else:
519526
raise ValueError(
520527
"input_example of type {} can not be empty".format(type(input_ex))
521528
)
522-
elif isinstance(input_ex, pd.Series):
529+
elif HAS_PANDAS and isinstance(input_ex, pd.Series):
523530
if not input_ex.empty:
524531
return input_ex.tolist()
525532
else:

0 commit comments

Comments
 (0)