简体   繁体   English

重新取样pandas时间序列而不计算新的偏移量

[英]Resampling pandas timeseries without computing a new offset

I'm reading in timeseries data that contains only the available times. 我正在阅读仅包含可用时间的时间序列数据。 This leads to a Series with no missing values, but an unequally spaced index. 这导致Series没有缺失值,但是索引间距不等。 I'd like to convert this to a Series with an equally spaced index with missing values. 我想将它转换为具有缺失值的等间距索引的Series Since I don't know a priori what the spacing will be, I'm currently using a function like 由于我不知道先验的间距是什么,我现在正在使用类似的功能

min_dt      = np.diff(series.index.values).min()
new_spacing = pandas.DateOffset(days=min_dt.days, seconds=min_dt.seconds,
                                microseconds=min_dt.microseconds)
series      = series.asfreq(new_spacing)

to compute what the spacing should be (note that this is using Pandas 0.7.3 - the 0.8 beta code looks slightly differently since I have to use series.index.to_pydatetime() for correct behavior with Numpy 1.6). 计算间距应该是多少(注意这是使用Pandas 0.7.3 - 0.8 beta代码看起来略有不同,因为我必须使用series.index.to_pydatetime()来获得Numpy 1.6的正确行为)。

Is there an easier way to do this operation using the pandas library? 使用pandas库是否有更简单的方法来执行此操作?

If you want NaN's in the places where there is no data, you can just use Minute() located in datetools (as of pandas 0.7.x) 如果你想在没有数据的地方使用NaN,你可以使用位于日期工具中的Minute() (截至pandas 0.7.x)

from pandas.core.datetools import day, Minute
tseries.asfreq(Minute())

That should provide an evenly spaced time series with 1 minute differences with NaNs as the series values where there is no data. 这应该提供均匀间隔的时间序列,其中NaNs为1分钟差异,作为没有数据的系列值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM