Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 52dfad2

Browse files
authoredNov 17, 2023
docs: code samples for Series.where and Series.mask (#217)
Thank you for opening a Pull Request! Before submitting your PR, there are a few things you can do to make sure it goes smoothly: - [ ] Make sure to open an issue as a [bug/issue](https://togithub.com/googleapis/python-bigquery-dataframes/issues/new/choose) before writing your code! That way we can discuss the change, evaluate designs, and agree on the general idea - [ ] Ensure the tests and linter pass - [ ] Code coverage does not decrease (if any source code was changed) - [x] Appropriate docs were updated - `Series.where`: https://screenshot.googleplex.com/9XWHpMnwrzVPF9G - `Series.mask`: https://screenshot.googleplex.com/4cPvvzoVaVzoCDD Fixes internal issue 310981880 🦕
1 parent 81125f9 commit 52dfad2

File tree

1 file changed

+114
-0
lines changed
  • third_party/bigframes_vendored/pandas/core

1 file changed

+114
-0
lines changed
 

‎third_party/bigframes_vendored/pandas/core/series.py

+114
Original file line numberDiff line numberDiff line change
@@ -1696,6 +1696,49 @@ def kurt(self):
16961696
def where(self, cond, other):
16971697
"""Replace values where the condition is False.
16981698
1699+
**Examples:**
1700+
1701+
>>> import bigframes.pandas as bpd
1702+
>>> bpd.options.display.progress_bar = None
1703+
1704+
>>> s = bpd.Series([10, 11, 12, 13, 14])
1705+
>>> s
1706+
0 10
1707+
1 11
1708+
2 12
1709+
3 13
1710+
4 14
1711+
dtype: Int64
1712+
1713+
You can filter the values in the Series based on a condition. The values
1714+
matching the condition would be kept, and not matching would be replaced.
1715+
The default replacement value is ``NA``.
1716+
1717+
>>> s.where(s % 2 == 0)
1718+
0 10
1719+
1 <NA>
1720+
2 12
1721+
3 <NA>
1722+
4 14
1723+
dtype: Int64
1724+
1725+
You can specify a custom replacement value for non-matching values.
1726+
1727+
>>> s.where(s % 2 == 0, -1)
1728+
0 10
1729+
1 -1
1730+
2 12
1731+
3 -1
1732+
4 14
1733+
dtype: Int64
1734+
>>> s.where(s % 2 == 0, 100*s)
1735+
0 10
1736+
1 1100
1737+
2 12
1738+
3 1300
1739+
4 14
1740+
dtype: Int64
1741+
16991742
Args:
17001743
cond (bool Series/DataFrame, array-like, or callable):
17011744
Where cond is True, keep the original value. Where False, replace
@@ -1720,6 +1763,77 @@ def where(self, cond, other):
17201763
def mask(self, cond, other):
17211764
"""Replace values where the condition is True.
17221765
1766+
**Examples:**
1767+
1768+
>>> import bigframes.pandas as bpd
1769+
>>> bpd.options.display.progress_bar = None
1770+
1771+
>>> s = bpd.Series([10, 11, 12, 13, 14])
1772+
>>> s
1773+
0 10
1774+
1 11
1775+
2 12
1776+
3 13
1777+
4 14
1778+
dtype: Int64
1779+
1780+
You can mask the values in the Series based on a condition. The values
1781+
matching the condition would be masked.
1782+
1783+
>>> s.mask(s % 2 == 0)
1784+
0 <NA>
1785+
1 11
1786+
2 <NA>
1787+
3 13
1788+
4 <NA>
1789+
dtype: Int64
1790+
1791+
You can specify a custom mask value.
1792+
1793+
>>> s.mask(s % 2 == 0, -1)
1794+
0 -1
1795+
1 11
1796+
2 -1
1797+
3 13
1798+
4 -1
1799+
dtype: Int64
1800+
>>> s.mask(s % 2 == 0, 100*s)
1801+
0 1000
1802+
1 11
1803+
2 1200
1804+
3 13
1805+
4 1400
1806+
dtype: Int64
1807+
1808+
You can also use a remote function to evaluate the mask condition. This
1809+
is useful in situation such as the following, where the mask
1810+
condition is evaluated based on a complicated business logic which cannot
1811+
be expressed in form of a Series.
1812+
1813+
>>> @bpd.remote_function([str], bool, reuse=False)
1814+
... def should_mask(name):
1815+
... hash = 0
1816+
... for char_ in name:
1817+
... hash += ord(char_)
1818+
... return hash % 2 == 0
1819+
1820+
>>> s = bpd.Series(["Alice", "Bob", "Caroline"])
1821+
>>> s
1822+
0 Alice
1823+
1 Bob
1824+
2 Caroline
1825+
dtype: string
1826+
>>> s.mask(should_mask)
1827+
0 <NA>
1828+
1 Bob
1829+
2 Caroline
1830+
dtype: string
1831+
>>> s.mask(should_mask, "REDACTED")
1832+
0 REDACTED
1833+
1 Bob
1834+
2 Caroline
1835+
dtype: string
1836+
17231837
Args:
17241838
cond (bool Series/DataFrame, array-like, or callable):
17251839
Where cond is False, keep the original value. Where True, replace

0 commit comments

Comments
 (0)
Failed to load comments.