将 polyfit 与日期系列一起使用

Using polyfit with date series

如何将带有此类数据的日期系列管理到 polyfit 中而不会出现任何错误。

data1 = np.array(['1801-01-01', '1802-01-01', '1803-01-01', '1804-01-01', '1805-01-01'])
data2 = np.array([5, 6, 7, 8, 9])
ser1 = pd.Series(data1)
ser2 = pd.Series(data2)
date = pd.to_datetime(ser1)
a, b = np.polyfit(date, ser2, 1)

错误消息说

UFuncTypeError: ufunc 'add' cannot use operands with types dtype('<M8[ns]') and dtype('float64')

有没有办法将日期转换为浮点数以使 polyfit 工作?

通过转换为 int64:

将日期时间转换为 unix 时间
a, b = np.polyfit(date.astype(np.int64), ser2, 1)
print (a, b)
3.169241676570259e-17 174.020212777763