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

Date x-axis is shifted by -1 day #2540

Closed
topspinj opened this issue Dec 21, 2021 · 11 comments
Closed

Date x-axis is shifted by -1 day #2540

topspinj opened this issue Dec 21, 2021 · 11 comments

Comments

@topspinj
Copy link

I've been using Altair within Streamlit and have been enjoying it for the most part. However, I noticed that Altair behaves oddly when I create plots where the x-axis is a date field.

Screen Shot 2021-12-21 at 2 12 41 PM

the min date is Sept 1, 2021 but when I hover over the Altair chart, it says Aug 31, 2021 🤔

I haven't reproduced this in Jupyter notebooks yet but I do see this strange behaviour consistently in the context of Streamlit. Anyone else experience this issue?

@topspinj topspinj added the bug label Dec 21, 2021
@jakevdp
Copy link
Collaborator

jakevdp commented Dec 21, 2021

This is a timezone/date parsing issue in Javascript. You need your date strings to be fully-qualified ISO 8601 date strings, or Javascript will parse them as UTC rather than local time. If you use a pandas dataframe and ensure that your column is a datetime type, Altair will take care of the details for you. See e.g. vega/vega-lite#6883 for details.

@jakevdp jakevdp closed this as completed Dec 21, 2021
@topspinj
Copy link
Author

Thanks for the swift response. I'm really glad you shared this issue with me because I wouldn't have found it otherwise. Do you think this is worth adding to the docs?

@jakevdp
Copy link
Collaborator

jakevdp commented Dec 21, 2021

Yeah, it might be worth adding this in a FAQ or a "gotchas" page.

@jakevdp
Copy link
Collaborator

jakevdp commented Dec 21, 2021

Here's a similar issue in the Altair repo: #2077

@topspinj
Copy link
Author

I tried casting the column to datetime and set utc=True. Still getting that weird -1 day behaviour in the plots.

pd.to_datetime(df["date"], utc=True)

Am I doing this right? I'm not familiar with ISO8601 strings so let me know if I'm doing this wrong.

@jakevdp
Copy link
Collaborator

jakevdp commented Dec 21, 2021

You probably don't want utc=True. If you do, then you should use UTC timeunits in your chart as well.

(also, just in case this is an area of confusion: pd.to_datetime does not do any in-place modification: you have to assign the result to something; e.g. df["date"] = pd.to_datetime(df["date"]))

@joelostblom
Copy link
Contributor

I encountered a similar off by one issue on SO the other day and I think that one was timezone related and will be fixed when this vegalite issue is closed vega/vega-lite#7795

@topspinj
Copy link
Author

Thanks for sharing this! looks like this PR is still waiting for reviewers. Unfortunately I don't have the context to review this particular PR but hopefully it gets shipped soon :shipit:

Any work-arounds that you've used in the meantime @joelostblom?

@joelostblom
Copy link
Contributor

I'm not sure on the top of my head, but you could try writing your dates as "2011-10-02T00:00:00" as Jake mentions here vega/vega-lite#6883 (comment) (and maybe play with including time zone info or not) or setting the axis values explicitly as mentioned here #2077

@joelostblom
Copy link
Contributor

@topspinj I just posted a possible workaround in this SO answer that you can try https://stackoverflow.com/a/71685940/2166823

@joelostblom
Copy link
Contributor

The corresponding Vega-Lite issues have now been merged and this appears fixed to me when trying with the SO example I linked above, so closing this. Please reopen with a spec that is not working in altair 5 if there are still issues.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants