Skip to content
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

Tooltip and axis labels are not displaying correct date for st.altair_chart #4342

Closed
gcamargo1 opened this issue Jan 31, 2022 · 7 comments · Fixed by #8278
Closed

Tooltip and axis labels are not displaying correct date for st.altair_chart #4342

gcamargo1 opened this issue Jan 31, 2022 · 7 comments · Fixed by #8278
Assignees
Labels
feature:st.altair_chart priority:P2 status:confirmed Bug has been confirmed by the Streamlit team type:bug Something isn't working

Comments

@gcamargo1
Copy link
Contributor

Summary

When plotting an Altair plot with date in tooltip, display is incorrect

Steps to reproduce

Code snippet:

import pandas as pd
import altair as alt
import streamlit as st

df = pd.DataFrame(
    {
        "value": [2, 4, 8, 0],
        "date_str": ["2015-06-01", "2015-06-02", "2015-06-03", "2015-06-04"],
        "date_datetime": ["2015-06-01", "2015-06-02", "2015-06-03", "2015-06-04"],
    },
)
df["date_datetime"] = pd.to_datetime(df["date_datetime"])

chart = (
    alt.Chart(df)
    .mark_line()
    .encode(
        x="date_datetime:T",
        y="value:Q",
        tooltip=["date_str", "date_datetime:T", "value:Q"],
    )
)
st.altair_chart(altair_chart=chart, use_container_width=True)

Expected behavior:

I expected to see June 4th 2015 and I saw June 3rd 2015.

image

Actual behavior:

Please see above

Is this a regression?

don't know.

Debug info

  • Streamlit version: 1.5.0
  • Python version: 3.9.4
  • Using Conda? PipEnv? PyEnv? Pex? - No
  • OS version: MacOS bigsur
  • Browser version: chrome 96.0.4664.55

Additional information

I looked into already published issues and I could not find anything related to this. Thank you.

@gcamargo1 gcamargo1 added type:bug Something isn't working status:needs-triage Has not been triaged by the Streamlit team labels Jan 31, 2022
@arcanaxion
Copy link

I'm facing a similar issue. Try changing your tooltip line to tooltip=["date_str", alt.Tooltip("date_datetime:T", format="%Y-%m-%dT%H:%M:%S"), "value:Q"] to see the time.

Say your machine is in UTC-5, you'll probably find that where date_str is "2015-06-04", date_datetime was probably "2015-06-03T19:00:00". In my case, since my machine is on UTC+8, I'm seeing it as "2015-06-04T08:00:00".

It seems like Streamlit is converting the datetimes to the timezone on the local machine. If no tz info is specified for the datetime given, it assumes UTC. From what I've tried, this only seems to be happening to Altair charts in Streamlit. It does not happen to Altair on its own, and it does not happen to st.line_chart.

@LukasMasuch
Copy link
Collaborator

Thanks for reporting this issue! As suggested by @arcanaxion, the problem is indeed related to how altair processes date timezones. One fix from our side might be to always set the scale to utc for datetime objects (as we do with date objects already). But I'm not sure yet if there are any other negative sideffects if we do this.

@LukasMasuch LukasMasuch added feature:charts Related to charting functionality status:confirmed Bug has been confirmed by the Streamlit team priority:P3 Champions 💖 and removed status:needs-triage Has not been triaged by the Streamlit team labels Mar 5, 2022
@kmcgrady kmcgrady added feature:st.altair_chart and removed feature:charts Related to charting functionality labels Mar 7, 2022
@kmcgrady
Copy link
Collaborator

This seems very related to vega/altair#2540 The solution would be to put the dates into the ISO-8601 format.

@sfc-gh-jcarroll
Copy link
Collaborator

I'm bumping this up to a P2 since it also impacts the axis labels (as seen in the screenshot above) and we heard about it from a few places recently. I don't think the provided workaround would fix the axis labels, just the tooltip.

I don't have edit access on the issue, but would be nice if someone could update the title and summary to reflect that it's not just the tooltip impacted.

@LukasMasuch LukasMasuch changed the title altair_chart tooltip not displaying correct date Tooltip and axis labels are not displaying correct date for st.altair_chart Aug 22, 2023
@selwyth
Copy link

selwyth commented Oct 9, 2023

Confirming that I experience this issue in the U.S. when using Streamlit, and not in Europe when using Streamlit.

It works everywhere in Jupyter notebook (without the st.altair_chart of course).

I suggest for the title to be "Tooltip and axis labels are not displaying correct for st.altair_chart when the end-user is in UTC -X timezones". 😄

@jmberros
Copy link

Still experiencing this as of Feb 2024, not a nice bug. I'm changing all tooltips to use a stringified version of the date meanwhile. :/

@kuza55
Copy link

kuza55 commented Mar 13, 2024

Running into the issue as well.

It seems like streamlit needs to set the scale property on the Tooltip as well as on the axis markers as per vega/altair#2077 (comment)

alt.Tooltip(name, scale={'type': 'utc'})

Though apparently Altair does not support this Vega feature, so this probably needs a fix in Altair first...

kmcgrady added a commit that referenced this issue Apr 2, 2024
## Describe your changes

When Streamlit apps work with dates/datetime, Vega can show the
incorrect date on the graphs. This is because datetimes are serialized
as milliseconds since epoch and are therefore assumed in UTC format.
Vega's engine takes in this timestamp and produces a Date that is set to
the local timezone, which can create some incorrect values. There's also
no way to determined from the serialized shape what timezone it is in,
so we need to offset the time for vega.

NOTE: We do this a lot in formatting for tables, but this is specific to
Vega's engine, so we isolate the change there.

## GitHub Issue Link (if applicable)
Closes #4342 

## Testing Plan

- JS Unit Tests are included and updated to test DATE, DATETIME, and
DATETIMETZ formats.
- Checking rendering of E2E Tests for the tooltip (like in the issue) is
really difficult. Manual testing of this is possible with PR Preview.

---

**Contribution License Agreement**

By submitting this pull request you agree that all contributions to this
project are made under the Apache 2.0 license.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature:st.altair_chart priority:P2 status:confirmed Bug has been confirmed by the Streamlit team type:bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

8 participants