<commit_before>"""Test the california_housing loader.

Skipped if california_housing is not already downloaded to data_home.
"""

import pytest

from sklearn.datasets import fetch_california_housing
from sklearn.datasets.tests.test_common import check_return_X_y
from functools import partial


def fetch(*args, **kwargs):
    return fetch_california_housing(*args, download_if_missing=False, **kwargs)


def _is_california_housing_dataset_not_available():
    try:
        fetch_california_housing(download_if_missing=False)
        return False
    except IOError:
        return True


@pytest.mark.skipif(
    _is_california_housing_dataset_not_available(),
    reason='Download California Housing dataset to run this test'
)
def test_fetch():
    data = fetch()
    assert((20640, 8) == data.data.shape)
    assert((20640, ) == data.target.shape)

    # test return_X_y option
    fetch_func = partial(fetch)
    check_return_X_y(data, fetch_func)


@pytest.mark.skipif(
    _is_california_housing_dataset_not_available(),
    reason='Download California Housing dataset to run this test'
)
def test_fetch_asframe():
    pd = pytest.importorskip('pandas')
    bunch = fetch(as_frame=True)
    frame = bunch.frame
    assert hasattr(bunch, 'frame') is True
    assert frame.shape == (20640, 9)
    assert isinstance(bunch.data, pd.DataFrame)
    assert isinstance(bunch.target, pd.DataFrame)


@pytest.mark.skipif(
    _is_california_housing_dataset_not_available(),
    reason='Download California Housing dataset to run this test'
)
def test_pandas_dependency_message():
    try:
        import pandas  # noqa
        pytest.skip("This test requires pandas to be not installed")
    except ImportError:
        # Check that pandas is imported lazily and that an informative error
        # message is raised when pandas is missing:
        expected_msg = ('fetch_california_housing with as_frame=True'
                        ' requires pandas')
        with pytest.raises(ImportError, match=expected_msg):
            fetch_california_housing(as_frame=True)
<commit_msg>TST Fixes test for California housing (#16389)<commit_after>"""Test the california_housing loader.

Skipped if california_housing is not already downloaded to data_home.
"""

import pytest

from sklearn.datasets import fetch_california_housing
from sklearn.datasets.tests.test_common import check_return_X_y
from functools import partial


def fetch(*args, **kwargs):
    return fetch_california_housing(*args, download_if_missing=False, **kwargs)


def _is_california_housing_dataset_not_available():
    try:
        fetch_california_housing(download_if_missing=False)
        return False
    except IOError:
        return True


@pytest.mark.skipif(
    _is_california_housing_dataset_not_available(),
    reason='Download California Housing dataset to run this test'
)
def test_fetch():
    data = fetch()
    assert((20640, 8) == data.data.shape)
    assert((20640, ) == data.target.shape)

    # test return_X_y option
    fetch_func = partial(fetch)
    check_return_X_y(data, fetch_func)


@pytest.mark.skipif(
    _is_california_housing_dataset_not_available(),
    reason='Download California Housing dataset to run this test'
)
def test_fetch_asframe():
    pd = pytest.importorskip('pandas')
    bunch = fetch(as_frame=True)
    frame = bunch.frame
    assert hasattr(bunch, 'frame') is True
    assert frame.shape == (20640, 9)
    assert isinstance(bunch.data, pd.DataFrame)
    assert isinstance(bunch.target, pd.Series)


@pytest.mark.skipif(
    _is_california_housing_dataset_not_available(),
    reason='Download California Housing dataset to run this test'
)
def test_pandas_dependency_message():
    try:
        import pandas  # noqa
        pytest.skip("This test requires pandas to be not installed")
    except ImportError:
        # Check that pandas is imported lazily and that an informative error
        # message is raised when pandas is missing:
        expected_msg = ('fetch_california_housing with as_frame=True'
                        ' requires pandas')
        with pytest.raises(ImportError, match=expected_msg):
            fetch_california_housing(as_frame=True)

{'fraction_non_alphanumeric': 0.08150095602294455, 'fraction_numerical': 0.009321223709369025, 'mean_word_length': 3.9409681227863045, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6123938', 'n_tokens_mistral': 1313, 'n_tokens_neox': 1265, 'n_words': 320}