pandas 时间序列 between_datetime 函数?

pandas timeseries between_datetime function?( pandas 时间序列 between_datetime 函数?)
本文介绍了 pandas 时间序列 between_datetime 函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我一直在 pandas 中使用 TimeSeries 的 between_time 方法,它返回指定时间之间的所有值,而不管它们的日期.

I have been using the between_time method of TimeSeries in pandas, which returns all values between the specified times, regardless of their date.

但我需要选择两个日期和时间,因为我的时间序列结构包含多个日期.

But I need to select both date and time, because my timeseries structure contains multiple dates.

解决这个问题的一种方法虽然很不灵活,但只是迭代值并删除那些不相关的值.

One way of solving this, though quite inflexible, is just iterate over the values and remove those which are not relevant.

有没有更优雅的方式来做到这一点?

Is there a more elegant way of doing this ?

推荐答案

可以先选择感兴趣的日期,然后使用between_time.例如,假设您有一个 72 小时的时间序列:

You can select the dates that are of interest first, and then use between_time. For example, suppose you have a time series of 72 hours:

import pandas as pd
from numpy.random import randn

rng = pd.date_range('1/1/2013', periods=72, freq='H')
ts = pd.Series(randn(len(rng)), index=rng)

选择 20:00 和1月2日和3日22:00你可以简单地做:

To select the between 20:00 & 22:00 on the 2nd and 3rd of January you can simply do:

ts['2013-01-02':'2013-01-03'].between_time('20:00', '22:00')

给你这样的东西:

2013-01-02 20:00:00    0.144399
2013-01-02 21:00:00    0.886806
2013-01-02 22:00:00    0.126844
2013-01-03 20:00:00   -0.464741
2013-01-03 21:00:00    1.856746
2013-01-03 22:00:00   -0.286726

这篇关于 pandas 时间序列 between_datetime 函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

Seasonal Decomposition of Time Series by Loess with Python(Loess 用 Python 对时间序列进行季节性分解)
Resample a time series with the index of another time series(使用另一个时间序列的索引重新采样一个时间序列)
How can I simply calculate the rolling/moving variance of a time series in python?(如何在 python 中简单地计算时间序列的滚动/移动方差?)
How to use Dynamic Time warping with kNN in python(如何在python中使用动态时间扭曲和kNN)
Keras LSTM: a time-series multi-step multi-features forecasting - poor results(Keras LSTM:时间序列多步多特征预测 - 结果不佳)
Python pandas time series interpolation and regularization(Python pandas 时间序列插值和正则化)