Into what unit does matplotlib dates converts datetime information by date2num?

Issue

This Content is from Stack Overflow. Question asked by Lepakk

I have some time delta information stored in a DaraFrame.
They look like

df.timedelta_last_nms[0:5]
Out: 
0   0 days 00:00:00
1   0 days 00:02:50
2   0 days 00:05:16
3   0 days 00:07:02
4   0 days 00:08:55
Name: timedelta_last_nms, dtype: timedelta64[ns]

Plotting this data as x-axis leads to a numbers on the scale of roughly 10^12 or so, which is, I believe, timedeltas in nanoseconds.
Therefore I tried converting the datetime information into numbers, using matplotlib.dates.date2num.

import matplotlib.dates as mdates    
mdates.date2num(df.timedelta_last_nms[0:5])
Out: array([0.        , 1.96759259, 3.65740741, 4.88425926, 6.19212963])

The datetime data is successfully converted into scalar numbers (or an array thereof), but the unit remains unclear. It seems like the unit is in the scale of about one to one and half minutes.

However, converting this back into datetime information, yields

mdates.num2date(mdates.date2num(df.timedelta_last_nms[0:5]))
Out: 
[datetime.datetime(1970, 1, 1, 0, 0, tzinfo=datetime.timezone.utc),
 datetime.datetime(1970, 1, 2, 23, 13, 20, tzinfo=datetime.timezone.utc),
 datetime.datetime(1970, 1, 4, 15, 46, 40, tzinfo=datetime.timezone.utc),
 datetime.datetime(1970, 1, 5, 21, 13, 20, tzinfo=datetime.timezone.utc),
 datetime.datetime(1970, 1, 7, 4, 36, 40, tzinfo=datetime.timezone.utc)]

where the scalar numbers seem to be interpreted as days.

Can anyone tell me, why the conversion units are different between the complementary functions and what the actual unit of date2num is?



Solution

This question is not yet answered, be the first one who answer using the comment. Later the confirmed answer will be published as the solution.

This Question and Answer are collected from stackoverflow and tested by JTuto community, is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?