1

No matter what I have tried, StyleFrame seems to insist on formatting all dates in DD/MM/YYYY format. I am trying to get them to format as MM/DD/YYYY.

I am trying to control this using number_format but it appears to be entirely ignored in the resulting Excel file. I have been able to apply many other kinds of styles successfully such as font size, text alignment, and column width but it seems to ignore number_format.

Specifically, I am trying to do this:

sf = StyleFrame(exampleDataFrame)
sf.apply_column_style(cols_to_style=['Start Date', 'End Date'],
                  width=35, styler_obj=Styler(number_format='MM/DD/YYYY', font_size=10))

The width and the font size are applied as expected but not the date format.

emersm2
  • 11
  • 1

1 Answers1

0

StyleFrame seems to insist on formatting all dates in DD/MM/YYYY

This is correct, and is due to a bit of an oversight in the design of the styling process.

Until a formal fix, a working workaround is to override the underlying default style for date (or datetime) objects. Unfortunately this does not provide control over specific columns and must be done before the creation of the StyleFrame object.

I'll create a github issue for this so it will be looked into in the future.

import pandas as pd
from styleframe import StyleFrame, utils

df = pd.DataFrame({'a': ['11/12/2020']})
df['a'] = pd.to_datetime(df['a'])

orig_date_time_format = utils.number_formats.default_date_time_format
utils.number_formats.default_date_time_format = 'YYYYMMDD'
sf = StyleFrame(df)
sf.to_excel('test.xlsx').save()
utils.number_formats.default_date_time_format = orig_date_time_format

This will result with 20201112 in the cell.

DeepSpace
  • 78,697
  • 11
  • 109
  • 154
  • Thank you for the reply. Your solution as stated does work. However, it seems that if I then try to apply any additional styling after the fact, it reverts back to the undesired format. For example: `exampleFrame['Start Date'] = pd.to_datetime(exampleFrame['Start Date']) utils.number_formats.default_date_time_format = 'MM/DD/YYYY' sf = StyleFrame(exampleFrame) sf.apply_column_style(cols_to_style=columns, width=25, styler_obj=Styler(font_size=10)) sf.to_excel(fileName).save()` The apply_column_style line seems to cause it to revert. – emersm2 Dec 11 '20 at 14:35