How to deal with datetime variable?

Hi,
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 :

01-01-2004,3.90,0.00,268.00,0.00

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

url=“C:/Users/id856158/Downloads/creditcardfraud/chennai_reservoir_levels.csv”
names=[‘Date’,‘POONDI’,‘CHOLAVARAM’,‘REDHILLS’,‘CHEMBARAMBAKKAM’]
dataset=pd.read_csv(url,names=names)

print(dataset.head(5))
print(dataset.shape)

array=dataset.values
X=array[:,0:5]

x_train,x_test,y_train,y_test=model_selection.train_test_split(X,Y,test_size=0.2,random_state=2)

model = LinearRegression()
model.fit(x_train,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