pandas 删除索引值列表时出现错误:“not found in axis”

w6mmgewl  于 2023-11-15  发布在  其他
关注(0)|答案(1)|浏览(359)

我试图从一个时间值从上午6点到下午8点的时间框架中删除每年两天的日期,日期从15.07到20.10。因此,我创建了一个列表,其中包含所有应该删除的日期,如下所示:

drop_list =[] 

for i in range(0,6):
    Year = 1999 + i
    drop_list.append(str(Year)+'-07-15')
    drop_list.append(str(Year)+'-07-16')

字符串
我的数据看起来像这样:
x1c 0d1x的数据
当我现在打电话:

y = y.drop(drop_list)


我得到:
KeyError:“2000 -07-15”“2000-07- 15”“2000 - 07 - 16 "" 2001-07 - 15”\n“2001 - 07 - 16 "”2002-07-15"“2002 - 07 - 16 "”2003 - 07 - 15“”2003 - 07 - 16“\n”2004-07- 15“”2004-07- 16 ']未在轴中找到”
任何建议我错过了什么?

htrmnn0y

htrmnn0y1#

如果要从日期时间索引中删除,则需要传递日期时间。
例如

>>> s = pd.Series([1,2,3], index=pd.to_datetime(['2020-01-01', '2020-01-02', '2020-01-03']))

>>> s
2020-01-01    1
2020-01-02    2
2020-01-03    3
dtype: int64

>>> s.drop(pd.to_datetime(['2020-01-01']))
2020-01-02    2
2020-01-03    3
dtype: int64

字符串
在您的情况下,y.drop(pd.to_datetime(drop_list))可能会工作。

相关问题