Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUG: DataFrame.set_axis() and rename_axis() fail if allows_duplicate_labels=False #44958

Open
3 tasks done
johnzangwill opened this issue Dec 18, 2021 · 0 comments
Open
3 tasks done
Labels
Bug Indexing Related to indexing on series/frames, not to indexes themselves

Comments

@johnzangwill
Copy link
Contributor

Pandas version checks

  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • I have confirmed this bug exists on the master branch of pandas.

Reproducible Example

import pandas as pd
df = pd.DataFrame([[1]]).set_flags(allows_duplicate_labels=False)
df.set_axis(labels=["a"])
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_3728/3468635665.py in <module>
      1 df = df.set_flags(allows_duplicate_labels=False)
----> 2 df.set_axis(labels=["a"])

c:\users\john\onedrive\documents\github\pandas_johnzangwill\pandas\util\_decorators.py in wrapper(*args, **kwargs)
    309                     stacklevel=stacklevel,
    310                 )
--> 311             return func(*args, **kwargs)
    312 
    313         return wrapper

c:\users\john\onedrive\documents\github\pandas_johnzangwill\pandas\core\frame.py in set_axis(self, labels, axis, inplace)
   4776     @Appender(NDFrame.set_axis.__doc__)
   4777     def set_axis(self, labels, axis: Axis = 0, inplace: bool = False):
-> 4778         return super().set_axis(labels, axis=axis, inplace=inplace)
   4779 
   4780     @Substitution(**_shared_doc_kwargs)

c:\users\john\onedrive\documents\github\pandas_johnzangwill\pandas\core\generic.py in set_axis(self, labels, axis, inplace)
    753         """
    754         self._check_inplace_and_allows_duplicate_labels(inplace)
--> 755         return self._set_axis_nocheck(labels, axis, inplace)
    756 
    757     @final

c:\users\john\onedrive\documents\github\pandas_johnzangwill\pandas\core\generic.py in _set_axis_nocheck(self, labels, axis, inplace)
    762         else:
    763             obj = self.copy()
--> 764             obj.set_axis(labels, axis=axis, inplace=True)
    765             return obj
    766 

c:\users\john\onedrive\documents\github\pandas_johnzangwill\pandas\util\_decorators.py in wrapper(*args, **kwargs)
    309                     stacklevel=stacklevel,
    310                 )
--> 311             return func(*args, **kwargs)
    312 
    313         return wrapper

c:\users\john\onedrive\documents\github\pandas_johnzangwill\pandas\core\frame.py in set_axis(self, labels, axis, inplace)
   4776     @Appender(NDFrame.set_axis.__doc__)
   4777     def set_axis(self, labels, axis: Axis = 0, inplace: bool = False):
-> 4778         return super().set_axis(labels, axis=axis, inplace=inplace)
   4779 
   4780     @Substitution(**_shared_doc_kwargs)

c:\users\john\onedrive\documents\github\pandas_johnzangwill\pandas\core\generic.py in set_axis(self, labels, axis, inplace)
    752         %(klass)s.rename_axis : Alter the name of the index%(see_also_sub)s.
    753         """
--> 754         self._check_inplace_and_allows_duplicate_labels(inplace)
    755         return self._set_axis_nocheck(labels, axis, inplace)
    756 

c:\users\john\onedrive\documents\github\pandas_johnzangwill\pandas\core\generic.py in _check_inplace_and_allows_duplicate_labels(self, inplace)
   4035     def _check_inplace_and_allows_duplicate_labels(self, inplace):
   4036         if inplace and not self.flags.allows_duplicate_labels:
-> 4037             raise ValueError(
   4038                 "Cannot specify 'inplace=True' when "
   4039                 "'self.flags.allows_duplicate_labels' is False."

ValueError: Cannot specify 'inplace=True' when 'self.flags.allows_duplicate_labels' is False.

df.rename_axis("a")
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_3728/2143830946.py in <module>
----> 1 df.rename_axis("a")

c:\users\john\onedrive\documents\github\pandas_johnzangwill\pandas\util\_decorators.py in wrapper(*args, **kwargs)
    322         @wraps(func)
    323         def wrapper(*args, **kwargs) -> Callable[..., Any]:
--> 324             return func(*args, **kwargs)
    325 
    326         kind = inspect.Parameter.POSITIONAL_OR_KEYWORD

c:\users\john\onedrive\documents\github\pandas_johnzangwill\pandas\core\generic.py in rename_axis(self, mapper, **kwargs)
   1319             )
   1320             if non_mapper:
