我有一个 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 之间的数据
谢谢
最佳答案
您可以找到时间在 a
和 b
之间的行的索引位置,然后使用 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/