aro*_*r09 3 python datetime time-series pandas
我想在开始日期和结束日期(包括那些边界)之间获得一个列表或一系列n个日期,但是
dateIndex=pd.date_range(start=dt.datetime.today().date(), end=pd.to_datetime(expiry).date(), periods=n)
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
您可以将其作为频率传递,但是对于不均匀划分的时间,这可能/将是不准确的:
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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5910 次 |
| 最近记录: |