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 c88d38e

Browse files
authoredNov 20, 2023
docs: add code samples for index and column properties (#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 🦕
1 parent dd78acb commit c88d38e

File tree

2 files changed

+126
-2
lines changed

2 files changed

+126
-2
lines changed
 

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

+78-1
Original file line numberDiff line numberDiff line change
@@ -3106,14 +3106,91 @@ def index(self):
31063106
index is used for label-based access and alignment, and can be accessed
31073107
or modified using this attribute.
31083108
3109+
**Examples:**
3110+
3111+
>>> import bigframes.pandas as bpd
3112+
>>> bpd.options.display.progress_bar = None
3113+
3114+
You can access the index of a DataFrame via ``index`` property.
3115+
3116+
>>> df = bpd.DataFrame({'Name': ['Alice', 'Bob', 'Aritra'],
3117+
... 'Age': [25, 30, 35],
3118+
... 'Location': ['Seattle', 'New York', 'Kona']},
3119+
... index=([10, 20, 30]))
3120+
>>> df
3121+
Name Age Location
3122+
10 Alice 25 Seattle
3123+
20 Bob 30 New York
3124+
30 Aritra 35 Kona
3125+
<BLANKLINE>
3126+
[3 rows x 3 columns]
3127+
>>> df.index # doctest: +ELLIPSIS
3128+
<bigframes.core.indexes.index.Index object at ...>
3129+
>>> df.index.values
3130+
array([10, 20, 30], dtype=object)
3131+
3132+
Let's try setting a new index for the dataframe and see that reflect via
3133+
``index`` property.
3134+
3135+
>>> df1 = df.set_index(["Name", "Location"])
3136+
>>> df1
3137+
Age
3138+
Name Location
3139+
Alice Seattle 25
3140+
Bob New York 30
3141+
Aritra Kona 35
3142+
<BLANKLINE>
3143+
[3 rows x 1 columns]
3144+
>>> df1.index # doctest: +ELLIPSIS
3145+
<bigframes.core.indexes.index.Index object at ...>
3146+
>>> df1.index.values
3147+
array([('Alice', 'Seattle'), ('Bob', 'New York'), ('Aritra', 'Kona')],
3148+
dtype=object)
3149+
31093150
Returns:
31103151
The index labels of the DataFrame.
31113152
"""
31123153
raise NotImplementedError(constants.ABSTRACT_METHOD_ERROR_MESSAGE)
31133154

31143155
@property
31153156
def columns(self):
3116-
"The column labels of the DataFrame."
3157+
"""The column labels of the DataFrame.
3158+
3159+
**Examples:**
3160+
3161+
>>> import bigframes.pandas as bpd
3162+
>>> bpd.options.display.progress_bar = None
3163+
3164+
You can access the column labels of a DataFrame via ``columns`` property.
3165+
3166+
>>> df = bpd.DataFrame({'Name': ['Alice', 'Bob', 'Aritra'],
3167+
... 'Age': [25, 30, 35],
3168+
... 'Location': ['Seattle', 'New York', 'Kona']},
3169+
... index=([10, 20, 30]))
3170+
>>> df
3171+
Name Age Location
3172+
10 Alice 25 Seattle
3173+
20 Bob 30 New York
3174+
30 Aritra 35 Kona
3175+
<BLANKLINE>
3176+
[3 rows x 3 columns]
3177+
>>> df.columns
3178+
Index(['Name', 'Age', 'Location'], dtype='object')
3179+
3180+
You can also set new labels for columns.
3181+
3182+
>>> df.columns = ["NewName", "NewAge", "NewLocation"]
3183+
>>> df
3184+
NewName NewAge NewLocation
3185+
10 Alice 25 Seattle
3186+
20 Bob 30 New York
3187+
30 Aritra 35 Kona
3188+
<BLANKLINE>
3189+
[3 rows x 3 columns]
3190+
>>> df.columns
3191+
Index(['NewName', 'NewAge', 'NewLocation'], dtype='object')
3192+
3193+
"""
31173194
raise NotImplementedError(constants.ABSTRACT_METHOD_ERROR_MESSAGE)
31183195

31193196
def value_counts(

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

+48-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,54 @@ def struct(self):
4444

4545
@property
4646
def index(self):
47-
"""The index (axis labels) of the Series."""
47+
"""The index (axis labels) of the Series.
48+
49+
The index of a Series is used to label and identify each element of the
50+
underlying data. The index can be thought of as an immutable ordered set
51+
(technically a multi-set, as it may contain duplicate labels), and is
52+
used to index and align data.
53+
54+
**Examples:**
55+
56+
>>> import bigframes.pandas as bpd
57+
>>> bpd.options.display.progress_bar = None
58+
59+
You can access the index of a Series via ``index`` property.
60+
61+
>>> df = bpd.DataFrame({'Name': ['Alice', 'Bob', 'Aritra'],
62+
... 'Age': [25, 30, 35],
63+
... 'Location': ['Seattle', 'New York', 'Kona']},
64+
... index=([10, 20, 30]))
65+
>>> s = df["Age"]
66+
>>> s
67+
10 25
68+
20 30
69+
30 35
70+
Name: Age, dtype: Int64
71+
>>> s.index # doctest: +ELLIPSIS
72+
<bigframes.core.indexes.index.Index object at ...>
73+
>>> s.index.values
74+
array([10, 20, 30], dtype=object)
75+
76+
Let's try setting a multi-index case reflect via ``index`` property.
77+
78+
>>> df1 = df.set_index(["Name", "Location"])
79+
>>> s1 = df1["Age"]
80+
>>> s1
81+
Name Location
82+
Alice Seattle 25
83+
Bob New York 30
84+
Aritra Kona 35
85+
Name: Age, dtype: Int64
86+
>>> s1.index # doctest: +ELLIPSIS
87+
<bigframes.core.indexes.index.Index object at ...>
88+
>>> s1.index.values
89+
array([('Alice', 'Seattle'), ('Bob', 'New York'), ('Aritra', 'Kona')],
90+
dtype=object)
91+
92+
Returns:
93+
The index labels of the Series.
94+
"""
4895
raise NotImplementedError(constants.ABSTRACT_METHOD_ERROR_MESSAGE)
4996

5097
@property

0 commit comments

Comments
 (0)
Failed to load comments.