-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7174954
commit 53b6550
Showing
3 changed files
with
45 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
"""Dataframe utilities""" | ||
|
||
from __future__ import annotations | ||
from pathlib import Path | ||
import typing as ty | ||
from koyo.typing import PathLike | ||
|
||
if ty.TYPE_CHECKING: | ||
import pandas as pd | ||
|
||
|
||
def read_csv_with_comments(path: PathLike) -> pd.DataFrame: | ||
"""Read CSV with comments.""" | ||
import pandas as pd | ||
|
||
path = Path(path) | ||
try: | ||
df = pd.read_csv(path) | ||
first_col = df.iloc[:, 0] | ||
# check whether any of the values start with # (comment) | ||
for value in first_col: | ||
if value.startswith("# "): | ||
df = df[~first_col.str.contains("# ")] | ||
df.reset_index(drop=True, inplace=True) | ||
df.columns = df.iloc[0] | ||
df.drop(df.index[0], inplace=True) | ||
break | ||
except pd.errors.ParserError: | ||
from io import StringIO | ||
|
||
data = path.read_text().split("\n") | ||
start_index, end_index = 0, 0 | ||
for row in data: | ||
if row.startswith("#"): | ||
start_index += 1 | ||
continue | ||
elif not row: | ||
end_index += 1 | ||
df = pd.read_csv(StringIO("\n".join(data[start_index:-end_index])), sep=",") | ||
except Exception: | ||
raise pd.errors.ParserError(f"Failed to parse grid '{path}'.") | ||
return df |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters