2016-10-24 49 views
1

我有两个问题,我相信都发布到日期格式。分散和线性回归的日期问题

我有日期和值的CVS:

2012-01-03 00:00:00  95812  
2012-01-04 00:00:00 101265 
... 
2016-10-21 00:00:00  93594 

后,我与read_csv我试图解析的日期加载:

X.Dated = pd.to_datetime(X.Dated, format='%Y-%m-%d %H:%M:%S', errors='raise') 

我也试图与:

dateparse = lambda x: pd.datetime.strptime(x, '%Y-%m-%d %H:%M:%S') 
X = pd.read_csv('sales.csv', parse_dates=['Dated'], date_parser=dateparse) 

infer_datetime_format的说法。

所有这些似乎工作正常,因为当我打印出来的日期看起来像这样:2012-01-03

问题出现,当我想要绘制在图表的数据,该行:

ax.scatter(X.Dated, X.Val, c='green', marker='.') 

给我一个错误:

TypeError: invalid type promotion 

而且当我尝试用线性回归使用()算法拟合命令 工作正常,但得分和预测给了我这个错误:

TypeError: Cannot cast array data from dtype('<M8[ns]') to dtype('float64') according to the rule 'safe' 

我尝试了很多事情来解决它,但没有运气。 任何帮助,将不胜感激。

回答

5

ax.scatter(目前)不接受熊猫系列,但它可以接受大熊猫时间戳列表(例如X['Dated'].tolist()),或D型datetime64[ns](如X['Dated'].values)的NumPy的数组:

import pandas as pd 
import matplotlib.pyplot as plt 

X = pd.DataFrame({'Dated': [pd.Timestamp('2012-01-03 00:00:00'), 
          pd.Timestamp('2012-01-04 00:00:00'), 
          pd.Timestamp('2016-10-21 00:00:00')], 
        'Val': [95812, 101265, 93594]}) 

fig, ax = plt.subplots() 
# ax.scatter(X['Dated'].tolist(), X['Val'], c='green', marker='.', s=200) 
ax.scatter(X['Dated'].values, X['Val'], c='green', marker='.', s=200) 
plt.show() 

enter image description here


Under the hood, the ax.scatter method calls

x = self.convert_xunits(x) 
y = self.convert_yunits(y) 

来处理类似日期的输入。 convert_xunits将NumPy datetime64数组转换为Matplotlib datenums,但它将Pandas时间序列转换为NumPy datetime64数组。

所以,当一个时间序列熊猫被作为输入传递到ax.scatter,代码最终失败时this line is reached

offsets = np.dstack((x, y)) 

np.dstack试图促进其输入的dtypes到一个共同的D型。如果x具有D型datetime64[ns]y具有D型float64,然后

TypeError: invalid type promotion 

上升,因为没有本地NumPy的D类是既兼容。

+0

感谢它的帮助,我现在可以在图表上显示它。这回答了我的问题的第一部分。你知道为什么我有另一个错误:根据规则'安全',无法将数组数据从dtype(' Greg

+0

@Greg:我补充了一些关于错误来自何处的解释。最终,当np.dstack试图将x和y输入组合到一个数组中时(通常使用dtype),会引发TypeError。由于'datetime64 [ns]'和'float' dtypes没有兼容的公共dtype,所以会引发TypeError。 – unutbu