Skip to content

WIP/ENH: negative and large datetimes #15148

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

Closed

Conversation

jklymak
Copy link
Member

@jklymak jklymak commented Aug 28, 2019

PR Summary

This is a WIP, but for discussion as to whether this is worth the extra code.

It is easy to make negative datetime64 objects, or large datetime64 objects that are greater than 10000 years, whereas right now we restrict to between year 1 and 9999 because we use datetime.datetime.

The obvious solution is to use datetime64, but that falls down because a) we want to be able to deal with timezones, and datetime64 doesn't by default. b) we want to use dateutil.rrule for things like "put a tick every other Wednesday".

So, the solution proposed here is to derive a subclass of datetime that keeps track of more years than 1-9999 (I'm not actually clear why datetime restricts to these values in the first place). I've attempted (probably poorly) to make the subclass as minimal as possible, and to call the super as often as possible.

import matplotlib.pyplot as plt
import numpy as np

fig, ax = plt.subplots()
dates = np.arange('-20000-01-01', '-20000-02-01', 10, dtype='datetime64[h]')
y = np.arange(len(dates))
ax.plot(dates, y, 'd')
plt.show()

Before

ValueError: view limit minimum -7305216.541666667 is less than 1 and is an invalid Matplotlib date value. This often happens if you pass a non-datetime value to an axis that has datetime units

This PR:

CDF

  • Make tests with this feature.
  • write up docs....

PR Checklist

  • Has Pytest style unit tests
  • Code is Flake 8 compliant
  • New features are documented, with examples if plot related
  • Documentation is sphinx and numpydoc compliant
  • Added an entry to doc/users/next_whats_new/ if major new feature (follow instructions in README.rst there)
  • Documented in doc/api/api_changes.rst if API changed in a backward-incompatible way

if not isinstance(vmax, _datetimey):
vmax = _datetimey._datetime_to_datetimey(vmax, 0)
vmind, vmaxd = _datetimey._datetimey_to_datetime_samey0(vmin, vmax)
self.rule.set(dtstart=vmind, until=vmaxd)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that in the old code dstart=vmin-(vmax-vmin) for some reason, and dstop = vmax + (vmax - vmin). Expanding the range by a factor of three doesn't make much sense to me. The test that changed (test_DateFormatter), in my opinion, changed for the better. So this is a mildly breaking change, but it gives far more consistent results.

@@ -206,6 +207,151 @@ def _get_rc_timezone():
MO, TU, WE, TH, FR, SA, SU)
WEEKDAYS = (MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY)

class _datetimey(datetime.datetime):
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

my guess is that this whole class should go in its own file...

@timhoffm
Copy link
Member

timhoffm commented May 4, 2020

Is this still relevant, now that we can set the epoch?

@jklymak
Copy link
Member Author

jklymak commented May 4, 2020

Yeah, because it works around datetime's issues with years > 9999 and less than 0. datetime64 has no issues with those (other than typical calendar craziness) so it would be nice to do something. But was waiting for the epoch stuff to shake out first.

@jklymak jklymak modified the milestones: v3.3.0, v3.4.0 May 4, 2020
@jklymak jklymak closed this Dec 19, 2020
@QuLogic QuLogic removed this from the v3.4.0 milestone Mar 16, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants