Skip to content

Commit 313ebbe

Browse files
add docstrings
1 parent 98fe262 commit 313ebbe

File tree

3 files changed

+81
-53
lines changed

3 files changed

+81
-53
lines changed

bigframes/core/col.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
import dataclasses
1717
from typing import Any, Hashable
1818

19+
import bigframes_vendored.pandas.core.col as pd_col
20+
1921
import bigframes.core.expression as bf_expression
2022
import bigframes.operations as bf_ops
2123

@@ -24,6 +26,8 @@
2426
# Name collision unintended
2527
@dataclasses.dataclass(frozen=True)
2628
class Expression:
29+
__doc__ = pd_col.Expression.__doc__
30+
2731
_value: bf_expression.Expression
2832

2933
def _apply_unary(self, op: bf_ops.UnaryOp) -> Expression:
@@ -117,3 +121,6 @@ def __invert__(self) -> Expression:
117121

118122
def col(col_name: Hashable) -> Expression:
119123
return Expression(bf_expression.free_var(col_name))
124+
125+
126+
col.__doc__ = pd_col.col.__doc__
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Contains code from https://github.com/pandas-dev/pandas/blob/main/pandas/core/col.py
2+
from __future__ import annotations
3+
4+
from collections.abc import Hashable
5+
6+
from bigframes import constants
7+
8+
9+
class Expression:
10+
"""
11+
Class representing a deferred column.
12+
13+
This is not meant to be instantiated directly. Instead, use :meth:`pandas.col`.
14+
"""
15+
16+
17+
def col(col_name: Hashable) -> Expression:
18+
"""
19+
Generate deferred object representing a column of a DataFrame.
20+
21+
Any place which accepts ``lambda df: df[col_name]``, such as
22+
:meth:`DataFrame.assign` or :meth:`DataFrame.loc`, can also accept
23+
``pd.col(col_name)``.
24+
25+
Args:
26+
col_name (Hashable):
27+
Column name.
28+
29+
Returns:
30+
Expression:
31+
A deferred object representing a column of a DataFrame.
32+
"""
33+
raise NotImplementedError(constants.ABSTRACT_METHOD_ERROR_MESSAGE)
34+
35+
36+
__all__ = ["Expression", "col"]
Lines changed: 38 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,53 @@
11
# Contains code from https://github.com/pandas-dev/pandas/blob/main/pandas/core/common.py
22
from __future__ import annotations
33

4-
from typing import Callable, TYPE_CHECKING
4+
from collections.abc import Hashable
55

6-
from bigframes_vendored.pandas.core.dtypes.inference import iterable_not_string
6+
from bigframes import constants
77

8-
if TYPE_CHECKING:
9-
from bigframes_vendored.pandas.pandas._typing import T
108

9+
class Expression:
10+
"""
11+
Class representing a deferred column.
1112
12-
def pipe(
13-
obj, func: Callable[..., T] | tuple[Callable[..., T], str], *args, **kwargs
14-
) -> T:
13+
This is not meant to be instantiated directly. Instead, use :meth:`pandas.col`.
1514
"""
16-
Apply a function ``func`` to object ``obj`` either by passing obj as the
17-
first argument to the function or, in the case that the func is a tuple,
18-
interpret the first element of the tuple as a function and pass the obj to
19-
that function as a keyword argument whose key is the value of the second
20-
element of the tuple.
2115

22-
Args:
23-
func (callable or tuple of (callable, str)):
24-
Function to apply to this object or, alternatively, a
25-
``(callable, data_keyword)`` tuple where ``data_keyword`` is a
26-
string indicating the keyword of ``callable`` that expects the
27-
object.
28-
args (iterable, optional):
29-
Positional arguments passed into ``func``.
30-
kwargs (dict, optional):
31-
A dictionary of keyword arguments passed into ``func``.
3216

33-
Returns:
34-
object: the return type of ``func``.
35-
"""
36-
if isinstance(func, tuple):
37-
func, target = func
38-
if target in kwargs:
39-
msg = f"{target} is both the pipe target and a keyword argument"
40-
raise ValueError(msg)
41-
kwargs[target] = obj
42-
return func(*args, **kwargs)
43-
else:
44-
return func(obj, *args, **kwargs)
45-
46-
47-
def flatten(line):
17+
def col(col_name: Hashable) -> Expression:
4818
"""
49-
Flatten an arbitrarily nested sequence.
19+
Generate deferred object representing a column of a DataFrame.
20+
21+
Any place which accepts ``lambda df: df[col_name]``, such as
22+
:meth:`DataFrame.assign` or :meth:`DataFrame.loc`, can also accept
23+
``pd.col(col_name)``.
24+
25+
**Examples:**
26+
27+
You can use `col` in `assign`.
5028
51-
Parameters
52-
----------
53-
line : sequence
54-
The non string sequence to flatten
29+
>>> df = bpd.DataFrame({"name": ["beluga", "narwhal"], "speed": [100, 110]})
30+
>>> df.assign(name_titlecase=bpd.col("name").str.title())
31+
name speed name_titlecase
32+
0 beluga 100 Beluga
33+
1 narwhal 110 Narwhal
5534
56-
Notes
57-
-----
58-
This doesn't consider strings sequences.
35+
You can also use it for filtering.
5936
60-
Returns
61-
-------
62-
flattened : generator
37+
>>> df.loc[bpd.col("speed") > 105]
38+
name speed
39+
1 narwhal 110
40+
41+
42+
Args:
43+
col_name (Hashable):
44+
Column name.
45+
46+
Returns:
47+
Expression:
48+
A deferred object representing a column of a DataFrame.
6349
"""
64-
for element in line:
65-
if iterable_not_string(element):
66-
yield from flatten(element)
67-
else:
68-
yield element
50+
raise NotImplementedError(constants.ABSTRACT_METHOD_ERROR_MESSAGE)
51+
52+
53+
__all__ = ["Expression", "col"]

0 commit comments

Comments
 (0)