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

ENH: Allow safe access to .book in ExcelWriter #45687

Merged
merged 8 commits into from
Feb 1, 2022

Conversation

rhshadrach
Copy link
Member

@rhshadrach rhshadrach commented Jan 28, 2022

Towards #45572

  • Ensure all linting tests pass, see here for how to run them

The plan to expose both sheets and book as public attributes of ExcelWriter. We need to make sure that sheets is correct if the user modifies the state via accessing book.

This accesses a protected attribute for xlwt; I looked at the xlwt code and there is no public access to determine the sheets. The xlwt repository is read-only, we only support the most recent version, and xlwt will be removed in pandas 2.0. So I think this is okay.

@rhshadrach rhshadrach added IO Excel read_excel, to_excel Enhancement labels Jan 28, 2022
@phofl
Copy link
Member

phofl commented Jan 29, 2022

Failures look related?

@rhshadrach
Copy link
Member Author

Thanks @phofl - wasn't using a context manager for ExcelWriter

@rhshadrach
Copy link
Member Author

Failure is unrelated.

FAILED pandas/tests/computation/test_eval.py::TestEval::test_unary_in_array[python-python]
FAILED pandas/tests/computation/test_eval.py::TestEval::test_unary_in_array[python-pandas]
FAILED pandas/tests/computation/test_eval.py::TestOperations::test_column_in[python-python]
FAILED pandas/tests/computation/test_eval.py::TestOperations::test_column_in[python-pandas]

Copy link
Member

@phofl phofl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Small comments

@phofl
Copy link
Member

phofl commented Jan 30, 2022

You did not add a test for xlsxwriter, was this on purpose?

@rhshadrach
Copy link
Member Author

You did not add a test for xlsxwriter, was this on purpose?

Yes, but I realize now that was a mistake. I removed the sheets attribute from the writer for xlsxwriter because it was never used (hence, no tests). But that creates an inconsistency between the writers, so I've added it back on.

Copy link
Contributor

@jreback jreback left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you add a whatsnew
can you add to the api doc reference
these also need a doc-string (the properties) with a versionadded 1.5

@@ -58,6 +58,16 @@ def __init__(
self.book = OpenDocumentSpreadsheet(**engine_kwargs)
self._style_dict: dict[str, str] = {}

@property
def sheets(self) -> dict[str, Any]:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is there a way to type these more specifically?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not easily, as far as I can tell. I plan to look into this more in the future.

Regarding whatsnew - this is not user facing. Currently in docs no attribute of ExcelWriter is public. I'd like to refactor ExcelWriter (namely, making protected attributes start with _) before updating docs.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

got it totally fine (make issues if you cannot to get to it soon), otherwise PR's totally fine.

is there a way to type these more specifically?

can we use the base class generic for typing? (again nbd)

@rhshadrach
Copy link
Member Author

@jreback - added docstrings. I'd like to hold off updating the docs until the API is ready to be public.

@jreback jreback added this to the 1.5 milestone Feb 1, 2022
Copy link
Contributor

@jreback jreback left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

comments for followup

if sheet_name in self.sheets:
wks = self.sheets[sheet_name]
else:
wks = self.book.get_worksheet_by_name(sheet_name)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i would change .book to be a private attribute ._book and then expose the .book property (followups for sure)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would the property be something besides just return self._book? In other words, why a property?

@jreback jreback merged commit f0c6b59 into pandas-dev:main Feb 1, 2022
@rhshadrach rhshadrach deleted the sheets_property branch February 5, 2022 20:45
phofl pushed a commit to phofl/pandas that referenced this pull request Feb 14, 2022
yehoshuadimarsky pushed a commit to yehoshuadimarsky/pandas that referenced this pull request Jul 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement IO Excel read_excel, to_excel
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants