Skip to content
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: support Index.to_frame() #894

Open
wants to merge 27 commits into
base: main
Choose a base branch
from
Open
Changes from 6 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
b62f1ca
chore: clean up OWNERS
Aug 5, 2024
ef7eaf8
Merge branch 'googleapis:main' into main
mattyopl Aug 6, 2024
cd97a45
feat: support `Index.to_frame()`
Aug 8, 2024
860f370
Merge branch 'googleapis:main' into main
mattyopl Aug 8, 2024
8761d8a
feat: support `Index.to_frame()`
Aug 8, 2024
c68a5f5
Merge branch 'main' of github.com:mattyopl/python-bigquery-dataframes
Aug 8, 2024
7c2e1b0
Merge branch 'main' of github.com:mattyopl/python-bigquery-dataframes
Aug 8, 2024
e9bffa8
Merge branch 'main' of github.com:mattyopl/python-bigquery-dataframes
Aug 8, 2024
0f2e291
Merge branch 'main' into main
mattyopl Aug 8, 2024
2e1afff
Merge branch 'main' of github.com:mattyopl/python-bigquery-dataframes
Aug 8, 2024
e376b44
Merge branch 'main' of github.com:mattyopl/python-bigquery-dataframes
Aug 9, 2024
31f3db8
Merge branch 'main' of github.com:mattyopl/python-bigquery-dataframes
Aug 9, 2024
e813c61
Merge branch 'main' of github.com:mattyopl/python-bigquery-dataframes
Aug 9, 2024
32eb06c
Merge branch 'main' of github.com:mattyopl/python-bigquery-dataframes
Aug 9, 2024
60bea0a
Merge branch 'main' of github.com:mattyopl/python-bigquery-dataframes
Aug 9, 2024
ec97d72
Merge branch 'main' into main
mattyopl Aug 9, 2024
af9f7a4
Merge branch 'main' of github.com:mattyopl/python-bigquery-dataframes
Aug 9, 2024
b212126
Merge branch 'main' of github.com:mattyopl/python-bigquery-dataframes
Aug 9, 2024
9c31fb3
Merge branch 'main' of github.com:mattyopl/python-bigquery-dataframes
Aug 9, 2024
266feae
Merge branch 'main' of github.com:mattyopl/python-bigquery-dataframes
Aug 12, 2024
43f826c
Merge branch 'main' of github.com:mattyopl/python-bigquery-dataframes
Aug 12, 2024
5d388da
Merge branch 'main' of github.com:mattyopl/python-bigquery-dataframes
Aug 12, 2024
c368362
Merge branch 'main' of github.com:mattyopl/python-bigquery-dataframes
Aug 12, 2024
3d2ba36
Merge branch 'main' of github.com:mattyopl/python-bigquery-dataframes
Aug 12, 2024
2966f08
Merge branch 'googleapis:main' into main
mattyopl Aug 14, 2024
ab2753e
Merge branch 'googleapis:main' into main
mattyopl Sep 9, 2024
777ff7d
Merge branch 'googleapis:main' into main
mattyopl Sep 16, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions bigframes/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,32 @@ def from_frame(
index = Index(frame._block)
index._linked_frame = frame
return index

def to_frame(
self, index: bool = True, name: blocks.Label | None = None
) -> bigframes.dataframe.DataFrame:
import numpy as np
provided_name = name if name else self.name
provided_index = self.values if index else np.arange(len(self.values))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just provide None if want default sequential index. np.arrange may create a very large in-memory array here if the original index is large (some BigFrames objects have billions of rows).

result = bigframes.dataframe.DataFrame({provided_name: self.values}, index= provided_index)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the index is a MultiIndex, the resulting DataFrame will need to have multiple columns (and names will need to be a list-like with length equal to the multi-index level depth).

if index: # matching pandas behavior
result.index.name = self.name
return result

def to_frame(
mattyopl marked this conversation as resolved.
Show resolved Hide resolved
self, index: bool = True, name: blocks.Label | None = None
) -> bigframes.dataframe.DataFrame:
import numpy as np
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Put the import at the top of the file for easier management. Actually numpy is already imported in the file.

Import in the method only to avoid circular imports.


provided_name = name if name else self.name
provided_index = self.values if index else np.arange(len(self.values))
result = bigframes.dataframe.DataFrame(
{provided_name: self.values}, index=provided_index
)
if index: # matching pandas behavior
result.index.name = self.name

return result
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Redefinition


@property
def _session(self):
Expand Down
Loading