Skip to content

Commit f565bcd

Browse files
Genesis929ashleyxuushobsiTrevorBergeron
committed
docs: add example for dataframe.melt, dataframe.pivot, dataframe.stac… (#252)
* docs: add example for dataframe.melt, dataframe.pivot, dataframe.stack, dataframe.unstack * remove empty line * docstring fix * spacing update * docs: correct the params rendering for `ml.remote` and `ml.ensemble` modules (#248) 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 (if necessary) - `ensemble.RandomForestClassifier`: https://screenshot.googleplex.com/4Q88xgdm5hkaYXu - `ensemble.RandomForestRegressor`: https://screenshot.googleplex.com/3CU6pJBjYHQvnDo - `remote.VertexAIModel`: https://screenshot.googleplex.com/8SL2max6GfPMwFe Fixes internal issue 314150462 🦕 * docs: add examples for dataframe.nunique, dataframe.diff, dataframe.a… (#251) * docs: add examples for dataframe.nunique, dataframe.diff, dataframe.agg, dataframe.describe * update spacing * update ordering * docs: Fix return annotation in API docstrings (#253) 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) - [ ] Appropriate docs were updated (if necessary) Fixes internal issue 314367409 🦕 * feat: add nunique method to Series/DataFrameGroupby (#256) 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) - [ ] Appropriate docs were updated (if necessary) Fixes #<issue_number_goes_here> 🦕 * docs: add example for dataframe.melt, dataframe.pivot, dataframe.stack, dataframe.unstack * docstring fix --------- Co-authored-by: Ashley Xu <[email protected]> Co-authored-by: Shobhit Singh <[email protected]> Co-authored-by: TrevorBergeron <[email protected]>
1 parent 98517a2 commit f565bcd

File tree

1 file changed

+165
-14
lines changed
  • third_party/bigframes_vendored/pandas/core

1 file changed

+165
-14
lines changed

third_party/bigframes_vendored/pandas/core/frame.py

+165-14
Original file line numberDiff line numberDiff line change
@@ -3414,18 +3414,75 @@ def melt(self, id_vars, value_vars, var_name, value_name):
34143414
the row axis, leaving just two non-identifier columns, 'variable' and
34153415
'value'.
34163416
3417-
Parameters
3418-
----------
3419-
id_vars (tuple, list, or ndarray, optional):
3420-
Column(s) to use as identifier variables.
3421-
value_vars (tuple, list, or ndarray, optional):
3422-
Column(s) to unpivot. If not specified, uses all columns that
3423-
are not set as `id_vars`.
3424-
var_name (scalar):
3425-
Name to use for the 'variable' column. If None it uses
3426-
``frame.columns.name`` or 'variable'.
3427-
value_name (scalar, default 'value'):
3428-
Name to use for the 'value' column.
3417+
**Examples:**
3418+
3419+
>>> import bigframes.pandas as bpd
3420+
>>> bpd.options.display.progress_bar = None
3421+
3422+
>>> df = bpd.DataFrame({"A": [1, None, 3, 4, 5],
3423+
... "B": [1, 2, 3, 4, 5],
3424+
... "C": [None, 3.5, None, 4.5, 5.0]})
3425+
>>> df
3426+
A B C
3427+
0 1.0 1 <NA>
3428+
1 <NA> 2 3.5
3429+
2 3.0 3 <NA>
3430+
3 4.0 4 4.5
3431+
4 5.0 5 5.0
3432+
<BLANKLINE>
3433+
[5 rows x 3 columns]
3434+
3435+
Using `melt` without optional arguments:
3436+
3437+
>>> df.melt()
3438+
variable value
3439+
0 A 1.0
3440+
1 A <NA>
3441+
2 A 3.0
3442+
3 A 4.0
3443+
4 A 5.0
3444+
5 B 1.0
3445+
6 B 2.0
3446+
7 B 3.0
3447+
8 B 4.0
3448+
9 B 5.0
3449+
10 C <NA>
3450+
11 C 3.5
3451+
12 C <NA>
3452+
13 C 4.5
3453+
14 C 5.0
3454+
<BLANKLINE>
3455+
[15 rows x 2 columns]
3456+
3457+
Using `melt` with `id_vars` and `value_vars`:
3458+
3459+
>>> df.melt(id_vars='A', value_vars=['B', 'C'])
3460+
A variable value
3461+
0 1.0 B 1
3462+
1 <NA> B 2
3463+
2 3.0 B 3
3464+
3 4.0 B 4
3465+
4 5.0 B 5
3466+
5 1.0 C <NA>
3467+
6 <NA> C 3
3468+
7 3.0 C <NA>
3469+
8 4.0 C 4
3470+
9 5.0 C 5
3471+
<BLANKLINE>
3472+
[10 rows x 3 columns]
3473+
3474+
3475+
Args:
3476+
id_vars (tuple, list, or ndarray, optional):
3477+
Column(s) to use as identifier variables.
3478+
value_vars (tuple, list, or ndarray, optional):
3479+
Column(s) to unpivot. If not specified, uses all columns that
3480+
are not set as `id_vars`.
3481+
var_name (scalar):
3482+
Name to use for the 'variable' column. If None it uses
3483+
``frame.columns.name`` or 'variable'.
3484+
value_name (scalar, default 'value'):
3485+
Name to use for the 'value' column.
34293486
34303487
Returns:
34313488
DataFrame: Unpivoted DataFrame.
@@ -3757,6 +3814,52 @@ def pivot(self, *, columns, index=None, values=None):
37573814
do not together uniquely identify input rows, the output will be
37583815
silently non-deterministic.
37593816
3817+
**Examples:**
3818+
3819+
>>> import bigframes.pandas as bpd
3820+
>>> bpd.options.display.progress_bar = None
3821+
3822+
>>> df = bpd.DataFrame({
3823+
... "foo": ["one", "one", "one", "two", "two"],
3824+
... "bar": ["A", "B", "C", "A", "B"],
3825+
... "baz": [1, 2, 3, 4, 5],
3826+
... "zoo": ['x', 'y', 'z', 'q', 'w']
3827+
... })
3828+
3829+
>>> df
3830+
foo bar baz zoo
3831+
0 one A 1 x
3832+
1 one B 2 y
3833+
2 one C 3 z
3834+
3 two A 4 q
3835+
4 two B 5 w
3836+
<BLANKLINE>
3837+
[5 rows x 4 columns]
3838+
3839+
Using `pivot` without optional arguments:
3840+
3841+
>>> df.pivot(columns='foo')
3842+
bar baz zoo
3843+
foo one two one two one two
3844+
0 A <NA> 1 <NA> x <NA>
3845+
1 B <NA> 2 <NA> y <NA>
3846+
2 C <NA> 3 <NA> z <NA>
3847+
3 <NA> A <NA> 4 <NA> q
3848+
4 <NA> B <NA> 5 <NA> w
3849+
<BLANKLINE>
3850+
[5 rows x 6 columns]
3851+
3852+
Using `pivot` with `index` and `values`:
3853+
3854+
>>> df.pivot(columns='foo', index='bar', values='baz')
3855+
foo one two
3856+
bar
3857+
A 1 4
3858+
B 2 5
3859+
C 3 <NA>
3860+
<BLANKLINE>
3861+
[3 rows x 2 columns]
3862+
37603863
Args:
37613864
columns (str or object or a list of str):
37623865
Column to use to make new frame's columns.
@@ -3774,7 +3877,7 @@ def pivot(self, *, columns, index=None, values=None):
37743877
"""
37753878
raise NotImplementedError(constants.ABSTRACT_METHOD_ERROR_MESSAGE)
37763879

3777-
def stack(self):
3880+
def stack(self, level=-1):
37783881
"""
37793882
Stack the prescribed level(s) from columns to index.
37803883
@@ -3792,12 +3895,36 @@ def stack(self):
37923895
BigQuery DataFrames does not support stack operations that would
37933896
combine columns of different dtypes.
37943897
3898+
**Example:**
3899+
3900+
>>> import bigframes.pandas as bpd
3901+
>>> bpd.options.display.progress_bar = None
3902+
3903+
>>> df = bpd.DataFrame({'A': [1, 3], 'B': [2, 4]}, index=['foo', 'bar'])
3904+
>>> df
3905+
A B
3906+
foo 1 2
3907+
bar 3 4
3908+
<BLANKLINE>
3909+
[2 rows x 2 columns]
3910+
3911+
>>> df.stack()
3912+
foo A 1
3913+
B 2
3914+
bar A 3
3915+
B 4
3916+
dtype: Int64
3917+
3918+
Args:
3919+
level (int, str, or list of these, default -1 (last level)):
3920+
Level(s) to stack from the column axis onto the index axis.
3921+
37953922
Returns:
37963923
DataFrame or Series: Stacked dataframe or series.
37973924
"""
37983925
raise NotImplementedError(constants.ABSTRACT_METHOD_ERROR_MESSAGE)
37993926

3800-
def unstack(self):
3927+
def unstack(self, level=-1):
38013928
"""
38023929
Pivot a level of the (necessarily hierarchical) index labels.
38033930
@@ -3807,6 +3934,30 @@ def unstack(self):
38073934
If the index is not a MultiIndex, the output will be a Series
38083935
(the analogue of stack when the columns are not a MultiIndex).
38093936
3937+
**Example:**
3938+
3939+
>>> import bigframes.pandas as bpd
3940+
>>> bpd.options.display.progress_bar = None
3941+
3942+
>>> df = bpd.DataFrame({'A': [1, 3], 'B': [2, 4]}, index=['foo', 'bar'])
3943+
>>> df
3944+
A B
3945+
foo 1 2
3946+
bar 3 4
3947+
<BLANKLINE>
3948+
[2 rows x 2 columns]
3949+
3950+
>>> df.unstack()
3951+
A foo 1
3952+
bar 3
3953+
B foo 2
3954+
bar 4
3955+
dtype: Int64
3956+
3957+
Args:
3958+
level (int, str, or list of these, default -1 (last level)):
3959+
Level(s) of index to unstack, can pass level name.
3960+
38103961
Returns:
38113962
DataFrame or Series: DataFrame or Series.
38123963
"""

0 commit comments

Comments
 (0)