Skip to content

Commit

Permalink
fix: Fix pickling polars.col on Python versions <3.11 (#21333)
Browse files Browse the repository at this point in the history
  • Loading branch information
nameexhaustion authored Feb 19, 2025
1 parent 109cc7e commit f97b46a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
12 changes: 12 additions & 0 deletions py-polars/polars/functions/col.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

import contextlib
import sys
from collections.abc import Iterable
from datetime import datetime, timedelta
from typing import TYPE_CHECKING
Expand All @@ -21,6 +22,9 @@
from polars._typing import PolarsDataType, PythonDataType
from polars.expr.expr import Expr

if not sys.version_info >= (3, 11):
from typing import Any

__all__ = ["col"]


Expand Down Expand Up @@ -362,5 +366,13 @@ def __getattr__(self, name: str) -> Expr:

return _create_col(name)

if not sys.version_info >= (3, 11):

def __getstate__(self) -> Any:
return self.__dict__

def __setstate__(self, state: Any) -> None:
self.__dict__ = state


col: Col = Col()
6 changes: 6 additions & 0 deletions py-polars/tests/unit/test_serde.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,3 +225,9 @@ def test_serde_empty_df_lazy_frame() -> None:
f.write(lf.serialize())
f.seek(0)
assert pl.LazyFrame.deserialize(f).collect().shape == (0, 0)


def test_pickle_class_objects_21021() -> None:
assert isinstance(pickle.loads(pickle.dumps(pl.col))("A"), pl.Expr)
assert isinstance(pickle.loads(pickle.dumps(pl.DataFrame))(), pl.DataFrame)
assert isinstance(pickle.loads(pickle.dumps(pl.LazyFrame))(), pl.LazyFrame)

0 comments on commit f97b46a

Please sign in to comment.