Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: code samples for Series.{map, to_list, count} #290

Merged
merged 4 commits into from
Dec 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions third_party/bigframes_vendored/pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,67 @@ def copy(self):
and indices. Modifications to the data or indices of the copy will not
be reflected in the original object.

**Examples:**

>>> import bigframes.pandas as bpd
>>> bpd.options.display.progress_bar = None

Modification in the original Series will not affect the copy Series:

>>> s = bpd.Series([1, 2], index=["a", "b"])
>>> s
a 1
b 2
dtype: Int64

>>> s_copy = s.copy()
>>> s_copy
a 1
b 2
dtype: Int64

>>> s.loc['b'] = 22
>>> s
a 1
b 22
dtype: Int64
>>> s_copy
a 1
b 2
dtype: Int64

Modification in the original DataFrame will not affect the copy DataFrame:

>>> df = bpd.DataFrame({'a': [1, 3], 'b': [2, 4]})
>>> df
a b
0 1 2
1 3 4
<BLANKLINE>
[2 rows x 2 columns]

>>> df_copy = df.copy()
>>> df_copy
a b
0 1 2
1 3 4
<BLANKLINE>
[2 rows x 2 columns]

>>> df.loc[df["b"] == 2, "b"] = 22
>>> df
a b
0 1 22.0
1 3 4.0
<BLANKLINE>
[2 rows x 2 columns]
>>> df_copy
a b
0 1 2
1 3 4
<BLANKLINE>
[2 rows x 2 columns]

Returns:
Object type matches caller.
"""
Expand Down
70 changes: 70 additions & 0 deletions third_party/bigframes_vendored/pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,21 @@ def tolist(self) -> list:
(for str, int, float) or a pandas scalar
(for Timestamp/Timedelta/Interval/Period).

**Examples:**

>>> import bigframes.pandas as bpd
>>> bpd.options.display.progress_bar = None

>>> s = bpd.Series([1, 2, 3])
>>> s
0 1
1 2
2 3
dtype: Int64

>>> s.to_list()
[1, 2, 3]

Returns:
list: list of the values
"""
Expand Down Expand Up @@ -560,6 +575,20 @@ def count(self):
"""
Return number of non-NA/null observations in the Series.

**Examples:**

>>> import bigframes.pandas as bpd
>>> bpd.options.display.progress_bar = None

>>> s = bpd.Series([0.0, 1.0, bpd.NA])
>>> s
0 0.0
1 1.0
2 <NA>
dtype: Float64
>>> s.count()
2

Returns:
int or Series (if level specified): Number of non-null values in the
Series.
Expand Down Expand Up @@ -2845,6 +2874,47 @@ def map(
``__missing__`` (i.e. provide a method for default values). These
are treated the same as ``dict``.

**Examples:**

>>> import bigframes.pandas as bpd
>>> bpd.options.display.progress_bar = None

>>> s = bpd.Series(['cat', 'dog', bpd.NA, 'rabbit'])
>>> s
0 cat
1 dog
2 <NA>
3 rabbit
dtype: string

`map` can accepts a `dict`. Values that are not found in the `dict` are
converted to `NA`:

>>> s.map({'cat': 'kitten', 'dog': 'puppy'})
0 kitten
1 puppy
2 <NA>
3 <NA>
dtype: string

It also accepts a remote function:

>>> @bpd.remote_function([str], str)
... def my_mapper(val):
... vowels = ["a", "e", "i", "o", "u"]
... if val:
... return "".join([
... ch.upper() if ch in vowels else ch for ch in val
... ])
... return "N/A"

>>> s.map(my_mapper)
0 cAt
1 dOg
2 N/A
3 rAbbIt
dtype: string

Args:
arg (function, Mapping, Series):
remote function, collections.abc.Mapping subclass or Series
Expand Down