How to Properly Format Dates in Streamlit and Pandas for Accurate Display

Working with Dates in Streamlit and Pandas

In this article, we will explore how to work with dates in Streamlit and Pandas. Specifically, we’ll delve into the challenges of formatting dates when working with these two popular libraries.

Understanding Date Formats

Before we dive into the code, let’s first understand how dates are represented in different formats. In Python, dates can be represented as strings or as datetime objects. When working with dates, it’s essential to choose a format that suits your needs.

In the question you posed on Stack Overflow, the user was using pd.to_datetime to convert date columns into datetime format, but they were seeing unexpected results when printing the dataframe in the console. Let’s take a closer look at what’s happening here.

The pd.to_datetime Function

When we use pd.to_datetime, it takes a series of values and converts them into datetime objects based on their format. By default, pd.to_datetime assumes that the dates are in ISO 8601 format, which is YYYY-MM-DDTHH:MM:SS.SSSZ.

In your case, the original Datetime column had the following format:

'2023-05-11T18:00:00.000+01:00'

When you applied pd.to_datetime, it converted this value into a datetime object with the UTC timezone:

pd.to_datetime('2023-05-11T18:00:00.000+01:00')

However, when you used dt.tz_convert("Europe/London"), it changed the timezone of the datetime object to Europe/London.

The Challenge of Formatting Dates

When printing the dataframe in the console, the dates were displayed in a different format than expected. This was due to the way Streamlit handles dates and formatting.

In your original code, you applied dt.strftime('%d %b %y') to the “Date” column:

self.df["Test"] = pd.to_datetime(self.df["Date"], format='%Y-%m-%d').dt.strftime('%d %b %y')

However, this formatting function is not correctly handling the timezone information. When pd.to_datetime converts a date to datetime format, it also takes into account the timezone. In your case, the dates were in UTC, but when you applied dt.tz_convert("Europe/London"), they became London time.

The Solution: Separating Date and Time

To solve this problem, we need to separate the date from the time component. When applying dt.strftime to a datetime object, we’re essentially formatting only the date part.

In your original code, you were making both “Date” and “Test” columns show the same thing. If you expected them to show different values, you could do it like this:

def _get_date(df):
    df["Date"] = (
        pd.to_datetime(df["Datetime"], utc=True).dt.tz_convert("Europe/London").dt.date
    )
    df["Test"] = pd.to_datetime(df["Date"], format="%Y-%m-%d").dt.strftime("%d %b %y")
    df["Date"] = df["Date"].astype(str)

In this version, the “Date” column shows the format 2023-05-11, and the “Test” column shows 11 May 23.

The Importance of Timezone Information

When working with dates in Streamlit and Pandas, it’s essential to consider timezone information. The correct formatting function depends on whether you’re dealing with UTC or local time.

In the answer provided, the solution separated the date from the time component using dt.strftime. This ensures that the formatted dates are correctly displayed in the console without considering the timezone.

Conclusion

Working with dates in Streamlit and Pandas requires careful consideration of formatting options. When dealing with datetime objects, it’s essential to separate the date from the time component to ensure accurate formatting.

By understanding how pd.to_datetime works and applying the correct formatting function, you can achieve consistent results when working with dates in these libraries. Remember to always consider timezone information when converting between different formats.


Last modified on 2025-02-23