Skip to content

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

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

Open
3 tasks done
ArunPesari2 opened this issue Sep 24, 2024 · 3 comments
Labels
Bug IO Excel read_excel, to_excel Needs Info Clarification about behavior needed to assess issue

Comments

@ArunPesari2
Copy link

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

@ArunPesari2 ArunPesari2 added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Sep 24, 2024
@ArunPesari2 ArunPesari2 changed the title BUG: When using to_excel() coolumns with Date Format are conversing in Excel to 'General Format. BUG: When using to_excel() columns with Date Format are converting in Excel to 'General Format. Sep 24, 2024
@asishm
Copy link
Contributor

asishm commented Sep 24, 2024

Thanks for the report.

Pandas to_excel method using a path-like/file-like value doesn't allow setting date/datetime formats when generating the file, but pd.ExcelWriter does. Can you try with it (you may have to specify engine='xlsxwriter' as there is an open issue with the openpyxl engine not using those parameters #44284)

Note: you can pass in an ExcelWriter to df.to_excel

@asishm asishm added the IO Excel read_excel, to_excel label Sep 24, 2024
@rhshadrach rhshadrach added Needs Info Clarification about behavior needed to assess issue and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Sep 30, 2024
@saldanhad
Copy link
Contributor

I checked this using pd.ExcelWriter, and the functionality works as expected with the XlsxWriter engine. Do we want to keep this issue open, or would you prefer to close it since there's a working solution?

@dlleigh
Copy link

dlleigh commented Jan 23, 2025

I agree that it works with xlsxwriter, however that engine does not support "append mode" (e.g. mode='a'). So currently there is no support for custom date/datetime formats when the use case involves adding a new sheet to an existing file.

The only workaround in this case is to set private attributes on the openpyxl writer as described in #44284 (comment)

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 Needs Info Clarification about behavior needed to assess issue
Projects
None yet
Development

No branches or pull requests

5 participants