Skip to content

Commit

Permalink
fix(pandas): only set columns when apply_column_names is set (#3275)
Browse files Browse the repository at this point in the history
Fixes #3263
Co-authored-by: Aaron Pham <29749331+aarnphm@users.noreply.github.com>
  • Loading branch information
mqk committed Nov 25, 2022
1 parent 8cc94b1 commit 4e771b6
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions src/bentoml/_internal/io_descriptors/pandas.py
Original file line number Diff line number Diff line change
Expand Up @@ -595,13 +595,18 @@ def validate_dataframe(
# if self._enforce_dtype:
# raise exception_cls(msg) from None

if self._columns is not None and len(self._columns) != dataframe.shape[1]:
msg = "length of 'columns' (%d) does not match the # of columns of incoming data."
if self._apply_column_names:
raise BadInput(msg % len(self._columns)) from None
else:
logger.debug(msg, len(self._columns))
dataframe.columns = pd.Index(self._columns)
if self._apply_column_names:
if not self._columns:
raise BadInput(
"When apply_column_names is set, you must provide columns."
)

if len(self._columns) != dataframe.shape[1]:
raise BadInput(
f"length of 'columns' ({len(self._columns)}) does not match the # of columns of incoming data ({dataframe.shape[1]})."
) from None

dataframe.columns = pd.Index(self._columns)

# TODO: convert from wide to long format (melt())
if self._shape is not None and self._shape != dataframe.shape:
Expand Down

0 comments on commit 4e771b6

Please sign in to comment.