我想在开始日期和结束日期(包括那些边界)之间获得一个列表或一系列n个日期,但是
dateIndex=pd.date_range(start=dt.datetime.today().date(), end=pd.to_datetime(expiry).date(), periods=n)
ValueError的结果:必须指定start,end或periods中的两个.我不能使用freq = Freq参数,因为我的日期范围不一致 – 它可能是从一个月到两年的任何时间跨度,因此我想要一个n点的等间隔时间序列.
谢谢!
解决方法:
我不认为你可以用date_range
做到这一点,但为什么不使用numpy的linspace
:
In [11]: start = pd.Timestamp('2012-01-01')
In [12]: end = pd.Timestamp('2012-02-01')
In [13]: np.linspace(start.value, end.value, 10) # 10 dates inclusive
Out[13]:
array([ 1.32537600e+18, 1.32567360e+18, 1.32597120e+18,
1.32626880e+18, 1.32656640e+18, 1.32686400e+18,
1.32716160e+18, 1.32745920e+18, 1.32775680e+18,
1.32805440e+18])
In [14]: pd.to_datetime(np.linspace(start.value, end.value, 10))
Out[14]:
<class 'pandas.tseries.index.DatetimeIndex'>
[2012-01-01 00:00:00, ..., 2012-02-01 00:00:00]
Length: 10, Freq: None, Timezone: None
您可以将其作为频率传递,但是对于不均匀划分的时间,这可能/将是不准确的:
In [21]: (end - start)/ 9
Out[21]: datetime.timedelta(3, 38400)
In [22]: ((end - start)/ 9).total_seconds()
Out[22]: 297600.0
# Note: perhaps there's a better way to pass this as a freq?
In [23]: pd.date_range(start=start, end=end, freq='%iS' % ((end - start)/ 9).total_seconds())
Out[23]:
<class 'pandas.tseries.index.DatetimeIndex'>
[2012-01-01 00:00:00, ..., 2012-02-01 00:00:00]
Length: 10, Freq: 297600S, Timezone: None
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。