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

scale_x_datetime(): error building plot for early dates #346

Closed
ASmirnov-HORIS opened this issue Mar 5, 2021 · 4 comments
Closed

scale_x_datetime(): error building plot for early dates #346

ASmirnov-HORIS opened this issue Mar 5, 2021 · 4 comments
Milestone

Comments

@ASmirnov-HORIS
Copy link
Collaborator

ASmirnov-HORIS commented Mar 5, 2021

Look at the following example:

from datetime import datetime

import pandas as pd

from lets_plot import *
LetsPlot.setup_html()

df = pd.DataFrame(
    {
        'd': pd.to_datetime(
            [
                datetime(1972, 6, 9), 
                datetime(1972, 6, 10), 
                datetime(2021, 3, 5)
            ]
        ), 
        'x': [0, 1, 2]
    }
)

ggplot(df, aes('d', 'x')) + geom_point() + scale_x_datetime()

Here we get:

If we remove the scale, it works:

If we remove the earliest date, it works again:

@ASmirnov-HORIS ASmirnov-HORIS changed the title scale_x_datetime(): error building plot for pre-1972 dates scale_x_datetime(): error building plot for early dates Mar 5, 2021
@alshan alshan added this to the None milestone Dec 28, 2021
@aeturrell
Copy link

I'm having the same issue with 3.2.0. REPREX:

First the setup:

from lets_plot import *
import pandas as pd

presidential = pd.read_csv("https://vincentarelbundock.github.io/Rdatasets/csv/ggplot2/presidential.csv", index_col=0)
presidential = presidential.astype({"start": "datetime64[ns]", "end": "datetime64[ns]"})
presidential["id"] = 33 + presidential.index

Note that the data are:

name start end party id
1 Eisenhower 1953-01-20 00:00:00 1961-01-20 00:00:00 Republican 34
2 Kennedy 1961-01-20 00:00:00 1963-11-22 00:00:00 Democratic 35
3 Johnson 1963-11-22 00:00:00 1969-01-20 00:00:00 Democratic 36
4 Nixon 1969-01-20 00:00:00 1974-08-09 00:00:00 Republican 37
5 Ford 1974-08-09 00:00:00 1977-01-20 00:00:00 Republican 38
6 Carter 1977-01-20 00:00:00 1981-01-20 00:00:00 Democratic 39
7 Reagan 1981-01-20 00:00:00 1989-01-20 00:00:00 Republican 40
8 Bush 1989-01-20 00:00:00 1993-01-20 00:00:00 Republican 41
9 Clinton 1993-01-20 00:00:00 2001-01-20 00:00:00 Democratic 42
10 Bush 2001-01-20 00:00:00 2009-01-20 00:00:00 Republican 43
11 Obama 2009-01-20 00:00:00 2017-01-20 00:00:00 Democratic 44
12 Trump 2017-01-20 00:00:00 2021-01-20 00:00:00 Republican 45

Now the following code, with only the last few entries (shorter time span) included, works:

(
    ggplot(presidential.iloc[-5:], aes(x="start", y="id")) +
    geom_point() +
    geom_segment(aes(xend="end", yend="id")) +
    scale_x_datetime(breaks=presidential["start"], format="%Y")
)

But the full dataset,

(
    ggplot(presidential, aes(x="start", y="id")) +
    geom_point() +
    geom_segment(aes(xend="end", yend="id")) +
    scale_x_datetime(breaks=presidential["start"], format="%Y")
)

Produces an error:

Internal error: IllegalArgumentException : <no message>

@alshan
Copy link
Collaborator

alshan commented Aug 14, 2023

Unfortunately, at the moment the earliest date scale datetime can handle is 1.1.1970

@aeturrell, a relatively easy workaround in your case would be to extract year from dates and add "year" as a numeric column to the dataset.
Then you can use regular x/y scales and format year-values using integer formatting "%d".

@alshan
Copy link
Collaborator

alshan commented Aug 16, 2023

integer formatting is just "d", sorry.

@alshan alshan modified the milestones: Backlog, 2023Q3 Aug 21, 2023
@aeturrell
Copy link

Woop! Thanks so much!

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

No branches or pull requests

3 participants