Last active
July 2, 2022 13:46
-
-
Save sgraaf/0f9c5971a62b5e08a21b71f15aec525a to your computer and use it in GitHub Desktop.
Pandas utility functions
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
import csv | |
from pathlib import Path | |
from typing import Optional, Union, Sequence, List, Literal | |
import pandas as pd | |
from tqdm import tqdm | |
def read_csv( | |
file: Path, | |
sep: str = ",", | |
header: Optional[Union[Sequence[int], int, Literal["infer"]]] = "infer", | |
names: Optional[List[str]] = None, | |
usecols: Optional[List[Union[int, str]]] = None, | |
encoding: Optional[str] = "utf-8", | |
) -> pd.DataFrame: | |
return pd.read_csv(file, sep=sep, header=header, usecols=usecols, names=names, encoding=encoding) | |
def read_csvs( | |
files: List[Path], | |
sep: str = ",", | |
header: Optional[Union[Sequence[int], int, Literal["infer"]]] = "infer", | |
names: Optional[List[str]] = None, | |
usecols: Optional[List[Union[int, str]]] = None, | |
encoding: Optional[str] = "utf-8", | |
use_tqdm: bool = False, | |
) -> pd.DataFrame: | |
if use_tqdm: | |
files = tqdm(files, desc="Reading CSV-files", total=len(files), unit="file") | |
return pd.concat( | |
[ | |
read_csv( | |
file, | |
sep=sep, | |
header=header, | |
names=names, | |
usecols=usecols, | |
encoding=encoding, | |
) | |
for file in files | |
] | |
) | |
def read_xlsx( | |
file: Path, | |
names: Optional[List[str]] = None, | |
usecols: Optional[List[Union[int, str]]] = None, | |
) -> pd.DataFrame: | |
return pd.read_excel(file, names=names, usecols=usecols) | |
def read_xlsxs( | |
files: List[Path], | |
names: Optional[List[str]] = None, | |
usecols: Optional[List[Union[int, str]]] = None, | |
use_tqdm: Optional[bool] = False, | |
) -> pd.DataFrame: | |
if use_tqdm: | |
files = tqdm(files, desc="Reading XLSX-files", total=len(files), unit="file") | |
return pd.concat([read_xlsx(file, names=names, usecols=usecols) for file in files]) | |
def write_csv( | |
df: pd.DataFrame, | |
file: Path, | |
sep: Optional[str] = ",", | |
encoding: Optional[str] = "utf-8", | |
quoting: Optional[int] = csv.QUOTE_MINIMAL, | |
quotechar: Optional[str] = '"', | |
decimal: Optional[str] = ".", | |
) -> None: | |
df.to_csv( | |
file, | |
sep=sep, | |
index=False, | |
encoding=encoding, | |
quoting=quoting, | |
quotechar=quotechar, | |
decimal=decimal, | |
) | |
def write_excel( | |
df: pd.DataFrame, file: Path, engine: Optional[str] = "openpyxl" | |
) -> None: | |
df.to_excel(file, index=False, engine=engine) | |
def sort_rows( | |
df: pd.DataFrame, by: Union[List[str], str], ascending: Optional[bool] = True | |
) -> pd.DataFrame: | |
return df.sort_values(by=by, ascending=ascending) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment