如何使用 pandas.date_range() 在指定的开始日期和结束日期之间获取具有 n 个指定周期(相等)的时间序列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25796030/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 22:27:44  来源:igfitidea点击:

How can I use pandas.date_range() to obtain a time series with n specified periods (equal) between a specified start and end date

pythondatetimepandastime-series

提问by arosner09

I'd like to get a list or series of n dates between a start and end date (inclusive of those bounds), but

我想在开始日期和结束日期(包括这些界限)之间获得一个列表或一系列 n 个日期,但是

dateIndex=pd.date_range(start=dt.datetime.today().date(), end=pd.to_datetime(expiry).date(), periods=n)

results with ValueError: Must specify two of start, end, or periods. I cannot use freq=Freq argument because my date range won't be uniform - it may be anywhere from a month to 2 years span, thus I'd like an equally spaced time series with n points.

带有 ValueError 的结果:必须指定开始、结束或句点中的两个。我不能使用 freq=Freq 参数,因为我的日期范围不会是统一的 - 它可能是一个月到 2 年的任何时间跨度,因此我想要一个具有 n 点的等距时间序列。

Thanks!

谢谢!

回答by Andy Hayden

I don't think you can do this just with date_range, but why not use numpy's linspace:

我认为你不能只用 来做到这一点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


You couldpass this as a freq, but this may/will be inaccurate for times which don't evenly divide:

可以将其作为频率传递,但是对于不均匀划分的时间,这可能/将不准确:

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

回答by Joooeey

As of Pandas 0.23 (or earlier), you can just use pandas.date_rangelike you originally tried. It does not raise an error and behaves as you would expect. Example:

从 Pandas 0.23(或更早版本)开始,您可以pandas.date_range像最初尝试的那样使用。它不会引发错误并按您的预期运行。例子:

pd.date_range('2016-01-01', '2017-01-01', periods=13, tz='utc')
Out[44]: 
DatetimeIndex(['2016-01-01 00:00:00+00:00', '2016-01-31 12:00:00+00:00',
               '2016-03-02 00:00:00+00:00', '2016-04-01 12:00:00+00:00',
               '2016-05-02 00:00:00+00:00', '2016-06-01 12:00:00+00:00',
               '2016-07-02 00:00:00+00:00', '2016-08-01 12:00:00+00:00',
               '2016-09-01 00:00:00+00:00', '2016-10-01 12:00:00+00:00',
               '2016-11-01 00:00:00+00:00', '2016-12-01 12:00:00+00:00',
               '2017-01-01 00:00:00+00:00'],
              dtype='datetime64[ns, UTC]', freq=None)

There were 366 days in 2016 (a leap year), so time stamps are 30.5 days apart.

2016 年有 366 天(闰年),因此时间戳相隔 30.5 天。