How to deal with datetime variable?

I’m new to ML and trying to build algorithms based on the data available in the net.
I have taken data available in the reservoir from the past 5 years and i’m trying to predict the water level for the next year.

While executing the code in python i’m getting the below. Can someone help me to resolve this issue.

Sample Dataset :


Code in Python :

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn import model_selection
from sklearn.metrics import accuracy_score
from sklearn.linear_model import LogisticRegression
from sklearn.linear_model import LinearRegression
from sklearn.ensemble import RandomForestClassifier
from sklearn.neighbors import KNeighborsClassifier
from sklearn.svm import SVC





model = LinearRegression(),y_train)

Error :

ValueError: could not convert string to float: ‘19-01-2011’

Hi @dgadela,

The linear regression algorithm cannot handle strings or datetime variables. You need to extract the useful information you can from this datetime variable and then you will have to remove this from the dataset before you build a linear regression model.

For instance, you can get the minute, hour, day of week, month, year etc and then remove this variable.

Yes @dgadela You can not train your model on Date-Time Variable. Instead, try to extract features from it; like: Date, Month , Year. separately. You might even look for better features depending on your problem domain. viz. Which season of year, public holiday or not, any festive season or not, etc.

I hope it helps you. :slight_smile:

© Copyright 2013-2019 Analytics Vidhya