Skip to content

Commit b188146

Browse files
authored
fix: include all names in MultiIndex repr (#564)
docs: include Index in table-of-contents (#564)
1 parent 4ae0262 commit b188146

File tree

11 files changed

+69
-17
lines changed

11 files changed

+69
-17
lines changed

bigframes/core/blocks.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1314,8 +1314,8 @@ def retrieve_repr_request_results(
13141314
head_block = self
13151315
computed_df, query_job = head_block.to_pandas()
13161316
formatted_df = computed_df.set_axis(self.column_labels, axis=1)
1317-
# we reset the axis and substitute the bf index name for the default
1318-
formatted_df.index.name = self.index.name
1317+
# we reset the axis and substitute the bf index name(s) for the default
1318+
formatted_df.index.names = self.index.names # type: ignore
13191319
return formatted_df, count, query_job
13201320

13211321
def promote_offsets(self, label: Label = None) -> typing.Tuple[Block, str]:

bigframes/core/indexes/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
from bigframes.core.indexes.index import Index
15+
from bigframes.core.indexes.base import Index
1616

1717
__all__ = [
1818
"Index",

bigframes/core/indexes/index.py renamed to bigframes/core/indexes/base.py

+6-9
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,12 @@ def from_frame(
8888

8989
@property
9090
def name(self) -> blocks.Label:
91-
return self.names[0]
91+
names = self.names
92+
if len(names) == 1:
93+
return self.names[0]
94+
else:
95+
# pandas returns None for MultiIndex.name.
96+
return None
9297

9398
@name.setter
9499
def name(self, value: blocks.Label):
@@ -460,14 +465,6 @@ def __init__(
460465
super().__init__(series_or_dataframe._block)
461466
self._whole_frame = series_or_dataframe
462467

463-
@property
464-
def name(self) -> blocks.Label:
465-
return self.names[0]
466-
467-
@name.setter
468-
def name(self, value: blocks.Label):
469-
self.names = [value]
470-
471468
@property
472469
def names(self) -> typing.Sequence[blocks.Label]:
473470
"""Returns the names of the Index."""

docs/reference/bigframes.pandas/indexing.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
Index objects
44
=============
55

6-
.. autoclass:: bigframes.core.indexes.index.Index
6+
.. autoclass:: bigframes.core.indexes.base.Index
77
:members:
88
:inherited-members:
99
:undoc-members:

docs/templates/toc.yml

+2
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@
4040
- name: SeriesGroupBy
4141
uid: bigframes.core.groupby.SeriesGroupBy
4242
name: Groupby
43+
- name: Index
44+
uid: bigframes.core.indexes.base.Index
4345
- items:
4446
- name: AtDataFrameIndexer
4547
uid: bigframes.core.indexers.AtDataFrameIndexer

scripts/publish_api_coverage.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@
4444
"dataframegroupby": (
4545
"https://cloud.google.com/python/docs/reference/bigframes/latest/bigframes.core.groupby.DataFrameGroupBy#bigframes_core_groupby_DataFrameGroupBy_"
4646
),
47+
"index": (
48+
"https://cloud.google.com/python/docs/reference/bigframes/latest/bigframes.core.indexes.base.Index#bigframes_core_indexes_base_Index_"
49+
),
4750
"series": (
4851
"https://cloud.google.com/python/docs/reference/bigframes/latest/bigframes.series.Series#bigframes_series_Series_"
4952
),
@@ -59,7 +62,6 @@
5962
"window": (
6063
"https://cloud.google.com/python/docs/reference/bigframes/latest/bigframes.core.window.Window#bigframes_core_window_Window_"
6164
),
62-
# TODO: Index not documented.
6365
}
6466

6567

tests/system/small/test_index.py

+39
Original file line numberDiff line numberDiff line change
@@ -370,3 +370,42 @@ def test_index_isin(scalars_df_index, scalars_pandas_df_index):
370370
bf_series,
371371
check_names=False,
372372
)
373+
374+
375+
def test_multiindex_name_is_none(session):
376+
df = pd.DataFrame(
377+
{
378+
"A": [0, 0, 0, 1, 1, 1],
379+
"B": ["x", "y", "z", "x", "y", "z"],
380+
"C": [123, 345, 789, -123, -345, -789],
381+
"D": ["a", "b", "c", "d", "e", "f"],
382+
},
383+
)
384+
index = session.read_pandas(df).set_index(["A", "B"]).index
385+
assert index.name is None
386+
387+
388+
def test_multiindex_names_not_none(session):
389+
df = pd.DataFrame(
390+
{
391+
"A": [0, 0, 0, 1, 1, 1],
392+
"B": ["x", "y", "z", "x", "y", "z"],
393+
"C": [123, 345, 789, -123, -345, -789],
394+
"D": ["a", "b", "c", "d", "e", "f"],
395+
},
396+
)
397+
index = session.read_pandas(df).set_index(["A", "B"]).index
398+
assert tuple(index.names) == ("A", "B")
399+
400+
401+
def test_multiindex_repr_includes_all_names(session):
402+
df = pd.DataFrame(
403+
{
404+
"A": [0, 0, 0, 1, 1, 1],
405+
"B": ["x", "y", "z", "x", "y", "z"],
406+
"C": [123, 345, 789, -123, -345, -789],
407+
"D": ["a", "b", "c", "d", "e", "f"],
408+
},
409+
)
410+
index = session.read_pandas(df).set_index(["A", "B"]).index
411+
assert "names=['A', 'B']" in repr(index)

tests/system/small/test_session.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
import pytest
2828

2929
import bigframes
30-
import bigframes.core.indexes.index
30+
import bigframes.core.indexes.base
3131
import bigframes.dataframe
3232
import bigframes.dtypes
3333
import bigframes.ml.linear_model

third_party/bigframes_vendored/pandas/core/frame.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -4797,7 +4797,7 @@ def index(self):
47974797
MultiIndex([( 'Alice', 'Seattle'),
47984798
( 'Bob', 'New York'),
47994799
('Aritra', 'Kona')],
4800-
name='Name')
4800+
names=['Name', 'Location'])
48014801
>>> df1.index.values
48024802
array([('Alice', 'Seattle'), ('Bob', 'New York'), ('Aritra', 'Kona')],
48034803
dtype=object)

third_party/bigframes_vendored/pandas/core/indexes/base.py

+12
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,18 @@ class Index:
88
"""Immutable sequence used for indexing and alignment.
99
1010
The basic object storing axis labels for all objects.
11+
12+
Args:
13+
data (pandas.Series | pandas.Index | bigframes.series.Series | bigframes.core.indexes.base.Index):
14+
Labels (1-dimensional).
15+
dtype:
16+
Data type for the output Index. If not specified, this will be
17+
inferred from `data`.
18+
name:
19+
Name to be stored in the index.
20+
session (Optional[bigframes.session.Session]):
21+
BigQuery DataFrames session where queries are run. If not set,
22+
a default session is used.
1123
"""
1224

1325
@property

third_party/bigframes_vendored/pandas/core/series.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ def index(self):
8787
MultiIndex([( 'Alice', 'Seattle'),
8888
( 'Bob', 'New York'),
8989
('Aritra', 'Kona')],
90-
name='Name')
90+
names=['Name', 'Location'])
9191
>>> s1.index.values
9292
array([('Alice', 'Seattle'), ('Bob', 'New York'), ('Aritra', 'Kona')],
9393
dtype=object)

0 commit comments

Comments
 (0)