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

[tests][python-package] refactor list_to_1d_numpy test to run without pandas installed #4639

Merged
merged 3 commits into from
Oct 7, 2021

Conversation

jmoralez
Copy link
Collaborator

@jmoralez jmoralez commented Oct 1, 2021

Currently tests/python-package-test/test_basic.py::test_list_to_1d_numpy uses pd_Series within the parametrize calls, which fails when pandas isn't installed. This moves the series creation after checking that pandas is indeed installed and we can be sure that pd_Series(collection) will succeed.

Fixes #4638.

[1] * 10,
[[1], [2]]
])
@pytest.mark.parametrize('collection', ['1d_np', '2d_np', 'pd_float', 'pd_str', '1d_list', '2d_list'])
Copy link
Contributor

Choose a reason for hiding this comment

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

I fear this construct might be hard to understand if the reason behind it is unknown.
Would it be an option to not change the test at all, but adapt the dummy implementation for pandas in lightgbm/compat.py

Current:

try:
    from pandas import DataFrame as pd_DataFrame
    from pandas import Series as pd_Series
    from pandas import concat
    from pandas.api.types import is_sparse as is_dtype_sparse
    PANDAS_INSTALLED = True
except ImportError:
    PANDAS_INSTALLED = False

    class pd_Series:  # type: ignore
        """Dummy class for pandas.Series."""

        pass

    class pd_DataFrame:  # type: ignore
        """Dummy class for pandas.DataFrame."""

        pass

Proposal:

try:
    from pandas import DataFrame as pd_DataFrame
    from pandas import Series as pd_Series
    from pandas import concat
    from pandas.api.types import is_sparse as is_dtype_sparse
    PANDAS_INSTALLED = True
except ImportError:
    PANDAS_INSTALLED = False

    class pd_Series:  # type: ignore
        """Dummy class for pandas.Series."""
        def __init__(self, *args):
            pass

    class pd_DataFrame:  # type: ignore
        """Dummy class for pandas.DataFrame."""
        def __init__(self, *args):
            pass

Copy link
Collaborator

Choose a reason for hiding this comment

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

I agree with @strobelTha . I believe that making all our dummy classes from compat.py creatable with any combination of arguments by adding catch-all constructor will be useful

class pd_Series:  # type: ignore
    """Dummy class for pandas.Series."""
    def __init__(self, *args, **kwargs):
        pass

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I like the refactor because of the following reasons:

  • Moving the collection definition inside the function instead of having that big parametrize seems cleaner.
  • Now the test will run for lists and numpy arrays when pandas isn't installed, whereas previously the whole test was (supposed to be) skipped.
  • In case the test fails the message is more descriptive, i.e. FAILED tests/python_package_test/test_basic.py::test_list_to_1d_numpy[float32-2d_np] vs FAILED tests/python_package_test/test_basic.py::test_list_to_1d_numpy[float64-y1].
class pd_Series:  # type: ignore
    """Dummy class for pandas.Series."""
    def __init__(self, *args, **kwargs):
        pass

@strobelTha would you like to contribute those changes?

Copy link
Collaborator

Choose a reason for hiding this comment

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

@jmoralez I liked reasons #2 and #3 you listed above. Thanks a lot for emphasizing advantages of the current refactor.

And indeed, we can add constructors for our dummy classes in a separate PR.

Copy link
Collaborator

@StrikerRUS StrikerRUS left a comment

Choose a reason for hiding this comment

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

LGTM!
Refer to #4639 (comment) for my more detailed comment.

@StrikerRUS StrikerRUS merged commit 29857c8 into microsoft:master Oct 7, 2021
@jmoralez jmoralez deleted the fix/tests-without-pandas branch October 7, 2021 17:13
@jameslamb jameslamb mentioned this pull request Oct 8, 2021
16 tasks
@github-actions
Copy link

This pull request has been automatically locked since there has not been any recent activity since it was closed. To start a new related discussion, open a new issue at https://github.com/microsoft/LightGBM/issues including a reference to this.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 23, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[python-package] Tests not running without pandas (tests.python_package_test/test_basic.py)
3 participants