Skip to content

Commit

Permalink
docs: add Duration dtype docstring example (#1516)
Browse files Browse the repository at this point in the history
* docs: add Duration dtype docstring example

* fix: address review comments from PR #1516
  • Loading branch information
munsoor authored Dec 6, 2024
1 parent 7447ee8 commit dcd722e
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
18 changes: 18 additions & 0 deletions narwhals/dtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,24 @@ class Duration(TemporalType):
Notes:
Adapted from [Polars implementation](https://github.com/pola-rs/polars/blob/py-1.7.1/py-polars/polars/datatypes/classes.py#L460-L502)
Examples:
>>> import pandas as pd
>>> import polars as pl
>>> import pyarrow as pa
>>> import narwhals as nw
>>> from datetime import timedelta
>>> data = [timedelta(seconds=d) for d in range(1, 4)]
>>> ser_pd = pd.Series(data).astype("timedelta64[ms]")
>>> ser_pl = pl.Series(data).cast(pl.Duration("ms"))
>>> ser_pa = pa.chunked_array([data], type=pa.duration("ms"))
>>> nw.from_native(ser_pd, series_only=True).dtype
Duration(time_unit='ms')
>>> nw.from_native(ser_pl, series_only=True).dtype
Duration(time_unit='ms')
>>> nw.from_native(ser_pa, series_only=True).dtype
Duration(time_unit='ms')
"""

def __init__(
Expand Down
18 changes: 18 additions & 0 deletions narwhals/stable/v1/_dtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,24 @@ class Duration(NwDuration):
Notes:
Adapted from [Polars implementation](https://github.com/pola-rs/polars/blob/py-1.7.1/py-polars/polars/datatypes/classes.py#L460-L502)
Examples:
>>> import pandas as pd
>>> import polars as pl
>>> import pyarrow as pa
>>> import narwhals as nw
>>> from datetime import timedelta
>>> data = [timedelta(seconds=d) for d in range(1, 4)]
>>> ser_pd = pd.Series(data).astype("timedelta64[ms]")
>>> ser_pl = pl.Series(data).cast(pl.Duration("ms"))
>>> ser_pa = pa.chunked_array([data], type=pa.duration("ms"))
>>> nw.from_native(ser_pd, series_only=True).dtype
Duration(time_unit='ms')
>>> nw.from_native(ser_pl, series_only=True).dtype
Duration(time_unit='ms')
>>> nw.from_native(ser_pa, series_only=True).dtype
Duration(time_unit='ms')
"""

def __hash__(self) -> int:
Expand Down

0 comments on commit dcd722e

Please sign in to comment.