python线性回归以日期为轴

标签 python pandas linear-regression

我从 mysql 获取数据并使用 panda DataFrame 将数据分离到列

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

当我将这些数据放入线性回归预测时,它给出的 dtype 是不同的。我如何将此日期字段转换为 int 并将其放入线性预测

最佳答案

您可以通过以下方式将日期转换为序数:

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

假设此列的类型为dtype('<M8[ns]')

它看起来像:

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

关于python线性回归以日期为轴,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37706623/

相关文章:

python - 将列表转换为矩阵

python - 为 flask 投票应用程序输入选项

python - osx -/usr/bin/python 和/System/Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7 有什么区别?

python - Pandas DataFrame 将某些列合并到嵌套 JSON 中

python - 用其他时间间隔填补时间间隔中的空白

python - pandas,matplotlib : a way to assign same colors, 子图中相同列标签的线条样式?

python - 如果 df 单元格中存在 ".",如何检查和过滤掉 Dataframe 中的行

numpy - numpy中对数对数直方图的线性回归

weka - 具有标称属性 weka 的线性回归

r - 当有多个因素水平作为基线时,如何解释 R 线性回归?