Skip to content

Commit

Permalink
More python documentation (#1414)
Browse files Browse the repository at this point in the history
  • Loading branch information
Romano Vacca committed Sep 22, 2021
1 parent 2c9d59f commit dff0876
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 1 deletion.
36 changes: 35 additions & 1 deletion py-polars/polars/eager/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -790,7 +790,7 @@ def to_csv(
>>> "bar": [6, 7, 8, 9, 10],
>>> "ham": ['a', 'b', 'c', 'd','e']
>>> })
>>> df.to_csv('new_file.csv', sep=',')
>>> df.to_csv('new_file.csv', delimiter=',')
"""
if file is None:
Expand Down Expand Up @@ -1837,6 +1837,40 @@ def downsample(self, by: Union[str, tp.List[str]], rule: str, n: int) -> "GroupB
n
Number of units (e.g. 5 "day", 15 "minute".
Examples
--------
>>> df = pl.DataFrame(
>>> {
>>> "A": ["2020-01-01", "2020-01-02", "2020-01-03","2020-01-04","2020-01-05"],
>>> "B": [1.0, 8.0, 6.0, 2.0, 16.0, 10.0],
>>> "C": [3.0, 6.0, 9.0, 2.0, 13.0, 8.0],
>>> "D": [12.0, 5.0, 9.0, 2.0, 11.0, 2.0],
>>> }
>>> )
>>> df['A'] = df['A'].str.strptime(pl.datatypes.Date32, "%Y-%m-%d")
>>>
>>> df.downsample("A", rule="day", n=3).agg(
>>> {
>>> "B": "max",
>>> "C": "min",
>>> "D": "last"
>>> }
>>> )
shape: (3, 4)
┌──────────────┬───────┬───────┬────────┐
│ A ┆ B_max ┆ C_min ┆ D_last │
│ --- ┆ --- ┆ --- ┆ --- │
│ date32(days) ┆ f64 ┆ f64 ┆ f64 │
╞══════════════╪═══════╪═══════╪════════╡
│ 2019-12-31 ┆ 8 ┆ 3 ┆ 5 │
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌┤
│ 2020-01-03 ┆ 16 ┆ 2 ┆ 11 │
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌┤
│ 2020-01-06 ┆ 10 ┆ 8 ┆ 2 │
└──────────────┴───────┴───────┴────────┘
"""
return GroupBy(
self._df,
Expand Down
20 changes: 20 additions & 0 deletions py-polars/polars/eager/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -1567,6 +1567,26 @@ def cast(self, dtype: Type[DataType], strict: bool = True) -> "Series":
DataType to cast to
strict
Throw an error if a cast could not be done for instance due to an overflow
Examples
--------
>>> s = pl.Series("a", ["2020-01-01", "2020-01-02", "2020-01-03"])
shape: (3,)
Series: 'a' [str]
[
"2020-01-01"
"2020-01-02"
"2020-01-03"
]
>>> s.cast(pl.datatypes.Date32)git
shape: (3,)
Series: 'a' [date32]
[
2020-01-01
2020-01-02
2020-01-03
]
"""
if dtype == int:
dtype = Int64
Expand Down
33 changes: 33 additions & 0 deletions py-polars/polars/lazy/expr.py
Original file line number Diff line number Diff line change
Expand Up @@ -1305,6 +1305,8 @@ def rolling_mean(
will (optionally) be multiplied with the weights given by the `weight` vector. The resultingParameters
values will be aggregated to their sum.
Parameters
----------
window_size
The length of the window.
weight
Expand All @@ -1317,6 +1319,37 @@ def rolling_mean(
min_periods
The number of values in the window that should be non-null before computing a result.
If None, it will be set equal to window size.
Examples
--------
>>> df = pl.DataFrame(
>>> {
>>> "A": [1.0, 8.0, 6.0, 2.0, 16.0, 10.0]
>>> }
>>> )
>>> df.select([
>>> col("A").rolling_mean(window_size=2)
>>> ])
shape: (6, 1)
┌──────┐
│ A │
│ --- │
│ f64 │
╞══════╡
│ null │
├╌╌╌╌╌╌┤
│ 4.5 │
├╌╌╌╌╌╌┤
│ 7 │
├╌╌╌╌╌╌┤
│ 4 │
├╌╌╌╌╌╌┤
│ 9 │
├╌╌╌╌╌╌┤
│ 13 │
└──────┘
"""
if min_periods is None:
min_periods = window_size
Expand Down

0 comments on commit dff0876

Please sign in to comment.