【问题标题】:pandas timeseries between_datetime function?日期时间函数之间的熊猫时间序列?
【发布时间】:2013-06-05 09:39:55
【问题描述】:

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

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

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

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

【问题讨论】:

  • 两者我都需要

标签: python pandas time-series


【解决方案1】:

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

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)

要选择 1 月 2 日和 3 日 20:00 和 22:00 之间的时间,您只需执行以下操作:

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

【讨论】:

    猜你喜欢
    • 2020-10-19
    • 2014-01-01
    • 2018-06-22
    • 2021-05-08
    • 1970-01-01
    • 2021-06-17
    • 2017-02-23
    • 2020-10-02
    相关资源
    最近更新 更多