-
Notifications
You must be signed in to change notification settings - Fork 47
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Add MultiIndex subclass. #596
Merged
+193
−29
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
e95874f
feat: Add MultiIndex subclass.
TrevorBergeron 658510c
fix test error
TrevorBergeron 46b2b58
resolve mypy issues
TrevorBergeron ecfd98d
Merge branch 'main' into index_subclass
TrevorBergeron aaeead9
Merge remote-tracking branch 'github/main' into index_subclass
TrevorBergeron 4e04a71
pr comments
TrevorBergeron 29c4dc6
Merge branch 'main' into index_subclass
TrevorBergeron bb59939
moved MultiIndex to own file
TrevorBergeron File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# Copyright 2024 Google LLC | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
from __future__ import annotations | ||
|
||
from typing import cast, Hashable, Iterable, Sequence | ||
|
||
import bigframes_vendored.pandas.core.indexes.multi as vendored_pandas_multindex | ||
import pandas | ||
|
||
from bigframes.core.indexes.base import Index | ||
|
||
|
||
class MultiIndex(Index, vendored_pandas_multindex.MultiIndex): | ||
__doc__ = vendored_pandas_multindex.MultiIndex.__doc__ | ||
|
||
@classmethod | ||
def from_tuples( | ||
cls, | ||
tuples: Iterable[tuple[Hashable, ...]], | ||
sortorder: int | None = None, | ||
names: Sequence[Hashable] | Hashable | None = None, | ||
) -> MultiIndex: | ||
pd_index = pandas.MultiIndex.from_tuples(tuples, sortorder, names) | ||
# Index.__new__ should detect multiple levels and properly create a multiindex | ||
return cast(MultiIndex, Index(pd_index)) | ||
|
||
@classmethod | ||
def from_arrays( | ||
cls, | ||
arrays, | ||
sortorder: int | None = None, | ||
names=None, | ||
) -> MultiIndex: | ||
pd_index = pandas.MultiIndex.from_arrays(arrays, sortorder, names) | ||
# Index.__new__ should detect multiple levels and properly create a multiindex | ||
return cast(MultiIndex, Index(pd_index)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
third_party/bigframes_vendored/pandas/core/indexes/multi.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
# Contains code from https://github.com/pandas-dev/pandas/blob/main/pandas/core/indexes/multi.py | ||
from __future__ import annotations | ||
|
||
from typing import Hashable, Iterable, Sequence | ||
|
||
import bigframes_vendored.pandas.core.indexes.base | ||
|
||
from bigframes import constants | ||
|
||
|
||
class MultiIndex(bigframes_vendored.pandas.core.indexes.base.Index): | ||
""" | ||
A multi-level, or hierarchical, index object for pandas objects. | ||
""" | ||
|
||
@classmethod | ||
def from_tuples( | ||
cls, | ||
tuples: Iterable[tuple[Hashable, ...]], | ||
sortorder: int | None = None, | ||
names: Sequence[Hashable] | Hashable | None = None, | ||
) -> MultiIndex: | ||
""" | ||
Convert list of tuples to MultiIndex. | ||
|
||
**Examples:** | ||
|
||
>>> import bigframes.pandas as bpd | ||
>>> bpd.options.display.progress_bar = None | ||
>>> tuples = [(1, 'red'), (1, 'blue'), | ||
... (2, 'red'), (2, 'blue')] | ||
>>> bpd.MultiIndex.from_tuples(tuples, names=('number', 'color')) | ||
MultiIndex([(1, 'red'), | ||
(1, 'blue'), | ||
(2, 'red'), | ||
(2, 'blue')], | ||
names=['number', 'color']) | ||
|
||
Args: | ||
tuples (list / sequence of tuple-likes): | ||
Each tuple is the index of one row/column. | ||
sortorder (int or None): | ||
Level of sortedness (must be lexicographically sorted by that | ||
level). | ||
names (list / sequence of str, optional): | ||
Names for the levels in the index. | ||
|
||
Returns: | ||
MultiIndex | ||
""" | ||
raise NotImplementedError(constants.ABSTRACT_METHOD_ERROR_MESSAGE) | ||
|
||
@classmethod | ||
def from_arrays( | ||
cls, | ||
arrays, | ||
sortorder: int | None = None, | ||
names=None, | ||
) -> MultiIndex: | ||
""" | ||
Convert arrays to MultiIndex. | ||
|
||
**Examples:** | ||
|
||
>>> import bigframes.pandas as bpd | ||
>>> bpd.options.display.progress_bar = None | ||
>>> arrays = [[1, 1, 2, 2], ['red', 'blue', 'red', 'blue']] | ||
>>> bpd.MultiIndex.from_arrays(arrays, names=('number', 'color')) | ||
MultiIndex([(1, 'red'), | ||
(1, 'blue'), | ||
(2, 'red'), | ||
(2, 'blue')], | ||
names=['number', 'color']) | ||
|
||
Args: | ||
arrays (list / sequence of array-likes): | ||
Each array-like gives one level's value for each data point. | ||
len(arrays) is the number of levels. | ||
sortorder (int or None): | ||
Level of sortedness (must be lexicographically sorted by that | ||
level). | ||
names (list / sequence of str, optional): | ||
Names for the levels in the index. | ||
|
||
Returns: | ||
MultiIndex | ||
""" | ||
raise NotImplementedError(constants.ABSTRACT_METHOD_ERROR_MESSAGE) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will be a breaking change that we may not want to introduce right now. @tswast
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am deleting a class yes, but it was more of an implementation detail of the Index class. Should only affect users if they were somehow depending on exact type or type name in their code, which would be very strange.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch @GarrettWu.
Given that we say
) -> Index:
in the only method that would return such an object and we don't explicitly documentFrameIndex
, I'm okay overlooking this and treatingFrameIndex
as a private implementation detail.