You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
importpandasaspdfromopenpyxlimportload_workbookfromopenpyxl.stylesimportNamedStyle# Create a sample DataFrame with dates in 'mm-dd-yyyy' formatdata= {
"date": ["12-31-2023", "01-01-2024", "05-23-2024"]
}
df=pd.DataFrame(data)
# Convert the 'date' column from 'mm-dd-yyyy' to datetime formatdf['date'] =pd.to_datetime(df['date'], format='%m-%d-%Y')
# Convert the datetime objects back to strings in 'dd-mm-yyyy' formatdf['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 enginedf.to_excel('formatted_dates_pandas.xlsx', index=False, engine='openpyxl')
# Load the Excel file with openpyxlworkbook=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')
forrowinrange(2, len(df) +2): # Skip header row, start from row 2worksheet.cell(row=row, column=1).value=df['date'].iloc[row-2] # Write the string directly# Save the workbookworkbook.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
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
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
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?
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)
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
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
The text was updated successfully, but these errors were encountered: