Python 创建过期的numpy linspace
我正在编写一个脚本,该脚本在x轴上绘制一些日期数据(在matplotlib中)。我需要在这些日期之外创建一个Python 创建过期的numpy linspace,python,datetime,numpy,matplotlib,Python,Datetime,Numpy,Matplotlib,我正在编写一个脚本,该脚本在x轴上绘制一些日期数据(在matplotlib中)。我需要在这些日期之外创建一个numpy.linspace,以便以后创建样条曲线。有可能吗 我所尝试的: import datetime import numpy as np dates = [ datetime.datetime(2015, 7, 2, 0, 31, 41), datetime.datetime(2015, 7, 2, 1, 35), datetime.datetime(20
numpy.linspace
,以便以后创建样条曲线。有可能吗
我所尝试的:
import datetime
import numpy as np
dates = [
datetime.datetime(2015, 7, 2, 0, 31, 41),
datetime.datetime(2015, 7, 2, 1, 35),
datetime.datetime(2015, 7, 2, 2, 37, 9),
datetime.datetime(2015, 7, 2, 3, 59, 16),
datetime.datetime(2015, 7, 2, 5, 2, 23)]
x = np.linspace(min(dates), max(dates), 500)
它抛出以下错误:
TypeError: unsupported operand type(s) for *: 'datetime.datetime' and 'float'
我也尝试过将datetime
转换为np.datetime64
,但效果并不理想:
dates = [ np.datetime64(i) for i in dates ]
x = np.linspace(min(dates), max(dates), 500)
错误:
TypeError: ufunc multiply cannot use operands with types dtype('<M8[us]') and dtype('float64')
TypeError:ufunc multiply不能使用类型为dtype(“据我所知,np.linspace不支持datetime对象的操作数。但也许我们可以制作自己的函数来大致模拟它:
def date_linspace(start, end, steps):
delta = (end - start) / steps
increments = range(0, steps) * np.array([delta]*steps)
return start + increments
这将为您提供一个np.array,其日期从步骤中的开始
到结束
(不包括结束日期,可以轻松修改).最后一个错误是告诉我们,np.datetime
对象不能相乘。加法已经定义-您可以将n
时间步长添加到一个日期,然后获得另一个日期。但是相乘日期没有任何意义
In [1238]: x=np.array([1000],dtype='datetime64[s]')
In [1239]: x
Out[1239]: array(['1970-01-01T00:16:40'], dtype='datetime64[s]')
In [1240]: x[0]*3
...
TypeError: ufunc multiply cannot use operands with types dtype('<M8[s]') and dtype('int32')
linspace
中的错误是尝试将start
乘以1的结果。
,如完整错误堆栈中所示:
In [1244]: np.linspace(x[0],x[-1],10)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-1244-6e50603c0c4e> in <module>()
----> 1 np.linspace(x[0],x[-1],10)
/usr/lib/python3/dist-packages/numpy/core/function_base.py in linspace(start, stop, num, endpoint, retstep, dtype)
88
89 # Convert float/complex array scalars to float, gh-3504
---> 90 start = start * 1.
91 stop = stop * 1.
92
TypeError: ufunc multiply cannot use operands with types dtype('<M8[s]') and dtype('float64')
您是否考虑过使用pandas
?使用来自的方法,您可以通过以下方式使用np.linspace
import pandas as pd
start = pd.Timestamp('2015-07-01')
end = pd.Timestamp('2015-08-01')
t = np.linspace(start.value, end.value, 100)
t = pd.to_datetime(t)
获取线性时间序列的np.array
In [3]: np.asarray(t)
Out[3]:
array(['2015-06-30T17:00:00.000000000-0700',
'2015-07-01T00:30:54.545454592-0700',
'2015-07-01T08:01:49.090909184-0700',
...
'2015-07-31T01:58:10.909090816-0700',
'2015-07-31T09:29:05.454545408-0700',
'2015-07-31T17:00:00.000000000-0700'], dtype='datetime64[ns]')
从0.23开始,您可以使用:
import numpy#1.15
start=numpy.datetime64('2001-01-01')
end=numpy.datetime64('2019-01-01')
#Linspace(以天为单位):
days=numpy.linspace(start.astype('f8')、end.astype('f8')、dtype='有一个numpy
datetime
,np.datetime64
的包装器(我想)这可能行得通。已经尝试过了,问题中的delta可能不精确,加起来,不精确会导致返回的结束值与处理小时间值时传入的结束值大幅度不匹配。为了包含结束日期,大致相当于np.linspace的endpoint=True
,我添加了endpoint=True
参数,并使用行除数=(步骤-1)if endpoint else steps
和delta=(end-start)/divisor
欢迎使用StackOverflow。虽然此代码片段可能会解决问题,但包含解释确实有助于提高您文章的质量。请花些时间阅读。请记住,您将为将来的读者回答此问题,而这些人可能不知道您代码建议的原因请注意is使用包含Pythondatetime
对象的dtypeobject
创建一个numpy数组。要使用dtypedatetime64
获取numpy数组,您需要使用。To\u numpy()
而不是To\u pydatetime()
。想要使用以下方法添加一个稍微简单的解决方案:t=pd.date\u range>('2015-07-01','2015-08-01',句点=100)
import pandas as pd
start = pd.Timestamp('2015-07-01')
end = pd.Timestamp('2015-08-01')
t = np.linspace(start.value, end.value, 100)
t = pd.to_datetime(t)
In [3]: np.asarray(t)
Out[3]:
array(['2015-06-30T17:00:00.000000000-0700',
'2015-07-01T00:30:54.545454592-0700',
'2015-07-01T08:01:49.090909184-0700',
...
'2015-07-31T01:58:10.909090816-0700',
'2015-07-31T09:29:05.454545408-0700',
'2015-07-31T17:00:00.000000000-0700'], dtype='datetime64[ns]')
import pandas as pd
x = pd.date_range(min(dates), max(dates), periods=500).to_pydatetime()
import numpy # 1.15
start = numpy.datetime64('2001-01-01')
end = numpy.datetime64('2019-01-01')
# Linspace in days:
days = numpy.linspace(start.astype('f8'), end.astype('f8'), dtype='<M8[D]')
# Linspace in milliseconds
MS1D = 24 * 60 * 60 * 1000
daytimes = numpy.linspace(start.astype('f8') * MS1D, end.astype('f8') * MS1D, dtype='<M8[ms]')