8000 [WIP] Add parameter as_frame to load_data_xxx to return data frames by nsorros · Pull Request #10972 · scikit-learn/scikit-learn · GitHub
[go: up one dir, main page]

Skip to content

[WIP] Add parameter as_frame to load_data_xxx to return data frames #10972

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

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions sklearn/datasets/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ def load_wine(return_X_y=False):
'proline'])


def load_iris(return_X_y=False):
def load_iris(return_X_y=False, as_frame=False):
Copy link
Member

Choose a reason for hiding this comment

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

Please document the parameter

"""Load and return the iris dataset (classification).

The iris dataset is a classic and very easy multi-class classification
Expand Down Expand Up @@ -382,11 +382,19 @@ def load_iris(return_X_y=False):
if return_X_y:
return data, target

feature_names = ['sepal length (cm)', 'sepal width (cm)',
'petal length (cm)', 'petal width (cm)']

if as_frame:
from pandas import Series, DataFrame
data_frame = DataFrame(data, columns=feature_names)
target_series = pd.Series(target, name="class")
return data_frame, target_series

return Bunch(data=data, target=target,
target_names=target_names,
DESCR=fdescr,
feature_names=['sepal length (cm)', 'sepal width (cm)',
'petal length (cm)', 'petal width (cm)'],
feature_names=feature_names,
filename=iris_csv_filename)


Expand Down
10 changes: 10 additions & 0 deletions sklearn/datasets/tests/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
from sklearn.utils.testing import assert_true
from sklearn.utils.testing import assert_equal
from sklearn.utils.testing import assert_raises
from sklearn.utils.testing import SkipTest


DATA_HOME = tempfile.mkdtemp(prefix="scikit_learn_data_home_test_")
Expand Down Expand Up @@ -202,6 +203,15 @@ def test_load_iris():
check_return_X_y(res, partial(load_iris))


def test_load_iris_as_frame():
try:
data_frame, target_series = load_iris(as_frame=True)
Copy link
Member

Choose a reason for hiding this comment

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

I don't get why this would raise SkipTest. Why not use importorskip?

Copy link
Author

Choose a reason for hiding this comment

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

while looking at other fragments of the code i though this was the way to skip the pandas test. let me check importorskip.

Copy link
Member

Choose a reason for hiding this comment

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

pytest is relatively new to us. But it provides importorskip for this kind of application.

assert_equal(data_frame.shape, (150, 4))
assert_equal(target_series.shape[0], 150)
except IOError as :
Copy link
Member

Choose a reason for hiding this comment

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

This is invalid syntax. You need to remove as

SkipTest("Pandas is needed to run the test")


def test_load_wine():
res = load_wine()
assert_equal(res.data.shape, (178, 13))
Expand Down
0