FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

ENH: support np.datenum64 in dates.py by jklymak · Pull Request #9779 · matplotlib/matplotlib · GitHub

Repository navigation

ENH: support np.datenum64 in dates.py - #9779

Merged
Kojoley merged 1 commit into
matplotlib:masterfrom
jklymak:add-datenum64
Nov 15, 2017
Merged

Kojoley merged 1 commit into
matplotlib:masterfrom
jklymak:add-datenum64

Conversation

jklymak commented Nov 14, 2017 •
edited
Loading

Copy link
Copy Markdown
Member

PR Summary

This allows numpy.datetime64 dates. New and improved!

time = [base - datetime.timedelta(days=x) + datetime.timedelta(seconds=0.900001) for x in range(0, 3)]

timenp = np.array(time, dtype='datetime64[us]')

fig, ax = plt.subplots(tight_layout=True)
ax.plot( time, data)
ax.plot( timenp, data+.1, )

works fine.

PR Checklist

  • Has Pytest style unit tests
  • Code is PEP 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

dstansby added this to the v2.2 milestone Nov 14, 2017
Comment thread lib/matplotlib/dates.py Outdated


def _to_ordinalf(dt):
def _to_ordinalf(dts):

dstansby Nov 14, 2017 •
edited
Loading

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

I think it's probably worth writing a new function called something like _dtime64_to_ordinalf(dts) with the new code to handle np.datetime64s. (for clarity)

Copy link
Copy Markdown
Member

xref #9610

efiring commented Nov 14, 2017

Copy link
Copy Markdown
Member

The normal use case would be with an ndarray of dtype np.datetime64. The conversion of this to matplotlib float datenums should be direct and vectorized--no loop--otherwise the performance will be terrible.

Comment thread lib/matplotlib/dates.py

locator.set_axis(self.axis)

if self.axis is not None:

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

ooops, I guess I missed a rebase here. Will fix after

jklymak force-pushed the add-datenum64 branch 4 times, most recently from ec3ec37 to a32aa1b Compare November 14, 2017 18:48
Comment thread lib/matplotlib/dates.py Outdated
zerod = False
if isinstance(d, np.datetime64):
zerod = True
d = np.atleast_1d(d)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

It runs for me without converting a single item into array and back. This is needed for some older numpy (mine is 1.13.3)?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Hmmm, I didn't even try! You're right..

Comment thread lib/matplotlib/dates.py
d = np.atleast_1d(d)
extra = d - d.astype('datetime64[s]')
extra = extra.astype('timedelta64[ns]')
t0 = np.datetime64('0001-01-01T00:00:00').astype('datetime64[s]')

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

t0 = np.datetime64('0001-01-01T00:00:00', 's')

import matplotlib.dates as mdates


def test_date_numpyx():

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

any reason this isn't done with base/time/timenp being pytest fixtures and the test parameterized or split into two?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Because I can understand this, and I have no idea what a pytest fixture is. Is there an advantage to doing something more obscure than a simple assert?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Only cause you're repeating yourself a lot so it's hard to tell what the specific tests are. Factoring out common features into a fixture means you can make two tests that are uniquely named for the things they're testing.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

I'll look into it. OTOH, I find some of the tests aren't very easy to follow. Sometime verbosity is a good thing!

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

OK, happy to take suggestions, but I find the uses of fixtures in the existing tests makes for pretty obfuscated code.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

shrugs can agree on that somewhat, either way it's not something I'd hold the PR up on.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Well, I didn't mean to say I wasn't willing to learn, just that I don't get it... More than happy if you wanted to push a change or give me some pseudo code to get it to work... Thanks!

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Sorry for missing your reply (the embedded ones seem to not pop up as notifications), and will totally try to remember your suggestion for the future. I should have been way clearer on what I wanted and given you an example. :/

Comment thread lib/matplotlib/tests/test_dates.py Outdated
assert np.array_equal(h.get_ydata(orig=False), hnp.get_ydata(orig=False))


def test_date_date2num_numpy():

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

same here, could this be parameterized with ids?

dstansby Nov 15, 2017 •
edited
Loading

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

I sympathise with @jklymak re. pytest, (I still have no idea what a fixture is) but find paramaterising fairly easy to understand (https://docs.pytest.org/en/latest/parametrize.html)

Something like this should work:

@pytest.mark.parametrize('t0', [datetime.datetime(2017, 1, 1, 0, 1, 1),

                                [datetime.datetime(2017, 1, 1, 0, 1, 1),
                                 datetime.datetime(2017, 1, 1, 1, 1, 1)],

                                [[datetime.datetime(2017, 1, 1, 0, 1, 1),
                                  datetime.datetime(2017, 1, 1, 1, 1, 1)],
                                 [datetime.datetime(2017, 1, 1, 2, 1, 1),
                                  datetime.datetime(2017, 1, 1, 3, 1, 1)]]])
@pytest.mark.parameterize('dtype', ['datetime64[s]',
                                    'datetime64[us]',
                                    'datetime64[ms]'])
def test_date_numpyx(t0, dytpe):
    time = mdates.date2num(t0)
    tnp = np.array(t0, dtype=dtype)
    nptime = mdates.date2num(tnp)
    assert time == nptime

This will run through every possible combination of t0 and dtype, and avoids copy/pasting the actual testing code.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

OK, I agree that parametrize works well here (despite pytests odd way of spelling it) Thanks for the example!

dstansby left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

👍 Looks good to me, thanks a lot for squashing this long running bug! I'm happy to tidy up the tests if you want @jklymak?

Kojoley merged commit 3ccaf2d into matplotlib:master Nov 15, 2017

Copy link
Copy Markdown
Member

That was easier than I expected...

Copy link
Copy Markdown
Member

Could someone open up a follow-on PR to add a whats_new about this?

jklymak commented Nov 15, 2017

Copy link
Copy Markdown
Member Author

Oops sorry I’ll do so tonight and prob add an example as well.

This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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.

7 participants


Back | FazBrowse Home | New Git URL