Skip to content

ENH: to_excel: warning/conversion of text values starting with '='  #59790

@mhooreman

Description

@mhooreman

Feature Type

  • Adding new functionality to pandas

  • Changing existing functionality in pandas

  • Removing existing functionality in pandas

Problem Description

Excel interprets text values starting with '=' as formula.
It can happen that values in a pandas Series (col of data frame) might start with '=' (for example '=== Attention ===').
If this is faced by excel:

  • In case of valid formula, it is interpreted
  • In case of invalid formula, the file is corrupted and the values are null after attempt to fix
    We should handle this better

Feature Description

If the value starts with '=', issue a warning and add a single quote prefix to the value:
Before: === Attention ===
After: '=== Attention ==='

Alternative Solutions

For columns with object dtypes, or categories with underlying object dtype, for values which are strings, force the excel format as text.

Additional Context

  • The proposed ' prefix is not 100% correct: when we prefix any text with a ' in excel, it is interpreted as text. Unfortunatety, the proposal will show the ' (see example code below)
  • It might be problematic if people wants to clearly use formula, but I don't think that's really meaningful. Maybe an option to avoid that conversion (but issue the warning anyway) can be a good option
  • Example code:
import pandas as pd

df = pd.DataFrame([
    "foo",
    "bar",
    "baz",
    "=== Attention ===",  # Problem
    "'===Attention ==="   # OK but the single quote will be displayed
])

df.to_excel('demo.xlsx')

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions