Skip to content

Commit

Permalink
Fix various small things in Python API (#1849)
Browse files Browse the repository at this point in the history
  • Loading branch information
zundertj committed Nov 21, 2021
1 parent be19af6 commit 66dc057
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 28 deletions.
4 changes: 3 additions & 1 deletion py-polars/polars/cfg.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@


class Config:
"Configure polars"
"""
Configure polars
"""

@classmethod
def set_utf8_tables(cls) -> "Type[Config]":
Expand Down
31 changes: 20 additions & 11 deletions py-polars/polars/internals/expr.py
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,7 @@ def is_not(self) -> "Expr":
├╌╌╌╌╌╌╌┼╌╌╌╌╌╌┤
│ false ┆ null │
╰───────┴──────╯
>>> df.select(col("a").is_not())
>>> df.select(pl.col("a").is_not())
shape: (3, 1)
╭───────╮
│ a │
Expand Down Expand Up @@ -576,7 +576,7 @@ def drop_nulls(self) -> "Expr":
"""
Syntactic sugar for:
>>> col("foo").filter(col("foo").is_not_null())
>>> pl.col("foo").filter(pl.col("foo").is_not_null())
"""
return self.filter(self.is_not_null())

Expand Down Expand Up @@ -667,7 +667,7 @@ def cast(self, dtype: Type[Any], strict: bool = True) -> "Expr":
Parameters
----------
data_type
dtype
DataType to cast to
strict
Throw an error if a cast could not be done for instance due to an overflow
Expand Down Expand Up @@ -945,8 +945,8 @@ def over(self, expr: Union[str, "Expr", tp.List[Union["Expr", str]]]) -> "Expr":
>>>})
>>> (df.lazy()
>>> .select([
>>> col("groups")
>>> sum("values").over("groups"))
>>> pl.col("groups")
>>> sum("values").over("groups")
>>> ]).collect())
╭────────┬────────╮
│ groups ┆ values │
Expand Down Expand Up @@ -1021,7 +1021,14 @@ def filter(self, predicate: "Expr") -> "Expr":
return wrap_expr(self._pyexpr.filter(predicate._pyexpr))

def where(self, predicate: "Expr") -> "Expr":
"alias for filter"
"""
Alias for filter
Parameters
----------
predicate
Boolean expression.
"""
return self.filter(predicate)

