使用"Date"作为数据帧列时出现意外行为



在以下情况下(使用Pandas 1.4.1(,是什么导致了问题?

给定一个以Date作为其中一列名称的数据帧:

In [9]: df = pd.DataFrame(dict(Date=[0], x=[1]))
In [10]: df
Out[10]:
Date  x
0     0  1

可以对该列进行索引

In [12]: df['Date']
Out[12]:
0    0
Name: Date, dtype: int64

尽管不能删除该列

In [11]: df.drop('Date')
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-11-0e8ebf6dc639> in <module>
----> 1 df.drop('Date')
~anaconda3libsite-packagespandasutil_decorators.py in wrapper(*args, **kwargs)
309                     stacklevel=stacklevel,
310                 )
--> 311             return func(*args, **kwargs)
312
313         return wrapper
~anaconda3libsite-packagespandascoreframe.py in drop(self, labels, axis, index, columns, level, inplace, errors)
4946                 weight  1.0     0.8
4947         """
-> 4948         return super().drop(
4949             labels=labels,
4950             axis=axis,
~anaconda3libsite-packagespandascoregeneric.py in drop(self, labels, axis, index, columns, level, inplace, errors)
4277         for axis, labels in axes.items():
4278             if labels is not None:
-> 4279                 obj = obj._drop_axis(labels, axis, level=level, errors=errors)
4280
4281         if inplace:
~anaconda3libsite-packagespandascoregeneric.py in _drop_axis(self, labels, axis, level, errors, consolidate, only_slice)
4321                 new_axis = axis.drop(labels, level=level, errors=errors)
4322             else:
-> 4323                 new_axis = axis.drop(labels, errors=errors)
4324             indexer = axis.get_indexer(new_axis)
4325
~anaconda3libsite-packagespandascoreindexesbase.py in drop(self, labels, errors)
6642         if mask.any():
6643             if errors != "ignore":
-> 6644                 raise KeyError(f"{list(labels[mask])} not found in axis")
6645             indexer = indexer[~mask]
6646         return self.delete(indexer)
KeyError: "['Date'] not found in axis"

并且该列不能重命名

In [22]: df.rename({'Date':'y'}, errors='raise')
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-22-7277fb5b8ca6> in <module>
----> 1 df.rename({'Date':'y'}, errors='raise')
~anaconda3libsite-packagespandascoreframe.py in rename(self, mapper, index, columns, axis, copy, inplace, level, errors)
5075         4  3  6
5076         """
-> 5077         return super()._rename(
5078             mapper=mapper,
5079             index=index,
~anaconda3libsite-packagespandascoregeneric.py in _rename(self, mapper, index, columns, axis, copy, inplace, level, errors)
1159                         if indexer[index] == -1
1160                     ]
-> 1161                     raise KeyError(f"{missing_labels} not found in axis")
1162
1163             new_index = ax._transform_index(f, level=level)
KeyError: "['Date'] not found in axis"

您必须指定要删除一列并重命名一列。您可以使用columns关键字或axis=1参数来执行此操作。以下内容应该有效:

df.drop(columns=['Date']) 

df.drop('Date', axis=1)

df.rename(columns={'Date':'y'})

df.rename({'Date':'y'}, axis=1) 

最新更新