Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Additional methods and fixes to DataFrameMixin #43

Merged
merged 43 commits into from
Aug 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
24ce034
adding __repr__ and __str__ to DiscreteDF
adamamer20 Jul 24, 2024
f3d5201
implement random_pos in DiscreteSpaceDF
adamamer20 Jul 24, 2024
6e86635
Changing set_cells docstring
adamamer20 Jul 24, 2024
ff812c9
adding _df_columns to mixin
adamamer20 Jul 24, 2024
8b049ee
add _df_column_names
adamamer20 Jul 24, 2024
c2218c6
- move capacity to DiscreteSpaceDF
adamamer20 Jul 24, 2024
6436186
adding _contains to mixin, changed _add_columns to _with_columns
adamamer20 Jul 24, 2024
0777041
- moved column names to SpaceDF
adamamer20 Jul 24, 2024
3e3f457
add _srs_range
adamamer20 Jul 24, 2024
00a5e1a
add _df_join to mixin
adamamer20 Jul 24, 2024
dee33f5
adding "cross" option to _df_join
adamamer20 Jul 24, 2024
fe2abfb
adding _df_filter_ to mixin
adamamer20 Jul 25, 2024
e47742c
add _df_rename_columns to DataFrameMixin
adamamer20 Jul 25, 2024
365b240
adding get_neighborhood to GridDF
adamamer20 Jul 25, 2024
16dfd5d
- remove _place_agents_df and move it to move_agents
adamamer20 Jul 25, 2024
49e5617
fix move update capacity first
adamamer20 Jul 25, 2024
de5aaf6
pandas implementation
adamamer20 Jul 25, 2024
6d185f4
move pandas, polars tests to their folders
adamamer20 Jul 25, 2024
46500a7
adding GridPandas to __init__
adamamer20 Jul 25, 2024
12bae83
adding remaining capacity
adamamer20 Jul 25, 2024
00cb4be
reorder mixin
adamamer20 Jul 25, 2024
71281cf
adding mixin_test for pandas
adamamer20 Jul 26, 2024
0363e67
fixes to abstract DataFrameMixin syntax
adamamer20 Jul 26, 2024
d26404e
updates to types
adamamer20 Jul 26, 2024
1794388
renamed files for pytest
adamamer20 Jul 26, 2024
499e4e2
adding typeguard to mixin
adamamer20 Jul 26, 2024
3943365
added series concatenation to DataFrameMixin
adamamer20 Jul 26, 2024
fd1b5f1
adding place_agents
adamamer20 Jul 26, 2024
91bc4aa
renaming test_agentsets (for pytest compatibility)
adamamer20 Jul 26, 2024
96307e6
added index as abstract property and changed inactive_agents mismatch…
adamamer20 Aug 1, 2024
b33d083
- changed index_col to index_cols across methods
adamamer20 Aug 1, 2024
5c07e53
- new methods in PandasMixin based on additions to DataFrameMixin
adamamer20 Aug 1, 2024
b43449c
- Distinction between place and move: the first raises Warning if age…
adamamer20 Aug 1, 2024
054ebf9
addition of the new methods to PolarsMixin according to the new abstr…
adamamer20 Aug 1, 2024
7e5bca4
- added index property to AgentsDF
adamamer20 Aug 1, 2024
e6e8f2a
- specified _copy_with_method attribute for fast copy through CopyMixin
adamamer20 Aug 1, 2024
2b181e7
added tests for GridPandas
adamamer20 Aug 1, 2024
34a6368
Merge branch 'main' of https://github.com/adamamer20/mesa-frames into…
adamamer20 Aug 1, 2024
2a2284e
Merge branch 'main' of https://github.com/adamamer20/mesa-frames into…
adamamer20 Aug 1, 2024
b166d66
Merge branch 'main' of https://github.com/adamamer20/mesa-frames into…
adamamer20 Aug 1, 2024
5106501
test_mixin_pandas will be added with it's own PR
adamamer20 Aug 1, 2024
e90cacd
moved test_space_pandas to pandas test folder and changed named to te…
adamamer20 Aug 1, 2024
02b3ab4
Revert space files to main/origin (will have their own PR)
adamamer20 Aug 1, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
214 changes: 199 additions & 15 deletions mesa_frames/abstract/mixin.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
from abc import ABC, abstractmethod
from collections.abc import Collection, Iterator, Sequence
from copy import copy, deepcopy

