从 pandas 数据框中删除leap年 [英] Remove leap year day from pandas dataframe
本文介绍了从 pandas 数据框中删除leap年的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有傻瓜.数据框:
datetime
2012-01-01 125.5010
2012-01-02 125.5010
2012-01-03 125.5010
2012-02-04 125.5010
2012-02-05 125.5010
2012-02-29 125.5010
2012-02-28 125.5010
2016-01-07 125.5010
2016-01-08 125.5010
2016-02-29 81.6237
我想删除所有与2月29日相对应的行,结果是foll.数据框:
I would like to drop all rows corresponding to Feb 29th, resulting in foll. data frame:
datetime
2012-01-01 125.5010
2012-01-02 125.5010
2012-01-03 125.5010
2012-02-04 125.5010
2012-02-05 125.5010
2012-02-28 125.5010
2016-01-07 125.5010
2016-01-08 125.5010
现在,我只是手动进行操作
Right now, I am just doing it manually:
df.drop(df.index[['2012-02-29']])
.我如何才能使其在所有年份都有效,而不必手动指定行索引.
df.drop(df.index[['2012-02-29']])
. How can I make it so that it works for all years, without haveing to manually specify row index.
推荐答案
IIUC you can mask it and remove by loc
:
def is_leap_and_29Feb(s):
return (s.index.year % 4 == 0) &
((s.index.year % 100 != 0) | (s.index.year % 400 == 0)) &
(s.index.month == 2) & (s.index.day == 29)
mask = is_leap_and_29Feb(df)
print mask
#[False False False False False True False False False True]
print df.loc[~mask]
# datetime
#2012-01-01 125.501
#2012-01-02 125.501
#2012-01-03 125.501
#2012-02-04 125.501
#2012-02-05 125.501
#2012-02-28 125.501
#2016-01-07 125.501
#2016-01-08 125.501
这篇关于从 pandas 数据框中删除leap年的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文