python - pandas datetimeindex Between_time函数(如何获取not_ Between_time)

标签 python numpy pandas

我有一个 pandas df,我使用 Between_time a 和 b 来清理数据。我如何 获得 non_ Between_time 行为?

我知道我可以尝试类似的事情。

df.between_time['00:00:00', a]   
df.between_time[b,23:59:59']

然后将其合并并对新的df进行排序。这是非常低效的,而且对我来说不起作用,因为我有 23:59:59 和 00:00:00 之间的数据

谢谢

最佳答案

您可以找到时间在 ab 之间的行的索引位置,然后使用 df.index.diff 删除这些行从索引:

import pandas as pd
import io
text = '''\
date,time, val
20120105, 080000,   1
20120105, 080030,   2
20120105, 080100,   3
20120105, 080130,   4
20120105, 080200,   5
20120105, 235959.01, 6
'''

df = pd.read_csv(io.BytesIO(text), parse_dates=[[0, 1]], index_col=0)
index = df.index
ivals = index.indexer_between_time('8:01:30','8:02')
print(df.reindex(index.diff(index[ivals])))

产量

                             val
date_time                       
2012-01-05 08:00:00            1
2012-01-05 08:00:30            2
2012-01-05 08:01:00            3
2012-01-05 23:59:59.010000     6

关于python - pandas datetimeindex Between_time函数(如何获取not_ Between_time),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22288611/

相关文章:

python - Pandas:使用时间窗口或移位变量的新行

包含\x 的格式不正确的字符串的 Python 问题

调用 exec 的 Python 内存错误

python - 切片 numpy 数组

Python numpy 将二维数组转换为已排序的一维数组

python - Threading Condition Acquire lock 并不是真正获取锁

python - 如何在python中连接多个函数

python - Pandas 中 2 列之间的 pct_change,带有行偏移

python如果多个字符串返回句子中包含的单词

python - 使用 pandas 按照模式将两列相乘