pandas 从熊猫数据框中删除闰年
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34966422/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Remove leap year day from pandas dataframe
提问by user308827
I have the foll. dataframe:
我有一个愚蠢的。数据框:
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
I would like to drop all rows corresponding to Feb 29th, resulting in foll. data frame:
我想删除对应于 2 月 29 日的所有行,从而导致 foll。数据框:
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']])
. How can I make it so that it works for all years, without haveing to manually specify row index.
df.drop(df.index[['2012-02-29']])
. 我怎样才能使它适用于所有年份,而不必手动指定行索引。
采纳答案by jezrael
IIUC you can mask it and remove by loc
:
IIUC 你可以屏蔽它并通过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
回答by Fabio Lamanna
If your dataframe has already the datetime
column as index you can:
如果您的数据框已经将该datetime
列作为索引,您可以:
df = df[~((df.index.month == 2) & (df.index.day == 29))]
this should remove the rows containing the day February 29th for all years.
这应该删除所有年份中包含 2 月 29 日这一天的行。
回答by Markus Weninger
You can see the date as string
and see if it ends with 02-29
:
您可以将日期视为string
并查看它是否以以下结尾02-29
:
df = df[~df.index.str.endswith('02-29')]
Using this method, you can use any string-comparism method like contains
, etc.
使用此方法,您可以使用任何字符串比较方法,例如contains
等。