本文介绍了如何使用涉及日期的逻辑表达式对 pandas 时间序列进行切片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想了解在Pandas中按时间序列进行切片的问题,我正在研究是否有可能将涉及日期的逻辑语句(合并和,或非操作数)条件合并在一起.

I want to understand slicing with timeseries in Pandas and I am looking at the possibility of combining in a logical statement (combining and , or, not operands) conditions involving dates.

这是一个可重现的示例:

So this is a reproducible example:

HAO_10
Date         Price
2018-01-02  30.240000
2018-01-03  30.629999
2018-01-04  30.860001
2018-01-05  31.010000
2018-01-08  31.389999
2018-01-09  31.309999
2018-01-10  31.400000
2018-01-11  31.580000
2018-01-12  31.680000
2018-01-16  31.200001

HAO_10.iloc[((HAO_10.index < datetime.strptime('2018-01-04', '%Y-%m-%d')) | 

             ((HAO_10.index > datetime.strptime('2018-01-08', '%Y-%m-%d')) & 
        (HAO_10.index  != datetime.strptime('2018-01-12', '%Y-%m-%d')))), ]

这是尝试切出与2018-01-04之前和2018-01-08之后的日期相对应的值,但不对与2018-01-12之前的日期相对应的值进行切分.

This is an attempt to slice out values corresponding to dates before 2018-01-04 and after 2018-01-08 but not the value corresponding to the date 2018-01-12.

有效.

有没有更优雅的方法来完成相同的工作?

Is there a more elegant way to accomplish the same?

推荐答案

首先使用DatetimeIndex .date_range.html"rel =" nofollow noreferrer> date_range union ,然后仅选择 difference ,原始索引为:

Create DatetimeIndex of removed values first with date_range and union, then select only difference with original index:

idx = pd.date_range('2018-01-04','2018-01-08').union(['2018-01-12'])
df = HAO_10.loc[HAO_10.index.difference(idx)]
#another similar solutions
#df = HAO_10.drop(idx, errors='ignore')
#df = HAO_10[~HAO_10.index.isin(idx)]

如果只想使用date,并且index也包含time floor 是您的朋友:

If want working with dates only and index contains also times floor is your friend:

df = HAO_10.loc[HAO_10.index.floor('d').difference(idx)]
#another similar solutions
#df = HAO_10[~HAO_10.index.floor('d').isin(idx)]

print (df)
                Price
2018-01-02  30.240000
2018-01-03  30.629999
2018-01-09  31.309999
2018-01-10  31.400000
2018-01-11  31.580000
2018-01-16  31.200001

您的解决方案应该简化:

Your solution should be simlify:

df = HAO_10[((HAO_10.index < '2018-01-04') | ((HAO_10.index > '2018-01-08') & 
                  (HAO_10.index  != '2018-01-12')))]

这篇关于如何使用涉及日期的逻辑表达式对 pandas 时间序列进行切片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-25 00:42