Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Unexpected behaviour using `Date` as a dataframe column

What seems to cause the problem in the following situation (using Pandas 1.4.1)?

Given a dataframe with Date as a name of one of the columns:

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

In [10]: df
Out[10]:
   Date  x
0     0  1

It is possible to index that column

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

Although that column cannot be dropped

In [11]: df.drop('Date')
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-11-0e8ebf6dc639> in <module>
----> 1 df.drop('Date')

~\anaconda3\lib\site-packages\pandas\util\_decorators.py in wrapper(*args, **kwargs)
    309                     stacklevel=stacklevel,
    310                 )
--> 311             return func(*args, **kwargs)
    312
    313         return wrapper

~\anaconda3\lib\site-packages\pandas\core\frame.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,

~\anaconda3\lib\site-packages\pandas\core\generic.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:

~\anaconda3\lib\site-packages\pandas\core\generic.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

~\anaconda3\lib\site-packages\pandas\core\indexes\base.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"

And that column cannot be renamed

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')

~\anaconda3\lib\site-packages\pandas\core\frame.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,

~\anaconda3\lib\site-packages\pandas\core\generic.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"

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

You have to specify that you’re dropping a column and renaming a column. You can do so using the columns keyword or axis=1 parameter. The following should work:

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

or

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

and

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

or

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

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading