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: True cannot be cast to bool #58159

Closed
3 tasks done
NathanDWalsh opened this issue Apr 5, 2024 · 6 comments · Fixed by #58994
Closed
3 tasks done

BUG: True cannot be cast to bool #58159

NathanDWalsh opened this issue Apr 5, 2024 · 6 comments · Fixed by #58994
Assignees
Labels
Bug IO Excel read_excel, to_excel NA - MaskedArrays Related to pd.NA and nullable extension arrays

Comments

@NathanDWalsh
Copy link

NathanDWalsh commented Apr 5, 2024

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({"bool_column": [True]}, dtype=pd.BooleanDtype.name)
df.to_excel("test_pandas_bool.xlsx", index=False)

df2 = pd.read_excel("test_pandas_bool.xlsx", dtype={"bool_column": pd.BooleanDtype.name})

Issue Description

When:

  • Reading an Excel file with pd.read_excel function
  • One of the Excel columns is a boolean type
  • The dtype for that column is specificed as pd.BooleanDtype

The following error is raised:

  • True cannot be cast to bool

Expected Behavior

Bool literal should be read, no error should be raised.

Installed Versions

commit : bdc79c1
python : 3.11.4.final.0
python-bits : 64
OS : Windows
OS-release : 10
Version : 10.0.19045
machine : AMD64
processor : Intel64 Family 6 Model 165 Stepping 2, GenuineIntel
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : English_United Kingdom.1252

pandas : 2.2.1
numpy : 1.26.4
pytz : 2023.3
dateutil : 2.8.2
setuptools : 68.1.2
pip : 24.0
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : 5.1.0
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 3.1.2
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 : None
pyarrow : 12.0.1
pyreadstat : None
python-calamine : None
pyxlsb : None
s3fs : None
scipy : 1.10.1
sqlalchemy : 2.0.20
tables : None
tabulate : None
xarray : None
xlrd : None
zstandard : None
tzdata : 2023.3
qtpy : None
pyqt5 : None

@NathanDWalsh NathanDWalsh added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Apr 5, 2024
@CaelThompson
Copy link

take

@rhshadrach rhshadrach added IO Excel read_excel, to_excel NA - MaskedArrays Related to pd.NA and nullable extension arrays and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Apr 7, 2024
@aabhinavg
Copy link

"Hi, @nwalsh82 I've found a patch that could potentially fix the issue. You can find the changes in the following link: shiny-new-feature patch. Could you please take a look and see if it addresses the problem? Let me know if you have any questions about it!"

@CaelThompson CaelThompson removed their assignment May 21, 2024
@rmhowe425
Copy link
Contributor

rmhowe425 commented Jun 2, 2024

@aabhinavg Have you created a PR for this bug? If so, could you link your PR to this issue by following the contribution guidelines?

I believe that I've found @aabhinavg's PR for this issue. It looks like it's closed because the PR went stale. Taking this issue unless @aabhinavg states that they are still interested in working on it.

@rmhowe425
Copy link
Contributor

take

@asishm
Copy link
Contributor

asishm commented Jun 2, 2024

looks like similar issue as #45903 (but that may be looking at None -> Boolean whereas this is True -> boolean). If so, the PR #46374 was closed due to going stale, which I feel was close. @rmhowe425

The main issue seems to be that pd.BooleanArray._from_sequence_of_strings is getting called with values that are not strings.

@asishm
Copy link
Contributor

asishm commented Jun 2, 2024

Also @yuanx749's comment #45903 (comment) of using dtype_backed='numpy_nullable' is a workaround, but it seems to only after the dtype evaluation if defined in the read_excel call.

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug IO Excel read_excel, to_excel NA - MaskedArrays Related to pd.NA and nullable extension arrays
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants