|
1 | 1 | # Contains code from https://github.com/pandas-dev/pandas/blob/main/pandas/core/common.py |
2 | 2 | from __future__ import annotations |
3 | 3 |
|
4 | | -from typing import Callable, TYPE_CHECKING |
| 4 | +from collections.abc import Hashable |
5 | 5 |
|
6 | | -from bigframes_vendored.pandas.core.dtypes.inference import iterable_not_string |
| 6 | +from bigframes import constants |
7 | 7 |
|
8 | | -if TYPE_CHECKING: |
9 | | - from bigframes_vendored.pandas.pandas._typing import T |
10 | 8 |
|
| 9 | +class Expression: |
| 10 | + """ |
| 11 | + Class representing a deferred column. |
11 | 12 |
|
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`. |
15 | 14 | """ |
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. |
21 | 15 |
|
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``. |
32 | 16 |
|
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: |
48 | 18 | """ |
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`. |
50 | 28 |
|
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 |
55 | 34 |
|
56 | | - Notes |
57 | | - ----- |
58 | | - This doesn't consider strings sequences. |
| 35 | + You can also use it for filtering. |
59 | 36 |
|
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. |
63 | 49 | """ |
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