10000 Add num2timedelta method with test by dstansby · Pull Request #8870 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content

Add num2timedelta method with test #8870

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

Merged
merged 2 commits into from
Aug 7, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Add num2timedelta method with test
  • Loading branch information
dstansby committed Jul 25, 2017
commit b8c2abf75452f88e841d5968e1437dc07a599c5c
32 changes: 32 additions & 0 deletions lib/matplotlib/dates.py
E9A6
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,38 @@ def num2date(x, tz=None):
return _from_ordinalf_np_vectorized(x, tz).tolist()


def _ordinalf_to_timedelta(x):
return datetime.timedelta(days=x)


_ordinalf_to_timedelta_np_vectorized = np.vectorize(_ordinalf_to_timedelta)


def num2timedelta(x):
"""
Converts number of days to a :class:`timdelta` object.
If *x* is a sequence, a sequence of :class:`timedelta` objects will
be returned.

Parameters
----------
x : float, sequence of floats
Number of days (fraction part represents hours, minutes, seconds)

Returns
-------
:class:`timedelta`
Copy link
Member

Choose a reason for hiding this comment

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

This is a timedelta or list[timedelta]


"""
if not cbook.iterable(x):
return _ordinalf_to_timedelta(x)
else:
x = np.asarray(x)
if not x.size:
return x
return _ordinalf_to_timedelta_np_vectorized(x).tolist()


def drange(dstart, dend, delta):
"""
Return a date range as float Gregorian ordinals. *dstart* and
Expand Down
9 changes: 9 additions & 0 deletions lib/matplotlib/tests/test_dates.py
Original file line number Diff line number Diff line change
Expand Up @@ -457,3 +457,12 @@ def test_DayLocator():
def test_tz_utc():
dt = datetime.datetime(1970, 1, 1, tzinfo=mdates.UTC)
dt.tzname()


@pytest.mark.parametrize("x, tdelta",
[(1, datetime.timedelta(days=1)),
([1, 1.5], [datetime.timedelta(days=1),
datetime.timedelta(days=1.5)])])
def test_num2timedelta(x, tdelta):
dt = mdates.num2timedelta(x)
assert dt == tdelta
0