def map(
Expand All @@ -1042,6 +1049,8 @@ def map(
Lambda/ function to apply.
return_dtype
Dtype of the output Series.
agg_list
"""
if return_dtype == str:
return_dtype = Utf8
Expand Down Expand Up @@ -1084,7 +1093,7 @@ def apply(
>>> df = pl.DataFrame({"a": [1, 2, 1, 1],
"b": ["a", "b", "c", "c"]})
>>> (df
>>> df
.lazy()
.groupby("b")
.agg([col("a").apply(lambda x: x.sum())])
Expand Down Expand Up @@ -1272,7 +1281,7 @@ def inspect(self, fmt: str = "{}") -> "Expr": # type: ignore
"""
Prints the value that this expression evaluates to and passes on the value.
>>> df.select(col("foo").cumsum().inspect("value is: {}").alias("bar"))
>>> df.select(pl.col("foo").cumsum().inspect("value is: {}").alias("bar"))
"""

def inspect(s: "pli.Series") -> "pli.Series":
Expand Down Expand Up @@ -1386,7 +1395,7 @@ def rolling_mean(
>>> }
>>> )
>>> df.select([
>>> col("A").rolling_mean(window_size=2)
>>> pl.col("A").rolling_mean(window_size=2)
>>> ])
shape: (6, 1)
┌──────┐
Expand Down Expand Up @@ -1542,7 +1551,7 @@ def rolling_apply(
>>> }
>>> )
>>> df.select([
>>> col("A").rolling_apply(3, lambda s: s.std())
>>> pl.col("A").rolling_apply(3, lambda s: s.std())
>>> ])
shape: (5, 1)
┌────────────────────┐
Expand Down Expand Up @@ -1752,7 +1761,7 @@ def str_concat(self, delimiter: str = "-") -> "Expr": # type: ignore
Examples
>>> df = pl.DataFrame({"foo": [1, None, 2]})
>>> df = df.select(col("foo").str_concat("-"))
>>> df = df.select(pl.col("foo").str_concat("-"))
shape: (1, 1)
┌──────────┐
│ foo │
Expand Down
12 changes: 7 additions & 5 deletions py-polars/polars/internals/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,8 @@ def read_csv(
- str -> all values encountered equal to this string will be null
- tp.List[str] -> A null value per column.
- Dict[str, str] -> A dictionary that maps column name to a null value string.
parse_dates
Whether to attempt to parse dates or not
Returns
-------
Expand Down Expand Up @@ -1156,7 +1158,7 @@ def __setitem__(self, key: Union[str, int, Tuple[Any, Any]], value: Any) -> None
elif isinstance(col_selection, str):
self.replace(col_selection, s)
else:
return NotImplemented
raise NotImplementedError

def __len__(self) -> int:
return self.height
Expand Down Expand Up @@ -1552,7 +1554,7 @@ def sort(
**Sort by multiple columns.**
For multiple columns we can also use expression syntax.
>>> df.sort([col("foo"), col("bar") ** 2], reverse=[True, False])
>>> df.sort([pl.col("foo"), pl.col("bar") ** 2], reverse=[True, False])
"""
if type(by) is list or isinstance(by, pli.Expr):
Expand Down Expand Up @@ -2201,12 +2203,12 @@ def with_column(self, column: Union["pli.Series", "pli.Expr"]) -> "DataFrame":

def with_column_renamed(self, existing_name: str, new_name: str) -> "DataFrame":
"""
Return a new DataFrame with the column added or replaced.
Return a new DataFrame with the column renamed.
Parameters
----------
column
Series, where the name of the Series refers to the column in the DataFrame.
existing_name
new_name
"""
return (
self.lazy()
Expand Down
4 changes: 2 additions & 2 deletions py-polars/polars/internals/lazy_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,9 +204,9 @@ def inspect(self, fmt: str = "{}") -> "LazyFrame": # type: ignore
"""
Prints the value that this node in the computation graph evaluates to and passes on the value.
>>> (df.select(col("foo").cumsum().alias("bar"))
>>> (df.select(pl.col("foo").cumsum().alias("bar"))
>>> .inspect() # print the node before the filter
>>> .filter(col("bar") == col("foo")))
>>> .filter(pl.col("bar") == pl.col("foo")))
"""

def inspect(s: pli.DataFrame) -> pli.DataFrame:
Expand Down
4 changes: 2 additions & 2 deletions py-polars/polars/internals/lazy_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -592,7 +592,7 @@ def map(
Parameters
----------
columns
exprs
Input Series to f
f
Function to apply over the input
Expand Down Expand Up @@ -627,7 +627,7 @@ def apply(
Parameters
----------
columns
exprs
Input Series to f
f
Function to apply over the input
Expand Down
14 changes: 7 additions & 7 deletions py-polars/polars/internals/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -678,14 +678,14 @@ def describe(self) -> "pli.DataFrame":
if self.len() == 0:
raise ValueError("Series must contain at least one value")
elif self.is_numeric():
self = self.cast(Float64)
s = self.cast(Float64)
stats = {
"min": self.min(),
"max": self.max(),
"null_count": self.null_count(),
"mean": self.mean(),
"std": self.std(),
"count": self.len(),
"min": s.min(),
"max": s.max(),
"null_count": s.null_count(),
"mean": s.mean(),
"std": s.std(),
"count": s.len(),
}
elif self.is_boolean():
stats = {
Expand Down
2 changes: 2 additions & 0 deletions py-polars/polars/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,8 @@ def scan_csv(
----------
file
Path to a file.
infer_schema_length
The number of rows Polars will read to try to determine the schema.
has_headers
If the CSV file has headers or not.
ignore_errors
Expand Down

0 comments on commit 66dc057

Please sign in to comment.