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

timestamp() error on Windows with dates before 1970 #5

Open
Biggels opened this issue Jul 14, 2020 · 4 comments
Open

timestamp() error on Windows with dates before 1970 #5

Biggels opened this issue Jul 14, 2020 · 4 comments

Comments

@Biggels
Copy link

Biggels commented Jul 14, 2020

Hi there,

I'm working through your NBA example, and I'm noticing that anywhere where we're trying to use .timestamp(), e.g.

  • t = datetime.strptime(row["date"], "%Y-%m-%d").timestamp()
  • or when setting timestamps=True in the plotting (ts = [datetime.fromtimestamp(t) for t in ts])

it throws an error when encountering dates before 1970, as below:
OSError: [Errno 22] Invalid argument

This doesn't happen in the online notebook, and from some googling it sounds like it's a Windows specific issue. I was able to work around it in the main section by simply manually calculating the timedelta and converting it to seconds (t = (datetime.strptime(row["date"], "%Y-%m-%d") - datetime(1970, 1, 1)).total_seconds()), but that doesn't help me with the plotting, unfortunately.

Do you have any suggested workarounds or fixes? Let me know if any more info is needed.

Edit: To clarify, I'm running Python 3.8 in Spyder 4.1.1 on Windows

@spidy0x0
Copy link

i hope this helps,similar problem
https://stackoverflow.com/q/54470408/10346275

also,set your date back by 1 year and try again

@lucasmaystre
Copy link
Owner

Hi @Biggels , apologies for the delay (and thank you @spidyhackx for helping out.)

Thanks for reporting. I don't have access to a Windows machine, so it is difficult for me to debug this. I'm glad you were able to work around it in the main notebook.

For the plotting: an easy fix would be to call plot_scores with timestamps=False. This function returns fig, ax, so you could always use ax.set_xticklabels() as needed to create a pretty plot.

Would the following change in plot_scores fix things on windows?

ts = [datetime(1970, 1, 1) + t for t in ts]

@Biggels
Copy link
Author

Biggels commented Jul 27, 2020

Thanks for the tip on setting ticklabels manually. Seems obvious in hindsight :)

As for your suggested change, I don't believe you can add a float directly to a datetime object. From some testing, it looks like you have to convert the float time in seconds to a timedelta object first. The following snippet, for example, worked for me to convert all the float times in my dictionary of observations to datetime objects:

from datetime import datetime, timedelta
ts = [observation['t'] for observation in observations]
ts = [datetime(1970, 1, 1) + timedelta(seconds=t) for t in ts]

@lucasmaystre
Copy link
Owner

Ah, yes, makes sense. OK, I'll change this in the plotting function 👍

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