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

feat(python): Support writing to file objects from write_excel #20638

Merged
merged 2 commits into from
Jan 10, 2025

Conversation

alexander-beedie
Copy link
Collaborator

@alexander-beedie alexander-beedie commented Jan 9, 2025

Closes #18849.

Allows write_excel to export to file objects as well as file paths (and BytesIO).

Example

from pathlib import Path
import polars as pl

df1 = pl.DataFrame({"colx": ["aaa", "bbb", "ccc"]})

# write to an open (binary-mode) file object
with Path("dataframe.xlsx").open(mode="wb") as p:
    df1.write_excel(p)

df2 = pl.read_excel(Path("dataframe.xlsx"))
df1.equals(df2)
# True

@github-actions github-actions bot added enhancement New feature or an improvement of an existing feature python Related to Python Polars labels Jan 9, 2025
@alexander-beedie alexander-beedie added the A-io-spreadsheet Area: reading/writing Excel/ODS files label Jan 9, 2025
Copy link

codecov bot commented Jan 9, 2025

Codecov Report

Attention: Patch coverage is 77.77778% with 2 lines in your changes missing coverage. Please review.

Project coverage is 79.04%. Comparing base (247f0b1) to head (e261006).
Report is 11 commits behind head on main.

Files with missing lines Patch % Lines
py-polars/polars/io/spreadsheet/_write_utils.py 77.77% 1 Missing and 1 partial ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##             main   #20638   +/-   ##
=======================================
  Coverage   79.03%   79.04%           
=======================================
  Files        1557     1557           
  Lines      220528   220545   +17     
  Branches     2510     2516    +6     
=======================================
+ Hits       174303   174324   +21     
+ Misses      45651    45646    -5     
- Partials      574      575    +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@ritchie46 ritchie46 merged commit 8abd261 into pola-rs:main Jan 10, 2025
15 checks passed
@alexander-beedie alexander-beedie deleted the excel-write-file-objects branch January 10, 2025 08:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-io-spreadsheet Area: reading/writing Excel/ODS files enhancement New feature or an improvement of an existing feature python Related to Python Polars
Projects
None yet
Development

Successfully merging this pull request may close these issues.

df.write_excel does not work with file objects
2 participants