Skip to content

BUG: DataFrame.query() throws error when df has duplicate column names #59950

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

Closed
3 tasks done
ddenuyl-bebr opened this issue Oct 3, 2024 · 12 comments · Fixed by #59971
Closed
3 tasks done

BUG: DataFrame.query() throws error when df has duplicate column names #59950

ddenuyl-bebr opened this issue Oct 3, 2024 · 12 comments · Fixed by #59971
Assignees

Comments

@ddenuyl-bebr
Copy link

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 main branch of pandas.

Reproducible Example

import pandas as pd

df = pd.DataFrame({'a': range(3), 'b': range(3), 'c': range(3)}).rename(columns={'b': 'a'})
print(df.query('c == 1'))

Issue Description

Since pandas 2.2.1 this throws an unexpected error:
TypeError: dtype 'a int64 a int64 dtype: object' not understood

This is because DataFrame.query() calls DataFrame.eval() which in turn calls DataFrame._get_cleaned_column_resolvers().

The dict comprehension in DataFrame._get_cleaned_column_resolvers() was changed in version 2.2.1.
version 2.2.0

return {
       clean_column_name(k): Series(
            v, copy=False, index=self.index, name=k
       ).__finalize__(self)
       for k, v in zip(self.columns, self._iter_column_arrays())
       if not isinstance(k, int)
 }

version 2.2.1

return {
            clean_column_name(k): Series(
                v, copy=False, index=self.index, name=k, dtype=self.dtypes[k]
            ).__finalize__(self)
            for k, v in zip(self.columns, self._iter_column_arrays())
            if not isinstance(k, int)
   }

since the dtypes are now checked when the Series are created, this introduces the error described above, since for a duplicate
column name self.dtypes[k] returns a Series instead of single value.

Expected Behavior

  1. I would expect either the behavior prior to v2.2.1 where the above example would return:
>>> df.query('c == 1')
   a  a  c
1  1  1  1

moreover, calling query() on column 'a' also works:

>>> df.query('a == 1')
   a  a  c
1  1  1  1

or
2) If above behavior is unwanted, I would except better error handling, smt like:

>>> df.query('c == 1')
DuplicateColumnError: DataFrame.query() is not supported for DataFrames with duplicate column names

Installed Versions

INSTALLED VERSIONS

commit : bdc79c1
python : 3.11.6.final.0
python-bits : 64
OS : Linux
OS-release : 5.4.0-165-generic
Version : #182-Ubuntu SMP Mon Oct 2 19:43:28 UTC 2023
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8
pandas : 2.2.1
numpy : 1.26.4
pytz : 2024.1
dateutil : 2.8.2
setuptools : 69.1.1
pip : 23.0
Cython : None
pytest : 8.2.0
hypothesis : 6.100.4
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : 5.2.2
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 3.1.3
IPython : None
pandas_datareader : None
adbc-driver-postgresql: None
adbc-driver-sqlite : None
bs4 : None
bottleneck : None
dataframe-api-compat : None
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : None
numba : None
numexpr : None
odfpy : None
openpyxl : 3.1.2
pandas_gbq : 0.19.2
pyarrow : 15.0.0
pyreadstat : None
python-calamine : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : 2.0.27
tables : None
tabulate : 0.9.0
xarray : None
xlrd : None
zstandard : None
tzdata : 2024.1
qtpy : None
pyqt5 : None

@ddenuyl-bebr ddenuyl-bebr added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Oct 3, 2024
@rhshadrach
Copy link
Member

Thanks for the report; it looks like changing DataFrame._get_cleaned_column_resolvers to be

return {
    clean_column_name(k): Series(
        v, copy=False, index=self.index, name=k, dtype=dtype
    ).__finalize__(self)
    for k, v, dtype in zip(self.columns, self._iter_column_arrays(), dtypes)
    if not isinstance(k, int)
}

will resolve the issue. PRs are welcome!

@rhshadrach rhshadrach added expressions pd.eval, query good first issue and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Oct 3, 2024
@miguelcsx
Copy link
Contributor

