在日期时间之外创建numpy linspace

时间:2016-06-22 09:36:57

标签: 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(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')

5 个答案:

答案 0 :(得分:12)

您是否考虑过使用pandas?使用this possible duplicate question中的方法,您可以按以下方式使用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]')

答案 1 :(得分:6)

据我所知,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,其日期从startend steps步骤(不包括结束日期,可以轻松修改)。

答案 2 :(得分:5)

截至pandas 0.23,您可以使用date_range

import pandas as pd
x = pd.date_range(min(dates), max(dates), periods=500).to_pydatetime()

答案 3 :(得分:1)

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]')

答案 4 :(得分:0)

最后一个错误告诉我们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')

因此,生成一系列日期时间对象的简单方法是添加时间步长范围。例如,我在这里使用10秒增量

In [1241]: x[0]+np.arange(0,60,10)
Out[1241]: 
array(['1970-01-01T00:16:40', '1970-01-01T00:16:50', '1970-01-01T00:17:00',
       '1970-01-01T00:17:10', '1970-01-01T00:17:20', '1970-01-01T00:17:30'], dtype='datetime64[s]')

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')

尽管有评论,但它似乎只是将整数转换为浮点数。无论如何,它还没有记住datetime64个对象。

如果您想使用user89161's语法,

linspace是可行的方法,否则您只需将所选大小的增量添加到开始日期。

arange适用于这些日期:

In [1256]: np.arange(x[0],x[0]+60,10)
Out[1256]: 
array(['1970-01-01T00:16:40', '1970-01-01T00:16:50', '1970-01-01T00:17:00',
       '1970-01-01T00:17:10', '1970-01-01T00:17:20', '1970-01-01T00:17:30'], dtype='datetime64[s]')