From de279be5dba0a79554b5b319b66c5dd59568c8b9 Mon Sep 17 00:00:00 2001 From: Franco Masotti Date: Sun, 7 Apr 2024 16:28:50 +0200 Subject: [PATCH] Cleanup - Remove redundant docstrings examples - Skip docstring test --- narwhals/dataframe.py | 148 ++++-------------------------------------- 1 file changed, 14 insertions(+), 134 deletions(-) diff --git a/narwhals/dataframe.py b/narwhals/dataframe.py index 5f2b6a070..3cee13a7c 100644 --- a/narwhals/dataframe.py +++ b/narwhals/dataframe.py @@ -189,116 +189,6 @@ class DataFrame(BaseFrame): │ 1 ┆ 3 │ │ 2 ┆ 4 │ └─────┴─────┘ - - To specify a more detailed/specific frame schema you can supply the `schema` - parameter with a dictionary of (name,dtype) pairs... - - >>> data = {"col1": [0, 2], "col2": [3, 7]} - >>> df_pl2 = pl.DataFrame(data, schema={"col1": pl.Float32, "col2": pl.Int64}) - >>> df2 = nw.DataFrame(df_pl2) - >>> df2 - ┌─────────────────────────────────────────────────┐ - | Narwhals DataFrame | - | Use `narwhals.to_native()` to see native output | - └─────────────────────────────────────────────────┘ - >>> nw.to_native(df2) - shape: (2, 2) - ┌──────┬──────┐ - │ col1 ┆ col2 │ - │ --- ┆ --- │ - │ f32 ┆ i64 │ - ╞══════╪══════╡ - │ 0.0 ┆ 3 │ - │ 2.0 ┆ 7 │ - └──────┴──────┘ - - ...a sequence of (name,dtype) pairs... - - >>> data = {"col1": [1, 2], "col2": [3, 4]} - >>> df_pl3 = pl.DataFrame(data, schema=[("col1", pl.Float32), ("col2", pl.Int64)]) - >>> df3 = nw.DataFrame(df_pl3) - >>> df3 - ┌─────────────────────────────────────────────────┐ - | Narwhals DataFrame | - | Use `narwhals.to_native()` to see native output | - └─────────────────────────────────────────────────┘ - >>> nw.to_native(df3) - shape: (2, 2) - ┌──────┬──────┐ - │ col1 ┆ col2 │ - │ --- ┆ --- │ - │ f32 ┆ i64 │ - ╞══════╪══════╡ - │ 1.0 ┆ 3 │ - │ 2.0 ┆ 4 │ - └──────┴──────┘ - - ...or a list of typed Series. - - >>> data = [ - ... pl.Series("col1", [1, 2], dtype=pl.Float32), - ... pl.Series("col2", [3, 4], dtype=pl.Int64), - ... ] - >>> df_pl4 = pl.DataFrame(data) - >>> df4 = nw.DataFrame(df_pl4) - >>> df4 - ┌─────────────────────────────────────────────────┐ - | Narwhals DataFrame | - | Use `narwhals.to_native()` to see native output | - └─────────────────────────────────────────────────┘ - >>> nw.to_native(df4) - shape: (2, 2) - ┌──────┬──────┐ - │ col1 ┆ col2 │ - │ --- ┆ --- │ - │ f32 ┆ i64 │ - ╞══════╪══════╡ - │ 1.0 ┆ 3 │ - │ 2.0 ┆ 4 │ - └──────┴──────┘ - - Constructing a DataFrame from a numpy ndarray, specifying column names: - - >>> import numpy as np - >>> data = np.array([(1, 2), (3, 4)], dtype=np.int64) - >>> df_pl5 = pl.DataFrame(data, schema=["a", "b"], orient="col") - >>> df5 = nw.DataFrame(df_pl5) - >>> df5 - ┌─────────────────────────────────────────────────┐ - | Narwhals DataFrame | - | Use `narwhals.to_native()` to see native output | - └─────────────────────────────────────────────────┘ - >>> nw.to_native(df5) - shape: (2, 2) - ┌─────┬─────┐ - │ a ┆ b │ - │ --- ┆ --- │ - │ i64 ┆ i64 │ - ╞═════╪═════╡ - │ 1 ┆ 3 │ - │ 2 ┆ 4 │ - └─────┴─────┘ - - Constructing a DataFrame from a list of lists, row orientation inferred: - - >>> data = [[1, 2, 3], [4, 5, 6]] - >>> df_pl6 = pl.DataFrame(data, schema=["a", "b", "c"]) - >>> df6 = nw.DataFrame(df_pl6) - >>> df6 - ┌─────────────────────────────────────────────────┐ - | Narwhals DataFrame | - | Use `narwhals.to_native()` to see native output | - └─────────────────────────────────────────────────┘ - >>> nw.to_native(df6) - shape: (2, 3) - ┌─────┬─────┬─────┐ - │ a ┆ b ┆ c │ - │ --- ┆ --- ┆ --- │ - │ i64 ┆ i64 ┆ i64 │ - ╞═════╪═════╪═════╡ - │ 1 ┆ 2 ┆ 3 │ - │ 4 ┆ 5 ┆ 6 │ - └─────┴─────┴─────┘ """ def __init__( @@ -481,15 +371,9 @@ def to_dict(self, *, as_series: bool = True) -> dict[str, Any]: │ 4 ┆ apple ┆ 2 ┆ beetle ┆ 2 │ │ 5 ┆ banana ┆ 1 ┆ beetle ┆ -30 │ └─────┴────────┴─────┴────────┴──────────┘ - >>> import pprint - >>> pprint.pprint(df.to_dict(as_series=False)) - {'A': [1, 2, 3, 4, 5], - 'B': [5, 4, 3, 2, 1], - 'cars': ['beetle', 'audi', 'beetle', 'beetle', 'beetle'], - 'fruits': ['banana', 'banana', 'apple', 'apple', 'banana'], - 'optional': [28, 300, None, 2, -30]} - >>> p = pprint.pformat(df.to_dict(as_series=True)).replace('\t', ' ') - >>> print(p) + >>> df.to_dict(as_series=False) + {'A': [1, 2, 3, 4, 5], 'fruits': ['banana', 'banana', 'apple', 'apple', 'banana'], 'B': [5, 4, 3, 2, 1], 'cars': ['beetle', 'audi', 'beetle', 'beetle', 'beetle'], 'optional': [28, 300, None, 2, -30]} + >>> df.to_dict(as_series=True) # doctest: +SKIP {'A': shape: (5,) Series: 'A' [i64] [ @@ -498,8 +382,15 @@ def to_dict(self, *, as_series: bool = True) -> dict[str, Any]: 3 4 5 - ], - 'B': shape: (5,) + ], 'fruits': shape: (5,) + Series: 'fruits' [str] + [ + "banana" + "banana" + "apple" + "apple" + "banana" + ], 'B': shape: (5,) Series: 'B' [i64] [ 5 @@ -507,8 +398,7 @@ def to_dict(self, *, as_series: bool = True) -> dict[str, Any]: 3 2 1 - ], - 'cars': shape: (5,) + ], 'cars': shape: (5,) Series: 'cars' [str] [ "beetle" @@ -516,17 +406,7 @@ def to_dict(self, *, as_series: bool = True) -> dict[str, Any]: "beetle" "beetle" "beetle" - ], - 'fruits': shape: (5,) - Series: 'fruits' [str] - [ - "banana" - "banana" - "apple" - "apple" - "banana" - ], - 'optional': shape: (5,) + ], 'optional': shape: (5,) Series: 'optional' [i64] [ 28