Skip to content

Commit

Permalink
docs: code samples for drop and fillna (#284)
Browse files Browse the repository at this point in the history
  • Loading branch information
shobsi authored and Genesis929 committed Dec 26, 2023
1 parent 0b262c3 commit 8ea4a66
Show file tree
Hide file tree
Showing 2 changed files with 222 additions and 0 deletions.
141 changes: 141 additions & 0 deletions third_party/bigframes_vendored/pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -869,6 +869,97 @@ def drop(
Remove columns by directly specifying column names.
**Examples:**
>>> import bigframes.pandas as bpd
>>> bpd.options.display.progress_bar = None
>>> df = bpd.DataFrame(np.arange(12).reshape(3, 4),
... columns=['A', 'B', 'C', 'D'])
>>> df
A B C D
0 0 1 2 3
1 4 5 6 7
2 8 9 10 11
<BLANKLINE>
[3 rows x 4 columns]
Drop columns:
>>> df.drop(['B', 'C'], axis=1)
A D
0 0 3
1 4 7
2 8 11
<BLANKLINE>
[3 rows x 2 columns]
>>> df.drop(columns=['B', 'C'])
A D
0 0 3
1 4 7
2 8 11
<BLANKLINE>
[3 rows x 2 columns]
Drop a row by index:
>>> df.drop([0, 1])
A B C D
2 8 9 10 11
<BLANKLINE>
[1 rows x 4 columns]
Drop columns and/or rows of MultiIndex DataFrame:
>>> import pandas as pd
>>> midx = pd.MultiIndex(levels=[['llama', 'cow', 'falcon'],
... ['speed', 'weight', 'length']],
... codes=[[0, 0, 0, 1, 1, 1, 2, 2, 2],
... [0, 1, 2, 0, 1, 2, 0, 1, 2]])
>>> df = bpd.DataFrame(index=midx, columns=['big', 'small'],
... data=[[45, 30], [200, 100], [1.5, 1], [30, 20],
... [250, 150], [1.5, 0.8], [320, 250],
... [1, 0.8], [0.3, 0.2]])
>>> df
big small
llama speed 45.0 30.0
weight 200.0 100.0
length 1.5 1.0
cow speed 30.0 20.0
weight 250.0 150.0
length 1.5 0.8
falcon speed 320.0 250.0
weight 1.0 0.8
length 0.3 0.2
<BLANKLINE>
[9 rows x 2 columns]
Drop a specific index and column combination from the MultiIndex
DataFrame, i.e., drop the index ``'cow'`` and column ``'small'``:
>>> df.drop(index='cow', columns='small')
big
llama speed 45.0
weight 200.0
length 1.5
falcon speed 320.0
weight 1.0
length 0.3
<BLANKLINE>
[6 rows x 1 columns]
>>> df.drop(index='length', level=1)
big small
llama speed 45.0 30.0
weight 200.0 100.0
cow speed 30.0 20.0
weight 250.0 150.0
falcon speed 320.0 250.0
weight 1.0 0.8
<BLANKLINE>
[6 rows x 2 columns]
Args:
labels:
Index or column labels to drop.
Expand Down Expand Up @@ -4343,6 +4434,56 @@ def fillna(self, value):
"""
Fill NA/NaN values using the specified method.
**Examples:**
>>> import bigframes.pandas as bpd
>>> bpd.options.display.progress_bar = None
>>> df = bpd.DataFrame([[np.nan, 2, np.nan, 0],
... [3, 4, np.nan, 1],
... [np.nan, np.nan, np.nan, np.nan],
... [np.nan, 3, np.nan, 4]],
... columns=list("ABCD")).astype("Float64")
>>> df
A B C D
0 <NA> 2.0 <NA> 0.0
1 3.0 4.0 <NA> 1.0
2 <NA> <NA> <NA> <NA>
3 <NA> 3.0 <NA> 4.0
<BLANKLINE>
[4 rows x 4 columns]
Replace all NA elements with 0s.
>>> df.fillna(0)
A B C D
0 0.0 2.0 0.0 0.0
1 3.0 4.0 0.0 1.0
2 0.0 0.0 0.0 0.0
3 0.0 3.0 0.0 4.0
<BLANKLINE>
[4 rows x 4 columns]
You can use fill values from another DataFrame:
>>> df_fill = bpd.DataFrame(np.arange(12).reshape(3, 4),
... columns=['A', 'B', 'C', 'D'])
>>> df_fill
A B C D
0 0 1 2 3
1 4 5 6 7
2 8 9 10 11
<BLANKLINE>
[3 rows x 4 columns]
>>> df.fillna(df_fill)
A B C D
0 0.0 2.0 2.0 0.0
1 3.0 4.0 6.0 1.0
2 8.0 9.0 10.0 11.0
3 <NA> 3.0 <NA> 4.0
<BLANKLINE>
[4 rows x 4 columns]
Args:
value (scalar, Series):
Value to use to fill holes (e.g. 0), alternately a
Expand Down
81 changes: 81 additions & 0 deletions third_party/bigframes_vendored/pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -1062,6 +1062,55 @@ def drop(
When using a multi-index, labels on different levels can be removed
by specifying the level.
**Examples:**
>>> import bigframes.pandas as bpd
>>> bpd.options.display.progress_bar = None
>>> s = bpd.Series(data=np.arange(3), index=['A', 'B', 'C'])
>>> s
A 0
B 1
C 2
dtype: Int64
Drop labels B and C:
>>> s.drop(labels=['B', 'C'])
A 0
dtype: Int64
Drop 2nd level label in MultiIndex Series:
>>> import pandas as pd
>>> midx = pd.MultiIndex(levels=[['llama', 'cow', 'falcon'],
... ['speed', 'weight', 'length']],
... codes=[[0, 0, 0, 1, 1, 1, 2, 2, 2],
... [0, 1, 2, 0, 1, 2, 0, 1, 2]])
>>> s = bpd.Series([45, 200, 1.2, 30, 250, 1.5, 320, 1, 0.3],
... index=midx)
>>> s
llama speed 45.0
weight 200.0
length 1.2
cow speed 30.0
weight 250.0
length 1.5
falcon speed 320.0
weight 1.0
length 0.3
dtype: Float64
>>> s.drop(labels='weight', level=1)
llama speed 45.0
length 1.2
cow speed 30.0
length 1.5
falcon speed 320.0
length 0.3
dtype: Float64
Args:
labels (single label or list-like):
Index labels to drop.
Expand Down Expand Up @@ -1193,6 +1242,38 @@ def fillna(
"""
Fill NA/NaN values using the specified method.
**Examples:**
>>> import bigframes.pandas as bpd
>>> bpd.options.display.progress_bar = None
>>> s = bpd.Series([np.nan, 2, np.nan, -1])
>>> s
0 <NA>
1 2.0
2 <NA>
3 -1.0
dtype: Float64
Replace all NA elements with 0s.
>>> s.fillna(0)
0 0.0
1 2.0
2 0.0
3 -1.0
dtype: Float64
You can use fill values from another Series:
>>> s_fill = bpd.Series([11, 22, 33])
>>> s.fillna(s_fill)
0 11.0
1 2.0
2 33.0
3 -1.0
dtype: Float64
Args:
value (scalar, dict, Series, or DataFrame, default None):
Value to use to fill holes (e.g. 0).
Expand Down

0 comments on commit 8ea4a66

Please sign in to comment.