Sachila Ranawaka
Sachila Ranawaka

Reputation: 41377

python linear regression date as axis

i'm getting data from mysql and using panda DataFrame i'm separating the data to column

data = pd.DataFrame(data)
print(data.ix[:,3])

0     2006-04-01
1     2006-08-01
2     2006-12-01
3     2006-02-01
4     2006-01-01
5     2006-07-01
6     2006-06-01
7     2006-03-01
8     2006-05-01
9     2006-11-01
10    2006-10-01
11    2006-09-01
12    2007-04-01
13    2007-08-01
14    2007-12-01
15    2007-02-01
16    2007-01-01
17    2007-07-01
18    2007-06-01
19    2007-03-01
20    2007-05-01
21    2007-11-01
22    2007-10-01
23    2007-09-01
24    2009-04-01
25    2009-08-01

when i put this data into linear regression prediction it gives dtype is different. how can i convert this date field to int and put it in the linear prediction

Upvotes: 1

Views: 1854

Answers (1)

piRSquared
piRSquared

Reputation: 294198

you can convert the dates into ordinals by:

data.loc[:, 3].apply(lambda x: x.toordinal())

assuming this column is of type dtype('<M8[ns]')

It would look like:

2006-04-01    732402
2006-08-01    732524
2006-12-01    732646
2006-02-01    732343
2006-01-01    732312
2006-07-01    732493
2006-06-01    732463
2006-03-01    732371
2006-05-01    732432
2006-11-01    732616
2006-10-01    732585
2006-09-01    732555
2007-04-01    732767
2007-08-01    732889
2007-12-01    733011
2007-02-01    732708
2007-01-01    732677
2007-07-01    732858
2007-06-01    732828
2007-03-01    732736
2007-05-01    732797
2007-11-01    732981
2007-10-01    732950
2007-09-01    732920
2009-04-01    733498
2009-08-01    733620
Name: 1, dtype: int64

Upvotes: 1

Related Questions