Skip to content

Commit

Permalink
docs: add code samples for index and column properties (googleapi…
Browse files Browse the repository at this point in the history
…s#212)

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
  - `DataFrame.columns`: https://screenshot.googleplex.com/3Bwdb482FBfEsi2
  - `DataFrame.index`: https://screenshot.googleplex.com/4iJymH3FxMn8Hhb
  - `Series.index`: https://screenshot.googleplex.com/7MXQcuASbQ3c8s5 

Fixes internal issue 310260952 🦕
  • Loading branch information
shobsi authored Nov 20, 2023
1 parent dd78acb commit c88d38e
Show file tree
Hide file tree
Showing 2 changed files with 126 additions and 2 deletions.
79 changes: 78 additions & 1 deletion third_party/bigframes_vendored/pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -3106,14 +3106,91 @@ def index(self):
index is used for label-based access and alignment, and can be accessed
or modified using this attribute.
**Examples:**
>>> import bigframes.pandas as bpd
>>> bpd.options.display.progress_bar = None
You can access the index of a DataFrame via ``index`` property.
>>> df = bpd.DataFrame({'Name': ['Alice', 'Bob', 'Aritra'],
... 'Age': [25, 30, 35],
... 'Location': ['Seattle', 'New York', 'Kona']},
... index=([10, 20, 30]))
>>> df
Name Age Location
10 Alice 25 Seattle
20 Bob 30 New York
30 Aritra 35 Kona
<BLANKLINE>
[3 rows x 3 columns]
>>> df.index # doctest: +ELLIPSIS
<bigframes.core.indexes.index.Index object at ...>
>>> df.index.values
array([10, 20, 30], dtype=object)
Let's try setting a new index for the dataframe and see that reflect via
``index`` property.
>>> df1 = df.set_index(["Name", "Location"])
>>> df1
Age
Name Location
Alice Seattle 25
Bob New York 30
Aritra Kona 35
<BLANKLINE>
[3 rows x 1 columns]
>>> df1.index # doctest: +ELLIPSIS
<bigframes.core.indexes.index.Index object at ...>
>>> df1.index.values
array([('Alice', 'Seattle'), ('Bob', 'New York'), ('Aritra', 'Kona')],
dtype=object)
Returns:
The index labels of the DataFrame.
"""
raise NotImplementedError(constants.ABSTRACT_METHOD_ERROR_MESSAGE)

@property
def columns(self):
"The column labels of the DataFrame."
"""The column labels of the DataFrame.
**Examples:**
>>> import bigframes.pandas as bpd
>>> bpd.options.display.progress_bar = None
You can access the column labels of a DataFrame via ``columns`` property.
>>> df = bpd.DataFrame({'Name': ['Alice', 'Bob', 'Aritra'],
... 'Age': [25, 30, 35],
... 'Location': ['Seattle', 'New York', 'Kona']},
... index=([10, 20, 30]))
>>> df
Name Age Location
10 Alice 25 Seattle
20 Bob 30 New York
30 Aritra 35 Kona
<BLANKLINE>
[3 rows x 3 columns]
>>> df.columns
Index(['Name', 'Age', 'Location'], dtype='object')
You can also set new labels for columns.
>>> df.columns = ["NewName", "NewAge", "NewLocation"]
>>> df
NewName NewAge NewLocation
10 Alice 25 Seattle
20 Bob 30 New York
30 Aritra 35 Kona
<BLANKLINE>
[3 rows x 3 columns]
>>> df.columns
Index(['NewName', 'NewAge', 'NewLocation'], dtype='object')
"""
raise NotImplementedError(constants.ABSTRACT_METHOD_ERROR_MESSAGE)

def value_counts(
Expand Down
49 changes: 48 additions & 1 deletion third_party/bigframes_vendored/pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,54 @@ def struct(self):

@property
def index(self):
"""The index (axis labels) of the Series."""
"""The index (axis labels) of the Series.
The index of a Series is used to label and identify each element of the
underlying data. The index can be thought of as an immutable ordered set
(technically a multi-set, as it may contain duplicate labels), and is
used to index and align data.
**Examples:**
>>> import bigframes.pandas as bpd
>>> bpd.options.display.progress_bar = None
You can access the index of a Series via ``index`` property.
>>> df = bpd.DataFrame({'Name': ['Alice', 'Bob', 'Aritra'],
... 'Age': [25, 30, 35],
... 'Location': ['Seattle', 'New York', 'Kona']},
... index=([10, 20, 30]))
>>> s = df["Age"]
>>> s
10 25
20 30
30 35
Name: Age, dtype: Int64
>>> s.index # doctest: +ELLIPSIS
<bigframes.core.indexes.index.Index object at ...>
>>> s.index.values
array([10, 20, 30], dtype=object)
Let's try setting a multi-index case reflect via ``index`` property.
>>> df1 = df.set_index(["Name", "Location"])
>>> s1 = df1["Age"]
>>> s1
Name Location
Alice Seattle 25
Bob New York 30
Aritra Kona 35
Name: Age, dtype: Int64
>>> s1.index # doctest: +ELLIPSIS
<bigframes.core.indexes.index.Index object at ...>
>>> s1.index.values
array([('Alice', 'Seattle'), ('Bob', 'New York'), ('Aritra', 'Kona')],
dtype=object)
Returns:
The index labels of the Series.
"""
raise NotImplementedError(constants.ABSTRACT_METHOD_ERROR_MESSAGE)

@property
Expand Down

0 comments on commit c88d38e

Please sign in to comment.