Skip to content

Commit

Permalink
added pandas
Browse files Browse the repository at this point in the history
  • Loading branch information
DeaMariaLeon committed Oct 25, 2024
1 parent c021267 commit 1e06635
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 12 deletions.
27 changes: 20 additions & 7 deletions narwhals/_pandas_like/expr.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from narwhals._expression_parsing import reuse_series_implementation
from narwhals._expression_parsing import reuse_series_namespace_implementation
from narwhals._pandas_like.series import PandasLikeSeries
from narwhals.dependencies import is_numpy_array

if TYPE_CHECKING:
from typing_extensions import Self
Expand All @@ -16,7 +17,6 @@
from narwhals._pandas_like.namespace import PandasLikeNamespace
from narwhals.typing import DTypes
from narwhals.utils import Implementation
from narwhals.dtypes import DType


class PandasLikeExpr:
Expand Down Expand Up @@ -387,17 +387,30 @@ def gather_every(self: Self, n: int, offset: int = 0) -> Self:

def mode(self: Self) -> Self:
return reuse_series_implementation(self, "mode")

def map_batches(self, function: Callable[[Any], Self], return_dtype: DType | None = None, *args: Any, **kwargs: Any) -> Self:
breakpoint()

def map_batches(self: Self, function: Callable[[Any], Any]) -> Self:
def func(df: PandasLikeDataFrame) -> list[PandasLikeSeries]:
result = [function(series) for series in self._call(df)]
if is_numpy_array(result[0]):
output_names = (
self._output_names if self._output_names is not None else []
)
result = [
df.__narwhals_namespace__()
._create_compliant_series(array)
.alias(output_name)
for array, output_name in zip(result, output_names)
]
return result

return self.__class__(
lambda df: [function(series) for series in self._call(df)],
depth=self._depth + 1,
func,
depth=self._depth + 1, # correct depth or self.depth?
function_name=self._function_name + "->map_batches",
root_names=self._root_names,
output_names=self._output_names,
implementation=self._implementation,
backend_version=self._backend_version,
backend_version=self._backend_version,
dtypes=self._dtypes,
)

Expand Down
12 changes: 7 additions & 5 deletions narwhals/expr.py
Original file line number Diff line number Diff line change
Expand Up @@ -494,14 +494,16 @@ def std(self, *, ddof: int = 1) -> Self:
"""
return self.__class__(lambda plx: self._call(plx).std(ddof=ddof))




def map_batches(self, function: Callable[[Any], Self], return_dtype: DType | None = None, *args: Any, **kwargs: Any) -> Self:
def map_batches(
self,
function: Callable[[Any], Self],
return_dtype: DType | None = None,
*args: Any,
**kwargs: Any,
) -> Self:
return self.__class__(lambda plx: self._call(plx).map_batches(function))


def sum(self) -> Expr:
"""
Return the sum value.
Expand Down

0 comments on commit 1e06635

Please sign in to comment.