-> 1321                 return self._set_axis_name(mapper, axis=axis, inplace=inplace)
   1322             else:
   1323                 raise ValueError("Use `.rename` to alter labels with a mapper.")

c:\users\john\onedrive\documents\github\pandas_johnzangwill\pandas\core\generic.py in _set_axis_name(self, name, axis, inplace)
   1398         inplace = validate_bool_kwarg(inplace, "inplace")
   1399         renamed = self if inplace else self.copy()
-> 1400         renamed.set_axis(idx, axis=axis, inplace=True)
   1401         if not inplace:
   1402             return renamed

c:\users\john\onedrive\documents\github\pandas_johnzangwill\pandas\util\_decorators.py in wrapper(*args, **kwargs)
    309                     stacklevel=stacklevel,
    310                 )
--> 311             return func(*args, **kwargs)
    312 
    313         return wrapper

c:\users\john\onedrive\documents\github\pandas_johnzangwill\pandas\core\frame.py in set_axis(self, labels, axis, inplace)
   4776     @Appender(NDFrame.set_axis.__doc__)
   4777     def set_axis(self, labels, axis: Axis = 0, inplace: bool = False):
-> 4778         return super().set_axis(labels, axis=axis, inplace=inplace)
   4779 
   4780     @Substitution(**_shared_doc_kwargs)

c:\users\john\onedrive\documents\github\pandas_johnzangwill\pandas\core\generic.py in set_axis(self, labels, axis, inplace)
    752         %(klass)s.rename_axis : Alter the name of the index%(see_also_sub)s.
    753         """
--> 754         self._check_inplace_and_allows_duplicate_labels(inplace)
    755         return self._set_axis_nocheck(labels, axis, inplace)
    756 

c:\users\john\onedrive\documents\github\pandas_johnzangwill\pandas\core\generic.py in _check_inplace_and_allows_duplicate_labels(self, inplace)
   4035     def _check_inplace_and_allows_duplicate_labels(self, inplace):
   4036         if inplace and not self.flags.allows_duplicate_labels:
-> 4037             raise ValueError(
   4038                 "Cannot specify 'inplace=True' when "
   4039                 "'self.flags.allows_duplicate_labels' is False."

ValueError: Cannot specify 'inplace=True' when 'self.flags.allows_duplicate_labels' is False.

Issue Description

If a DataFrame has its flags set with allows_duplicate_labels=False then set_axis() and rename_axis() raise due to intenal use of inplace=True.

Expected Behavior

df.set_axis(labels=["a"])
   0
a  1

df.rename_axis("a")
   0
a   
0  1

Installed Versions

INSTALLED VERSIONS

commit : 47eb219
python : 3.8.12.final.0
python-bits : 64
OS : Windows
OS-release : 10
Version : 10.0.19043
machine : AMD64
processor : Intel64 Family 6 Model 158 Stepping 9, GenuineIntel
byteorder : little
LC_ALL : None
LANG : None
LOCALE : English_United Kingdom.1252

pandas : 1.4.0.dev0+1415.g47eb219889
numpy : 1.21.4
pytz : 2021.3
dateutil : 2.8.2
pip : 21.3.1
setuptools : 59.4.0
Cython : 0.29.25
pytest : 6.2.5
hypothesis : 6.31.4
sphinx : 4.3.1
blosc : None
feather : None
xlsxwriter : 3.0.2
lxml.etree : 4.6.4
html5lib : 1.1
pymysql : None
psycopg2 : None
jinja2 : 3.0.3
IPython : 7.30.1
pandas_datareader: None
bs4 : 4.10.0
bottleneck : 1.3.2
fsspec : 2021.11.0
fastparquet : 0.7.2
gcsfs : 2021.11.0
matplotlib : 3.5.0
numexpr : 2.8.0
odfpy : None
openpyxl : 3.0.9
pandas_gbq : None
pyarrow : 6.0.1
pyxlsb : None
s3fs : 2021.11.0
scipy : 1.7.3
sqlalchemy : 1.4.28
tables : 3.6.1
tabulate : 0.8.9
xarray : 0.18.2
xlrd : 2.0.1
xlwt : 1.3.0
numba : 0.53.1

@johnzangwill johnzangwill added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Dec 18, 2021
@mroeschke mroeschke added Indexing Related to indexing on series/frames, not to indexes themselves and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Dec 27, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Indexing Related to indexing on series/frames, not to indexes themselves
Projects
None yet
Development

No branches or pull requests

2 participants