Hi @rhshadrach , I'm working on this issue, can you assign it to me, thanks 👍

@saldanhad
Copy link
Contributor

Hi @miguelcsx, you can comment take to get this self assigned to you. Please refer to contributing guide here: https://pandas.pydata.org/docs/dev/development/contributing.html#finding-an-issue-to-contribute-to

PS: I am not a maintainer, just tried helping.

@miguelcsx
Copy link
Contributor

take

@miguelcsx
Copy link
Contributor

Hi @miguelcsx, you can comment take to get this self assigned to you. Please refer to contributing guide here: https://pandas.pydata.org/docs/dev/development/contributing.html#finding-an-issue-to-contribute-to

PS: I am not a maintainer, just tried helping.

@saldanhad I didn't know this, thank you very much 🚀

miguelcsx added a commit to miguelcsx/pandas that referenced this issue Oct 4, 2024
- Fixed an issue where `Dataframe.query()` would throw an unexpected
  error

- The error was caused by `self.dtypes[k]`

- Adjusted the behavior to match the behavior prior to pandas version

- Added tests to ensure that `Dataframe.query()` works as expected
miguelcsx added a commit to miguelcsx/pandas that referenced this issue Oct 4, 2024
- Fixed an issue where `Dataframe.query()` would throw an unexpected
  error

- The error was caused by `self.dtypes[k]`

- Adjusted the behavior to match the behavior prior to pandas version

- Added tests to ensure that `Dataframe.query()` works as expected
miguelcsx added a commit to miguelcsx/pandas that referenced this issue Oct 4, 2024
- Fixed an issue where `Dataframe.query()` would throw an unexpected
  error

- The error was caused by `self.dtypes[k]`

- Adjusted the behavior to match the behavior prior to pandas version

- Added tests to ensure that `Dataframe.query()` works as expected
miguelcsx added a commit to miguelcsx/pandas that referenced this issue Oct 4, 2024
- Fixed an issue where `Dataframe.query()` would throw an unexpected
  error

- The error was caused by `self.dtypes[k]`

- Adjusted the behavior to match the behavior prior to pandas version

- Added tests to ensure that `Dataframe.query()` works as expected
miguelcsx added a commit to miguelcsx/pandas that referenced this issue Oct 5, 2024
- Fixed an issue where `Dataframe.query()` would throw an unexpected
  error

- The error was caused by `self.dtypes[k]`

- Adjusted the behavior to match the behavior prior to pandas version

- Added tests to ensure that `Dataframe.query()` works as expected
@Asifussain
Copy link

take

@miguelcsx
Copy link
Contributor

Hi @rhshadrach , I've solved it, and I added a test to make sure it doesn't keep failing, thanks

@ddenuyl-bebr
Copy link
Author

thanks all for the swift response and resolution!

@Asifussain
Copy link

Hi, I am new to open source contribution. I was working with this and the test is in progress. I want to know if this is already closed?

@sunlight798
Copy link
Contributor

Hello, I am new contributor in pandas. Is this issue still open? Can i work on this issue?

@tohfas
Copy link

tohfas commented Oct 9, 2024

Hi, I am looking for this issue for my university assignment. Can this issue be assigned to me? Please let me know. Thanks.

@saldanhad
Copy link
Contributor

It looks like someone is already working on this. For ongoing PRs, sometimes they might not be linked directly to the issue, so please check the PRs as well to see if someone has already submitted one. If you're looking for another task, I recommend checking out issues labeled with good first issue.

You can refer to the contributing guide here

mroeschke added a commit that referenced this issue Nov 5, 2024
…59971)

fix: #59950 handle duplicate column names in dataframe queries

- Fixed an issue where `Dataframe.query()` would throw an unexpected
  error

- The error was caused by `self.dtypes[k]`

- Adjusted the behavior to match the behavior prior to pandas version

- Added tests to ensure that `Dataframe.query()` works as expected

Co-authored-by: Matthew Roeschke <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants