Skip to content

Instantly share code, notes, and snippets.

@tsvikas
Last active December 23, 2024 15:49
Show Gist options
  • Save tsvikas/51bf2e59a5b541dff8d7f51a71d2de69 to your computer and use it in GitHub Desktop.
Save tsvikas/51bf2e59a5b541dff8d7f51a71d2de69 to your computer and use it in GitHub Desktop.
safe predictable pd.concat
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