Last active
December 23, 2024 15:49
-
-
Save tsvikas/51bf2e59a5b541dff8d7f51a71d2de69 to your computer and use it in GitHub Desktop.
safe predictable pd.concat
This file contains hidden or 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 typing | |
from collections.abc import Iterable, Mapping | |
import pandas as pd | |
HashableT = typing.TypeVar("HashableT", bound=typing.Hashable) | |
T = TypeVar("T", pd.Series, pd.DataFrame) | |
def concat_list( | |
objs: Iterable[T], *, ignore_index: bool = False, skip_verify: bool = False, | |
) -> T: | |
obj = pd.concat( | |
objs=objs, | |
axis="index", | |
join="outer", | |
ignore_index=ignore_index, | |
names=None, | |
verify_integrity=not ignore_index and not skip_verify, | |
sort=False, | |
copy=True, | |
) | |
return obj | |
def concat_dict( | |
objs: Mapping[HashableT, T], *, names: list[HashableT] | None = None, | |
) -> T: | |
obj = pd.concat( | |
objs=objs, | |
axis="index", | |
join="outer", | |
ignore_index=False, | |
names=names, | |
verify_integrity=False, | |
sort=False, | |
copy=True, | |
) | |
return obj |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment