|
13 | 13 | from sklearn.utils.testing import assert_raises_regexp
|
14 | 14 | from sklearn.utils import as_float_array, check_array, check_symmetric
|
15 | 15 | from sklearn.utils import check_X_y
|
| 16 | +from sklearn.utils.mocking import MockDataFrame |
16 | 17 | from sklearn.utils.estimator_checks import NotAnArray
|
17 | 18 | from sklearn.random_projection import sparse_random_matrix
|
18 | 19 | from sklearn.linear_model import ARDRegression
|
@@ -218,6 +219,25 @@ def test_check_array():
|
218 | 219 | assert_true(isinstance(result, np.ndarray))
|
219 | 220 |
|
220 | 221 |
|
| 222 | +def test_check_array_pandas_dtype_object_conversion(): |
| 223 | + # test that data-frame like objects with dtype object |
| 224 | + # get converted |
| 225 | + X = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]], dtype=np.object) |
| 226 | + X_df = MockDataFrame(X) |
| 227 | + assert_equal(check_array(X_df).dtype.kind, "f") |
| 228 | + assert_equal(check_array(X_df, ensure_2d=False).dtype.kind, "f") |
| 229 | + # smoke-test against dataframes with column named "dtype" |
| 230 | + X_df.dtype = "Hans" |
| 231 | + assert_equal(check_array(X_df, ensure_2d=False).dtype.kind, "f") |
| 232 | + |
| 233 | + |
| 234 | +def test_check_array_dtype_stability(): |
| 235 | + # test that lists with ints don't get converted to floats |
| 236 | + X = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] |
| 237 | + assert_equal(check_array(X).dtype.kind, "i") |
| 238 | + assert_equal(check_array(X, ensure_2d=False).dtype.kind, "i") |
| 239 | + |
| 240 | + |
221 | 241 | def test_check_array_min_samples_and_features_messages():
|
222 | 242 | # empty list is considered 2D by default:
|
223 | 243 | msg = "0 feature(s) (shape=(1, 0)) while a minimum of 1 is required."
|
|
0 commit comments