-
Notifications
You must be signed in to change notification settings - Fork 68
feat: support Index.to_frame()
#894
Changes from all commits
b62f1ca
ef7eaf8
cd97a45
860f370
8761d8a
c68a5f5
7c2e1b0
e9bffa8
0f2e291
2e1afff
e376b44
31f3db8
e813c61
32eb06c
60bea0a
ec97d72
af9f7a4
b212126
9c31fb3
266feae
43f826c
5d388da
c368362
3d2ba36
2966f08
ab2753e
777ff7d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,6 +16,7 @@ | |
| import pandas as pd | ||
| import pytest | ||
|
|
||
| import bigframes.core.indexes as indexes | ||
| import bigframes.pandas as bpd | ||
| from tests.system.utils import assert_pandas_index_equal_ignore_index_type | ||
|
|
||
|
|
@@ -320,6 +321,30 @@ def test_index_to_series( | |
| pd.testing.assert_series_equal(bf_result, pd_result) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("index_arg", [True, False]) | ||
| @pytest.mark.parametrize("name_arg", [None, "food"]) | ||
| def test_index_to_frame(index_arg, name_arg): | ||
| pd_idx: pd.Index = pd.Index( | ||
| ["Ant", "Bear", "Cow"], name="animal", dtype="string[pyarrow]" | ||
| ) | ||
| bf_idx = indexes.Index(["Ant", "Bear", "Cow"], name="animal") | ||
|
|
||
| if name_arg is None: | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It doesn't need the if else condition. Just is good enough
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Pandas implementation of
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I can add something similar to BigFrames if you think it is better to exactly mirror pandas functionality: I am equally happy either way
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, that's a weird behavior of pandas. Thanks for pointing out. Then we shouldn't let the default to be None. But should have sth similar. @TrevorBergeron |
||
| pd_df = pd_idx.to_frame(index=index_arg) | ||
| bf_df = bf_idx.to_frame(index=index_arg) | ||
| else: | ||
| pd_df = pd_idx.to_frame(index=index_arg, name=name_arg) | ||
| bf_df = bf_idx.to_frame(index=index_arg, name=name_arg) | ||
| pd.testing.assert_frame_equal( | ||
| pd_df, bf_df.to_pandas(), check_column_type=False, check_index_type=False | ||
| ) | ||
| # BigFrames type casting is weird | ||
| # automatically casts dtype to string whereas pandas dtype is object | ||
| # additionally, pandas uses string[python] and BigFrames uses string[pyarrow] | ||
| # so we set dtype in pandas index creation | ||
| # similarly, pandas uses int64 dtype for numerical index and BigFrames uses Int64 | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| ("how",), | ||
| [ | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,6 +16,7 @@ | |
| import pandas | ||
| import pytest | ||
|
|
||
| import bigframes.core.indexes as indexes | ||
| import bigframes.pandas as bpd | ||
| from tests.system.utils import assert_pandas_df_equal, skip_legacy_pandas | ||
|
|
||
|
|
@@ -45,6 +46,27 @@ def test_multi_index_from_arrays(): | |
| pandas.testing.assert_index_equal(bf_idx.to_pandas(), pd_idx) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("index_arg", [True, False]) | ||
| @pytest.mark.parametrize("name_arg", [None, ["x", "y"]]) | ||
| def test_multi_index_to_frame(index_arg, name_arg): | ||
|
|
||
| pd_idx = pandas.MultiIndex.from_arrays([["a", "b", "c"], ["d", "e", "f"]]) | ||
| bf_idx = indexes.MultiIndex.from_arrays([["a", "b", "c"], ["d", "e", "f"]]) | ||
| if name_arg is None: | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same here. |
||
| pd_df = pd_idx.to_frame(index=index_arg) | ||
| bf_df = bf_idx.to_frame(index=index_arg) | ||
| else: | ||
| pd_df = pd_idx.to_frame(index=index_arg, name=name_arg) | ||
| bf_df = bf_idx.to_frame(index=index_arg, name=name_arg) | ||
| pandas.testing.assert_frame_equal( | ||
| pd_df, | ||
| bf_df.to_pandas(), | ||
| check_dtype=False, | ||
| check_column_type=False, | ||
| check_index_type=False, | ||
| ) | ||
|
|
||
|
|
||
| @skip_legacy_pandas | ||
| def test_read_pandas_multi_index_axes(): | ||
| index = pandas.MultiIndex.from_arrays( | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.