在datetime中创建numpy linspace [英] Creating numpy linspace out of datetime

查看:216
本文介绍了在datetime中创建numpy linspace的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个脚本,它在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(最小(日期),最大(日期),500 )

它会抛出此错误:

  TypeError:*:'datetime.datetime'和'float'的不支持的操作数类型

我还尝试将 datetime 转换为 np.datetime64 ,但是没有t工作:

  dates = [np.datetime64(i)for i in dates] 
x = np。 linspace(min(dates),max(dates),500)

错误:

  TypeError:ufunc乘法不能使用具有类型dtype('< M8 [us]')和dtype('float64')的操作数


大熊猫吗?使用这个可能的重复问题,您可以以下列方式使用 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

 在[3]中:np.asarray(t)
输出[3]:
数组(['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 ='date time64 [ns]')


I'm writing a script that plots some data with dates on the x axis (in matplotlib). I need to create a numpy.linspace out of those dates in order to create a spline afterwards. Is it possible to do that?

What I've tried:

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)

It throws this error:

TypeError: unsupported operand type(s) for *: 'datetime.datetime' and 'float'

I've also tried converting datetime to np.datetime64, but that doesn't work as well:

dates = [ np.datetime64(i) for i in dates ]
x = np.linspace(min(dates), max(dates), 500)

Error:

TypeError: ufunc multiply cannot use operands with types dtype('<M8[us]') and dtype('float64')

解决方案

Have you considered using pandas? Using an approach from this possible duplicate question, you can make use of np.linspace in the following way

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)

To obtain an np.array of the linear timeseries

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

这篇关于在datetime中创建numpy linspace的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