Skip to content

Commit

Permalink
Fast/cheap empty clone ops (#3883)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-beedie committed Jul 4, 2022
1 parent dddd95e commit 78c769c
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 4 deletions.
1 change: 1 addition & 0 deletions py-polars/docs/source/reference/dataframe.rst
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ Manipulation/ selection
.. autosummary::
:toctree: api/

DataFrame.cleared
DataFrame.clone
DataFrame.distinct
DataFrame.drop
Expand Down
1 change: 1 addition & 0 deletions py-polars/docs/source/reference/lazyframe.rst
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ Manipulation/ selection
.. autosummary::
:toctree: api/

LazyFrame.cleared
LazyFrame.clone
LazyFrame.distinct
LazyFrame.drop
Expand Down
1 change: 1 addition & 0 deletions py-polars/docs/source/reference/series.rst
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ Manipulation/ selection
Series.argsort
Series.cast
Series.ceil
Series.cleared
Series.clip
Series.clone
Series.drop_nans
Expand Down
8 changes: 7 additions & 1 deletion py-polars/polars/internals/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -4081,9 +4081,15 @@ def select_at_idx(self, idx: int) -> pli.Series:
"""
return pli.wrap_s(self._df.select_at_idx(idx))

def cleared(self: DF) -> DF:
"""
Create an empty copy of the current DataFrame, with identical schema but no data.
"""
return self.head(0) if len(self) > 0 else self.clone()

def clone(self: DF) -> DF:
"""
Cheap deepcopy/clone.
Very cheap deepcopy/clone.
"""
return self._from_pydf(self._df.clone())

Expand Down
8 changes: 7 additions & 1 deletion py-polars/polars/internals/lazy_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -746,9 +746,15 @@ def cache(self: LDF) -> LDF:
"""
return self._from_pyldf(self._ldf.cache())

def cleared(self: LDF) -> LDF:
"""
Create an empty copy of the current LazyFrame, with identical schema but no data.
"""
return self._dataframe_class(columns=self.schema).lazy()

def clone(self: LDF) -> LDF:
"""
Cheap deepcopy/clone.
Very cheap deepcopy/clone.
"""
return self._from_pyldf(self._ldf.clone())

Expand Down
10 changes: 8 additions & 2 deletions py-polars/polars/internals/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -2352,9 +2352,15 @@ def set_at_idx(

return wrap_s(f(idx_array, value))

def clone(self) -> Series:
def cleared(self) -> "Series":
"""
Cheap deep clones.
Create an empty copy of the current Series, with identical name/dtype but no data.
"""
return self.limit(0) if len(self) > 0 else self.clone()

def clone(self) -> "Series":
"""
Very cheap deepcopy/clone.
"""
return wrap_s(self._s.clone())

Expand Down
6 changes: 6 additions & 0 deletions py-polars/tests/test_df.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ def test_init_only_columns() -> None:
assert df.dtypes == [pl.Date, pl.UInt64, pl.Int8, pl.List]
assert getattr(df.schema["d"], "inner") == pl.UInt8

dfe = df.cleared()
assert (df.schema == dfe.schema) and (dfe.shape == df.shape)


def test_special_char_colname_init() -> None:
from string import punctuation
Expand Down Expand Up @@ -173,6 +176,9 @@ def test_init_dict() -> None:
)
assert df.schema == {"c": pl.Int8, "d": pl.Int16}

dfe = df.cleared()
assert (df.schema == dfe.schema) and (len(dfe) == 0)


def test_init_ndarray() -> None:
# Empty array
Expand Down
3 changes: 3 additions & 0 deletions py-polars/tests/test_lazy.py
Original file line number Diff line number Diff line change
Expand Up @@ -1258,6 +1258,9 @@ def test_lazy_schema() -> None:
).lazy()
assert lf.dtypes == [pl.Int64, pl.Float64, pl.Utf8]

lfe = lf.cleared()
assert lfe.schema == lf.schema


def test_deadlocks_3409() -> None:
assert (
Expand Down
5 changes: 5 additions & 0 deletions py-polars/tests/test_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -630,6 +630,11 @@ def test_empty() -> None:
pl.Series(dtype=pl.Int32), pl.Series(dtype=pl.Int64), check_dtype=False
)

a = pl.Series(name="a", values=[1, 2, 3], dtype=pl.Int16)
empty_a = a.cleared()
assert a.dtype == empty_a.dtype
assert len(empty_a) == 0


def test_describe() -> None:
num_s = pl.Series([1, 2, 3])
Expand Down

0 comments on commit 78c769c

Please sign in to comment.