Skip to content

Commit

Permalink
refactor: add option to skip header validation on resource files (#260)
Browse files Browse the repository at this point in the history
ECALC-333
  • Loading branch information
markusrf committed Oct 30, 2023
1 parent eed527e commit 883b7e6
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions src/libecalc/input/file_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -408,10 +408,11 @@ def _validate_not_nan(columns: List[List]):
)


def _dataframe_to_resource(df: pd.DataFrame) -> Resource:
def _dataframe_to_resource(df: pd.DataFrame, validate_headers: bool = True) -> Resource:
headers = df.columns.tolist()
headers = [header.strip() for header in headers]
_validate_headers(headers)
if validate_headers:
_validate_headers(headers)
df.columns = df.columns.str.strip()
columns = [df[header].tolist() for header in headers]
return Resource(
Expand Down Expand Up @@ -439,11 +440,12 @@ def read_csv(csv_data: Union[str, TextIO, BytesIO]) -> pandas.DataFrame:
return pd.read_csv(stream, comment="#", float_precision="round_trip", skipinitialspace=True, thousands=" ")


def read_resource_from_string(resource_string: str) -> Resource:
def read_resource_from_string(resource_string: str, validate_headers: bool = True) -> Resource:
"""Read resource from stream without validation."""
resource_df = read_csv(resource_string)

return _dataframe_to_resource(resource_df.replace(np.nan, ""))
resource = _dataframe_to_resource(resource_df.replace(np.nan, ""), validate_headers=validate_headers)
return resource


def convert_dataframe_to_timeseries_resource(resource_df: pd.DataFrame) -> Resource:
Expand Down

0 comments on commit 883b7e6

Please sign in to comment.