-
Notifications
You must be signed in to change notification settings - Fork 117
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SNOW-1855330, SNOW-1856158: Add support for DataFrame.from_dict, Data…
…Frame.from_records
- Loading branch information
1 parent
3a66c84
commit f77f93c
Showing
7 changed files
with
295 additions
and
10 deletions.
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
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,60 @@ | ||
# | ||
# Copyright (c) 2012-2024 Snowflake Computing Inc. All rights reserved. | ||
# | ||
import modin.pandas as pd | ||
import pandas as native_pd | ||
|
||
from tests.integ.modin.utils import assert_frame_equal | ||
from tests.integ.utils.sql_counter import sql_count_checker | ||
|
||
|
||
@sql_count_checker(query_count=1) | ||
def test_from_dict_basic(): | ||
data = {"col_1": [3, 2, 1, 0], "col_2": ["a", "b", "c", "d"]} | ||
|
||
assert_frame_equal( | ||
pd.DataFrame.from_dict(data), | ||
native_pd.DataFrame.from_dict(data), | ||
check_dtype=False, | ||
) | ||
|
||
|
||
@sql_count_checker(query_count=1) | ||
def test_from_dict_orient_index(): | ||
data = {"row_1": [3, 2, 1, 0], "row_2": ["a", "b", "c", "d"]} | ||
|
||
assert_frame_equal( | ||
pd.DataFrame.from_dict(data, orient="index"), | ||
native_pd.DataFrame.from_dict(data, orient="index"), | ||
check_dtype=False, | ||
) | ||
|
||
|
||
@sql_count_checker(query_count=1) | ||
def test_from_dict_orient_index_columns(): | ||
data = {"row_1": [3, 2, 1, 0], "row_2": ["a", "b", "c", "d"]} | ||
|
||
assert_frame_equal( | ||
pd.DataFrame.from_dict(data, orient="index", columns=["A", "B", "C", "D"]), | ||
native_pd.DataFrame.from_dict( | ||
data, orient="index", columns=["A", "B", "C", "D"] | ||
), | ||
check_dtype=False, | ||
) | ||
|
||
|
||
@sql_count_checker(query_count=1) | ||
def test_from_dict_orient_index_tight(): | ||
data = { | ||
"index": [("a", "b"), ("a", "c")], | ||
"columns": [("x", 1), ("y", 2)], | ||
"data": [[1, 3], [2, 4]], | ||
"index_names": ["n1", "n2"], | ||
"column_names": ["z1", "z2"], | ||
} | ||
|
||
assert_frame_equal( | ||
pd.DataFrame.from_dict(data, orient="tight"), | ||
native_pd.DataFrame.from_dict(data, orient="tight"), | ||
check_dtype=False, | ||
) |
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,61 @@ | ||
# | ||
# Copyright (c) 2012-2024 Snowflake Computing Inc. All rights reserved. | ||
# | ||
import modin.pandas as pd | ||
import pandas as native_pd | ||
import numpy as np | ||
import pytest | ||
|
||
from tests.integ.modin.utils import assert_frame_equal | ||
from tests.integ.utils.sql_counter import sql_count_checker | ||
|
||
|
||
@sql_count_checker(query_count=1) | ||
def test_from_records_structured_ndarray(): | ||
data = np.array( | ||
[(3, "a"), (2, "b"), (1, "c"), (0, "d")], | ||
dtype=[("col_1", "i4"), ("col_2", "U1")], | ||
) | ||
assert_frame_equal( | ||
pd.DataFrame.from_records(data), | ||
native_pd.DataFrame.from_records(data), | ||
check_dtype=False, | ||
) | ||
|
||
|
||
@sql_count_checker(query_count=1) | ||
def test_from_records_list_of_dicts(): | ||
data = [ | ||
{"col_1": 3, "col_2": "a"}, | ||
{"col_1": 2, "col_2": "b"}, | ||
{"col_1": 1, "col_2": "c"}, | ||
{"col_1": 0, "col_2": "d"}, | ||
] | ||
|
||
assert_frame_equal( | ||
pd.DataFrame.from_records(data), | ||
native_pd.DataFrame.from_records(data), | ||
check_dtype=False, | ||
) | ||
|
||
|
||
@sql_count_checker(query_count=1) | ||
def test_from_records_list_of_records(): | ||
data = [(3, "a"), (2, "b"), (1, "c"), (0, "d")] | ||
|
||
assert_frame_equal( | ||
pd.DataFrame.from_records(data), | ||
native_pd.DataFrame.from_records(data), | ||
check_dtype=False, | ||
) | ||
|
||
|
||
@sql_count_checker(query_count=0) | ||
def test_from_records_neg(): | ||
data = pd.DataFrame({"col1": [1, 2], "col2": [3, 4]}) | ||
|
||
with pytest.raises( | ||
NotImplementedError, | ||
match="Snowpark pandas 'DataFrame.from_records' method does not yet support 'data' parameter of type 'DataFrame'", | ||
): | ||
pd.DataFrame.from_records(data), |
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