Skip to content

Commit 20cae2d

Browse files
committed
plumb through session for from_tuples and from_arrays
1 parent bed4069 commit 20cae2d

File tree

2 files changed

+43
-10
lines changed

2 files changed

+43
-10
lines changed

bigframes/core/indexes/multi.py

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,16 @@
1414

1515
from __future__ import annotations
1616

17-
from typing import cast, Hashable, Iterable, Sequence
17+
from typing import cast, Hashable, Iterable, Optional, Sequence, TYPE_CHECKING
1818

1919
import bigframes_vendored.pandas.core.indexes.multi as vendored_pandas_multindex
2020
import pandas
2121

2222
from bigframes.core.indexes.base import Index
2323

24+
if TYPE_CHECKING:
25+
import bigframes.session
26+
2427

2528
class MultiIndex(Index, vendored_pandas_multindex.MultiIndex):
2629
__doc__ = vendored_pandas_multindex.MultiIndex.__doc__
@@ -31,18 +34,50 @@ def from_tuples(
3134
tuples: Iterable[tuple[Hashable, ...]],
3235
sortorder: int | None = None,
3336
names: Sequence[Hashable] | Hashable | None = None,
37+
*,
38+
session: Optional[bigframes.session.Session] = None,
3439
) -> MultiIndex:
3540
pd_index = pandas.MultiIndex.from_tuples(tuples, sortorder, names)
3641
# Index.__new__ should detect multiple levels and properly create a multiindex
37-
return cast(MultiIndex, Index(pd_index))
42+
return cast(MultiIndex, Index(pd_index, session=session))
3843

3944
@classmethod
4045
def from_arrays(
4146
cls,
4247
arrays,
4348
sortorder: int | None = None,
4449
names=None,
50+
*,
51+
session: Optional[bigframes.session.Session] = None,
4552
) -> MultiIndex:
4653
pd_index = pandas.MultiIndex.from_arrays(arrays, sortorder, names)
4754
# Index.__new__ should detect multiple levels and properly create a multiindex
48-
return cast(MultiIndex, Index(pd_index))
55+
return cast(MultiIndex, Index(pd_index, session=session))
56+
57+
58+
class MultiIndexAccessor:
59+
"""Proxy to MultiIndex constructors to allow a session to be passed in."""
60+
61+
def __init__(self, session: bigframes.session.Session):
62+
self._session = session
63+
64+
def __call__(self, *args, **kwargs) -> MultiIndex:
65+
"""Construct a MultiIndex using the associated Session.
66+
67+
See :class:`bigframes.pandas.MultiIndex`.
68+
"""
69+
return MultiIndex(*args, session=self._session, **kwargs)
70+
71+
def from_arrays(self, *args, **kwargs) -> MultiIndex:
72+
"""Construct a MultiIndex using the associated Session.
73+
74+
See :func:`bigframes.pandas.MultiIndex.from_arrays`.
75+
"""
76+
return MultiIndex.from_arrays(*args, session=self._session, **kwargs)
77+
78+
def from_tuples(self, *args, **kwargs) -> MultiIndex:
79+
"""Construct a MultiIndex using the associated Session.
80+
81+
See :func:`bigframes.pandas.MultiIndex.from_tuples`.
82+
"""
83+
return MultiIndex.from_tuples(*args, session=self._session, **kwargs)

bigframes/session/__init__.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
import bigframes.core
6969
from bigframes.core import blocks, log_adapter, utils
7070
import bigframes.core.indexes
71+
import bigframes.core.indexes.multi
7172
import bigframes.core.pyformat
7273

7374
# Even though the ibis.backends.bigquery import is unused, it's needed
@@ -2316,20 +2317,17 @@ def DataFrame(self, *args, **kwargs):
23162317

23172318
return bigframes.dataframe.DataFrame(*args, session=self, **kwargs)
23182319

2319-
def MultiIndex(self, *args, **kwargs):
2320+
@property
2321+
def MultiIndex(self) -> bigframes.core.indexes.multi.MultiIndexAccessor:
23202322
"""Constructs a MultiIndex.
23212323
23222324
Included for compatibility between bpd and Session.
23232325
23242326
See :class:`bigframes.pandas.MulitIndex` for full documentation.
23252327
"""
2326-
import bigframes.core.indexes
2327-
2328-
return bigframes.core.indexes.MultiIndex(*args, session=self, **kwargs)
2328+
import bigframes.core.indexes.multi
23292329

2330-
MultiIndex.from_tuples = bigframes.core.indexes.MultiIndex.from_tuples # type: ignore
2331-
MultiIndex.from_frame = bigframes.core.indexes.MultiIndex.from_frame # type: ignore
2332-
MultiIndex.from_arrays = bigframes.core.indexes.MultiIndex.from_arrays # type: ignore
2330+
return bigframes.core.indexes.multi.MultiIndexAccessor(self)
23332331

23342332
def Index(self, *args, **kwargs):
23352333
"""Constructs a Index.

0 commit comments

Comments
 (0)