Skip to content

BUG: When using to_excel() columns with Date Format are converting in Excel to 'General Format.  #59882

Open
@ArunPesari2

Description

@ArunPesari2

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
from openpyxl import load_workbook
from openpyxl.styles import NamedStyle

# Create a sample DataFrame with dates in 'mm-dd-yyyy' format
data = {
    "date": ["12-31-2023", "01-01-2024", "05-23-2024"]
}
df = pd.DataFrame(data)

# Convert the 'date' column from 'mm-dd-yyyy' to datetime format
df['date'] = pd.to_datetime(df['date'], format='%m-%d-%Y')



# Convert the datetime objects back to strings in 'dd-mm-yyyy' format
df['date'] = df['date'].dt.strftime('%d-%m-%Y')
df['date'] = pd.to_datetime(df['date'], format='%d-%m-%Y')



# Save the DataFrame to an Excel file using openpyxl as the engine
df.to_excel('formatted_dates_pandas.xlsx', index=False, engine='openpyxl')

# Load the Excel file with openpyxl
workbook = load_workbook('formatted_dates_pandas.xlsx')
worksheet = workbook.active

# Define a date format style for 'DD-MM-YYYY'
date_style = NamedStyle(name='date_style', number_format='DD-MM-YYYY')



for row in range(2, len(df) + 2):  # Skip header row, start from row 2
    worksheet.cell(row=row, column=1).value = df['date'].iloc[row - 2]  # Write the string directly

# Save the workbook
workbook.save('formatted_dates_pandas.xlsx')

Issue Description

i have a date column in pandas, when i write to an excel , i am loosing the format in excel.
In excel it is seen as General Format. If this a bug, pls fix it or revert back with an approach.

Expected Behavior

In Excel we should not loose format of date (dd-mm-YYYY).

Installed Versions

INSTALLED VERSIONS

commit : 0691c5c
python : 3.11.4
python-bits : 64
OS : Windows
OS-release : 10
Version : 10.0.22621
machine : AMD64
processor : Intel64 Family 6 Model 154 Stepping 3, GenuineIntel
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : English_India.1252

pandas : 2.2.3
numpy : 2.1.1
pytz : 2024.2
dateutil : 2.9.0.post0
pip : 23.1.2
Cython : None
sphinx : None
IPython : None
adbc-driver-postgresql: None
adbc-driver-sqlite : None
bs4 : None
blosc : None
bottleneck : None
dataframe-api-compat : None
fastparquet : None
fsspec : None
html5lib : None
hypothesis : None
gcsfs : None
jinja2 : None
lxml.etree : None
matplotlib : None
numba : None
numexpr : None
odfpy : None
openpyxl : 3.1.5
pandas_gbq : None
psycopg2 : None
pymysql : None
pyarrow : 17.0.0
pyreadstat : None
pytest : None
python-calamine : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlsxwriter : 3.2.0
zstandard : None
tzdata : 2024.2
qtpy : None
pyqt5 : None

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugIO Excelread_excel, to_excelNeeds InfoClarification about behavior needed to assess issue

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions