Using pandas TimeStamp with scikit-learn

sklearn classifiers take pandas' TimeStamp(= datetime64[ns]) as a column in X if all X columns are of this type. But when there are columns TimeStampand float, sklearn refuses to work with TimeStamp.

Is there any workaround besides converting TimeStamp to intusing astype ( int)? (I still need the original column to access dt.year, etc., so it would be ideal not to create a duplicate column to provide the sklearn function.)

import pandas as pd
from sklearn.linear_model import LinearRegression
test = pd.date_range('20000101', periods = 100)
test_df = pd.DataFrame({'date': test})
test_df['a'] = 1
test_df['y'] = 1
lr = LinearRegression()
lr.fit(test_df[['date']], test_df['y']) # works fine
lr.fit(test_df[['date', 'date']], test_df['y']) # works fine
lr.fit(test_df[['date', 'a']], test_df['y']) # complains

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-90-0605fa5bcdfa> in <module>()
----> 1 lr.fit(test_df[['date', 'a']], test_df['y'])

/home/shoya/.pyenv/versions/3.5.0/envs/study-env/lib/python3.5/site-packages/sklearn/linear_model/base.py in fit(self, X, y, sample_weight)
    434         n_jobs_ = self.n_jobs
    435         X, y = check_X_y(X, y, accept_sparse=['csr', 'csc', 'coo'],
--> 436                          y_numeric=True, multi_output=True)
    437 
    438         if ((sample_weight is not None) and np.atleast_1d(

/home/shoya/.pyenv/versions/3.5.0/envs/study-env/lib/python3.5/site-packages/sklearn/utils/validation.py in check_X_y(X, y, accept_sparse, dtype, order, copy, force_all_finite, ensure_2d, allow_nd, multi_output, ensure_min_samples, ensure_min_features, y_numeric, warn_on_dtype, estimator)
    521     X = check_array(X, accept_sparse, dtype, order, copy, force_all_finite,
    522                     ensure_2d, allow_nd, ensure_min_samples,
--> 523                     ensure_min_features, warn_on_dtype, estimator)
    524     if multi_output:
    525         y = check_array(y, 'csr', force_all_finite=True, ensure_2d=False,

/home/shoya/.pyenv/versions/3.5.0/envs/study-env/lib/python3.5/site-packages/sklearn/utils/validation.py in check_array(array, accept_sparse, dtype, order, copy, force_all_finite, ensure_2d, allow_nd, ensure_min_samples, ensure_min_features, warn_on_dtype, estimator)
    402         # make sure we acually converted to numeric:
    403         if dtype_numeric and array.dtype.kind == "O":
--> 404             array = array.astype(np.float64)
    405         if not allow_nd and array.ndim >= 3:
    406             raise ValueError("Found array with dim %d. %s expected <= 2."

TypeError: float() argument must be a string or a number, not 'Timestamp'

-, dtypes , ndarray object, sklearn float, TimeStamp. dtypes datetime64[ns], sklearn .

+4
1

float

test_df['date'] = test_df['date'].astype(int)
0

Source: https://habr.com/ru/post/1629032/


All Articles