Skip to content

Commit

Permalink
Add expression tests (#1894)
Browse files Browse the repository at this point in the history
  • Loading branch information
zundertj committed Nov 26, 2021
1 parent 1f85f31 commit fc7dad8
Show file tree
Hide file tree
Showing 7 changed files with 213 additions and 43 deletions.
2 changes: 1 addition & 1 deletion py-polars/polars/internals/construction.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def numpy_to_pyseries(
"""
Construct a PySeries from a numpy array.
"""
if not values.data.contiguous:
if not values.flags["C_CONTIGUOUS"]:
values = np.array(values)

if len(values.shape) == 1:
Expand Down
20 changes: 7 additions & 13 deletions py-polars/polars/internals/expr.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,7 @@ def _from_pyexpr(pyexpr: "PyExpr") -> "Expr":
return self

def __to_pyexpr(self, other: Any) -> "PyExpr":
if isinstance(other, PyExpr):
return other
elif isinstance(other, Expr):
return other._pyexpr
else:
return pli.lit(other)._pyexpr
return self.__to_expr(other)._pyexpr

def __to_expr(self, other: Any) -> "Expr":
if isinstance(other, Expr):
Expand Down Expand Up @@ -185,16 +180,13 @@ def __array_ufunc__(
else:
dtype = None # type: ignore

args = []
for inp in inputs:
if not isinstance(inp, Expr):
args.append(inp)
args = [inp for inp in inputs if not isinstance(inp, Expr)]

def function(s: "pli.Series") -> "pli.Series":
return ufunc(s, *args, **kwargs)
return ufunc(s, *args, **kwargs) # pragma: no cover

if "dtype" in kwargs:
return self.map(function, return_dtype=kwargs["dtype"])
dtype = kwargs["dtype"]

return self.map(function, return_dtype=dtype)

Expand Down Expand Up @@ -741,7 +733,9 @@ def sort_by(

return wrap_expr(self._pyexpr.sort_by(by, reverse))

def take(self, index: Union[tp.List[int], "Expr", "pli.Series"]) -> "Expr":
def take(
self, index: Union[tp.List[int], "Expr", "pli.Series", np.ndarray]
) -> "Expr":
"""
Take values by index.
Expand Down
9 changes: 9 additions & 0 deletions py-polars/tests/test_datelike.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from datetime import date, datetime, timedelta

import numpy as np
import pyarrow as pa
import pytest

Expand Down Expand Up @@ -153,6 +154,14 @@ def test_to_python_datetime() -> None:
)


def test_from_numpy() -> None:
# numpy support is limited; will be stored as object
x = np.asarray(range(100_000, 200_000, 10_000), dtype="datetime64[s]")
s = pl.Series(x)
assert s[0] == x[0]
assert len(s) == 10


def test_datetime_consistency() -> None:
# dt = datetime(2021, 1, 1, 10, 30, 45, 123456)
dt = datetime(2021, 1, 1, 10, 30, 45, 123000)
Expand Down
8 changes: 7 additions & 1 deletion py-polars/tests/test_exprs.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,13 @@ def test_horizontal_agg(fruits_cars: pl.DataFrame) -> None:
assert out[:, 0].to_list() == [1, 2, 3, 2, 1]


def test_prefix(fruits_cars: pl.DataFrame) -> None:
def test_suffix(fruits_cars: pl.DataFrame) -> None:
df = fruits_cars
out = df.select([pl.all().suffix("_reverse")])
assert out.columns == ["A_reverse", "fruits_reverse", "B_reverse", "cars_reverse"]


def test_prefix(fruits_cars: pl.DataFrame) -> None:
df = fruits_cars
out = df.select([pl.all().prefix("reverse_")])
assert out.columns == ["reverse_A", "reverse_fruits", "reverse_B", "reverse_cars"]
8 changes: 8 additions & 0 deletions py-polars/tests/test_interop.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,12 +111,20 @@ def test_from_arrow() -> None:
df = pl.from_arrow(data)
assert df.shape == (3, 2)

# if not a PyArrow type, raise a ValueError
with pytest.raises(ValueError):
_ = pl.from_arrow([1, 2]) # type: ignore


def test_from_pandas_dataframe() -> None:
pd_df = pd.DataFrame([[1, 2, 3], [4, 5, 6]], columns=["a", "b", "c"])
df = pl.from_pandas(pd_df)
assert df.shape == (2, 3)

# if not a Pandas dataframe, raise a ValueError
with pytest.raises(ValueError):
_ = pl.from_pandas([1, 2]) # type: ignore


def test_from_pandas_series() -> None:
pd_series = pd.Series([1, 2, 3], name="pd")
Expand Down

0 comments on commit fc7dad8

Please sign in to comment.