from typing_extensions import Any, Self
from typing import Literal
from collections.abc import Collection, Iterator, Sequence

from mesa_frames.types_ import BoolSeries, DataFrame, Mask, Series
from typing_extensions import Any, Self, overload

from collections.abc import Hashable

from mesa_frames.types_ import BoolSeries, DataFrame, Index, Mask, Series


class CopyMixin(ABC):
Expand Down Expand Up @@ -149,38 +151,119 @@ def __deepcopy__(self, memo: dict) -> Self:


class DataFrameMixin(ABC):
def _df_remove(self, df: DataFrame, mask: Mask, index_cols: str) -> DataFrame:
return self._df_get_masked_df(df, index_cols, mask, negate=True)

@abstractmethod
def _df_add_columns(
self, original_df: DataFrame, new_columns: list[str], data: Any
def _df_add(
self,
df: DataFrame,
other: DataFrame | Sequence[float | int],
axis: Literal["index", "columns"] = "index",
index_cols: str | list[str] | None = None,
) -> DataFrame: ...

@abstractmethod
def _df_all(
self,
df: DataFrame,
name: str,
axis: str = "columns",
index_cols: str | list[str] | None = None,
) -> DataFrame: ...

@abstractmethod
def _df_column_names(self, df: DataFrame) -> list[str]: ...

@abstractmethod
def _df_combine_first(
self, original_df: DataFrame, new_df: DataFrame, index_cols: list[str]
self, original_df: DataFrame, new_df: DataFrame, index_cols: str | list[str]
) -> DataFrame: ...

@overload
@abstractmethod
def _df_concat(
self,
dfs: Collection[DataFrame],
objs: Collection[Series],
how: Literal["horizontal"] | Literal["vertical"] = "vertical",
ignore_index: bool = False,
index_cols: str | None = None,
) -> Series: ...

@overload
@abstractmethod
def _df_concat(
self,
objs: Collection[DataFrame],
how: Literal["horizontal"] | Literal["vertical"] = "vertical",
ignore_index: bool = False,
index_cols: str | None = None,
) -> DataFrame: ...

@abstractmethod
def _df_concat(
self,
objs: Collection[DataFrame] | Collection[Series],
how: Literal["horizontal"] | Literal["vertical"] = "vertical",
ignore_index: bool = False,
index_cols: str | None = None,
) -> DataFrame | Series: ...

@abstractmethod
def _df_contains(
self,
df: DataFrame,
column: str,
values: Sequence[Any],
) -> BoolSeries: ...

@abstractmethod
def _df_constructor(
self,
data: Sequence[Sequence] | dict[str | Any] | None = None,
columns: list[str] | None = None,
index_col: str | list[str] | None = None,
index: Index | None = None,
index_cols: str | list[str] | None = None,
dtypes: dict[str, Any] | None = None,
) -> DataFrame: ...

@abstractmethod
def _df_div(
self,
df: DataFrame,
other: DataFrame | Sequence[float | int],
axis: Literal["index", "columns"] = "index",
index_cols: str | list[str] | None = None,
) -> DataFrame: ...

@abstractmethod
def _df_drop_columns(
self,
df: DataFrame,
columns: str | list[str],
) -> DataFrame: ...

@abstractmethod
def _df_drop_duplicates(
self,
df: DataFrame,
subset: str | list[str] | None = None,
keep: Literal["first", "last", False] = "first",
) -> DataFrame: ...

@abstractmethod
def _df_filter(
self,
df: DataFrame,
condition: BoolSeries,
all: bool = True,
) -> DataFrame: ...

@abstractmethod
def _df_get_bool_mask(
self,
df: DataFrame,
index_col: str,
index_cols: str | list[str],
mask: Mask | None = None,
negate: bool = False,
) -> BoolSeries: ...
Expand All @@ -189,21 +272,88 @@ def _df_get_bool_mask(
def _df_get_masked_df(
self,
df: DataFrame,
index_col: str,
index_cols: str,
mask: Mask | None = None,
columns: list[str] | None = None,
columns: str | list[str] | None = None,
negate: bool = False,
) -> DataFrame: ...

@abstractmethod
def _df_groupby_cumcount(
self,
df: DataFrame,
by: str | list[str],
) -> Series: ...

@abstractmethod
def _df_iterator(self, df: DataFrame) -> Iterator[dict[str, Any]]: ...

@abstractmethod
def _df_norm(self, df: DataFrame) -> DataFrame: ...
def _df_join(
self,
left: DataFrame,
right: DataFrame,
index_cols: str | list[str] | None = None,
on: str | list[str] | None = None,
left_on: str | list[str] | None = None,
right_on: str | list[str] | None = None,
how: Literal["left"]
| Literal["right"]
| Literal["inner"]
| Literal["outer"]
| Literal["cross"] = "left",
suffix="_right",
) -> DataFrame: ...

@abstractmethod
def _df_mul(
self,
df: DataFrame,
other: DataFrame | Sequence[float | int],
axis: Literal["index", "columns"] = "index",
index_cols: str | list[str] | None = None,
) -> DataFrame: ...

@abstractmethod
@overload
def _df_norm(
self,
df: DataFrame,
srs_name: str = "norm",
include_cols: Literal[False] = False,
) -> Series: ...

@abstractmethod
@overload
def _df_norm(
self,
df: DataFrame,
srs_name: str = "norm",
include_cols: Literal[True] = False,
) -> DataFrame: ...

@abstractmethod
def _df_norm(
self,
df: DataFrame,
srs_name: str = "norm",
include_cols: bool = False,
) -> Series | DataFrame: ...

@abstractmethod
def _df_remove(
self, df: DataFrame, ids: Sequence[Any], index_col: str | None = None
def _df_rename_columns(
self,
df: DataFrame,
old_columns: list[str],
new_columns: list[str],
) -> DataFrame: ...

@abstractmethod
def _df_reset_index(
self,
df: DataFrame,
index_cols: str | list[str] | None = None,
drop: bool = False,
) -> DataFrame: ...

@abstractmethod
Expand All @@ -217,6 +367,27 @@ def _df_sample(
seed: int | None = None,
) -> DataFrame: ...

@abstractmethod
def _df_set_index(
self,
df: DataFrame,
index_name: str,
new_index: Sequence[Hashable] | None = None,
) -> DataFrame: ...

@abstractmethod
def _df_with_columns(
self,
original_df: DataFrame,
data: DataFrame
| Series
| Sequence[Sequence]
| dict[str | Any]
| Sequence[Any]
| Any,
new_columns: str | list[str] | None = None,
) -> DataFrame: ...

@abstractmethod
def _srs_constructor(
self,
Expand All @@ -225,3 +396,16 @@ def _srs_constructor(
dtype: Any | None = None,
index: Sequence[Any] | None = None,
) -> Series: ...

@abstractmethod
def _srs_contains(
self,
srs: Sequence[Any],
values: Any | Sequence[Any],
) -> BoolSeries: ...

@abstractmethod
def _srs_range(self, name: str, start: int, end: int, step: int = 1) -> Series: ...

@abstractmethod
def _srs_to_df(self, srs: Series, index: Index | None = None) -> DataFrame: ...
Loading