Skip to content

Commit a64a677

Browse files
revert common.py
1 parent 313ebbe commit a64a677

File tree

1 file changed

+53
-38
lines changed
  • third_party/bigframes_vendored/pandas/core

1 file changed

+53
-38
lines changed
Lines changed: 53 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,68 @@
11
# Contains code from https://github.com/pandas-dev/pandas/blob/main/pandas/core/common.py
22
from __future__ import annotations
33

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

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

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-
"""
8+
if TYPE_CHECKING:
9+
from bigframes_vendored.pandas.pandas._typing import T
1510

1611

17-
def col(col_name: Hashable) -> Expression:
12+
def pipe(
13+
obj, func: Callable[..., T] | tuple[Callable[..., T], str], *args, **kwargs
14+
) -> T:
1815
"""
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`.
28-
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
34-
35-
You can also use it for filtering.
36-
37-
>>> df.loc[bpd.col("speed") > 105]
38-
name speed
39-
1 narwhal 110
40-
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.
4121
4222
Args:
43-
col_name (Hashable):
44-
Column name.
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``.
4532
4633
Returns:
47-
Expression:
48-
A deferred object representing a column of a DataFrame.
34+
object: the return type of ``func``.
4935
"""
50-
raise NotImplementedError(constants.ABSTRACT_METHOD_ERROR_MESSAGE)
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):
48+
"""
49+
Flatten an arbitrarily nested sequence.
50+
51+
Parameters
52+
----------
53+
line : sequence
54+
The non string sequence to flatten
5155
56+
Notes
57+
-----
58+
This doesn't consider strings sequences.
5259
53-
__all__ = ["Expression", "col"]
60+
Returns
61+
-------
62+
flattened : generator
63+
"""
64+
for element in line:
65+
if iterable_not_string(element):
66+
yield from flatten(element)
67+
else:
68+
yield element

0 commit comments

Comments
 (0)