Skip to content

Commit

Permalink
fix to_snowpark_pandas import
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-joshi committed Aug 14, 2024
1 parent 4de4c63 commit 5c1aefd
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/snowflake/snowpark/dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -1005,8 +1005,12 @@ def to_snowpark_pandas(
B A
2 1 1 3 1
"""
import snowflake.snowpark.modin.pandas as pd # pragma: no cover

# black and isort disagree on how to format this section with isort: skip
# fmt: off
import snowflake.snowpark.modin.plugin # isort: skip # noqa: F401
# If snowflake.snowpark.modin.plugin was successfully imported, then modin.pandas is available
import modin.pandas as pd # isort: skip
# fmt: on
# create a temporary table out of the current snowpark dataframe
temporary_table_name = random_name_for_temp_object(
TempObjectType.TABLE
Expand Down
39 changes: 39 additions & 0 deletions tests/integ/test_df_to_snowpark_pandas.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#!/usr/bin/env python3
#
# Copyright (c) 2012-2024 Snowflake Computing Inc. All rights reserved.
#

# Tests behavior of to_snowpark_pandas() without explicitly initializing Snowpark pandas.

import pytest

from snowflake.snowpark._internal.utils import TempObjectType
from tests.utils import Utils


@pytest.fixture(scope="module")
def tmp_table_basic(session):
table_name = Utils.random_name_for_temp_object(TempObjectType.TABLE)
Utils.create_table(
session, table_name, "id integer, foot_size float, shoe_model varchar"
)
session.sql(f"insert into {table_name} values (1, 32.0, 'medium')").collect()
session.sql(f"insert into {table_name} values (2, 27.0, 'small')").collect()
session.sql(f"insert into {table_name} values (3, 40.0, 'large')").collect()

try:
yield table_name
finally:
Utils.drop_table(session, table_name)


def test_to_snowpark_pandas_no_modin(session, tmp_table_basic):
snowpark_df = session.table(tmp_table_basic)
# Check if modin is installed
try:
import modin # noqa: F401

snowpark_df.to_snowpark_pandas() # should have no errors
except ModuleNotFoundError:
with pytest.raises(ModuleNotFoundError, match="Modin is not installed."):
snowpark_df.to_snowpark_pandas()

0 comments on commit 5c1aefd

Please sign in to comment.