Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit4b179b9

Browse files
authored
Merge pull request#18021 from jklymak/fix-update-num2julian
FIX: update num2julian and julian2num
2 parents6e76411 +4757d98 commit4b179b9

File tree

3 files changed

+50
-11
lines changed

3 files changed

+50
-11
lines changed
Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
1-
Reverted deprecation of `~.dates.num2epoch` and `~.dates.epoch2num`
1+
Reverted deprecation of ``num2epoch`` and ``epoch2num``
22
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
33

44
These two functions were deprecated in 3.3.0, and did not return
55
an accurate Matplotlib datenum relative to the new Matplotlib epoch
66
handling (`~.dates.get_epoch` and:rc:`date.epoch`). This version
7-
reverts the deprecation and fixes those functions to work with
8-
`~.dates.get_epoch`.
7+
reverts the deprecation.
8+
9+
Functions ``epoch2num`` and ``dates.julian2num`` use ``date.epoch`` rcParam
10+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
11+
12+
Now `~.dates.epoch2num` and (undocumented) ``julian2num`` return floating point
13+
days since `~.dates.get_epoch` as set by:rc:`date.epoch`, instead of
14+
floating point days since the old epoch of "0000-12-31T00:00:00". If
15+
needed, you can translate from the new to old values as
16+
``old = new + mdates.date2num(np.datetime64('0000-12-31'))``

‎lib/matplotlib/dates.py

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,13 @@ def _get_rc_timezone():
197197
Time-related constants.
198198
"""
199199
EPOCH_OFFSET=float(datetime.datetime(1970,1,1).toordinal())
200-
JULIAN_OFFSET=1721424.5# Julian date at 0001-01-01
200+
# EPOCH_OFFSET is not used by matplotlib
201+
JULIAN_OFFSET=1721424.5# Julian date at 0000-12-31
202+
# note that the Julian day epoch is achievable w/
203+
# np.datetime64('-4713-11-24T12:00:00'); datetime64 is proleptic
204+
# Gregorian and BC has a one-year offset. So
205+
# np.datetime64('0000-12-31') - np.datetime64('-4713-11-24T12:00') = 1721424.5
206+
# Ref: https://en.wikipedia.org/wiki/Julian_day
201207
MICROSECONDLY=SECONDLY+1
202208
HOURS_PER_DAY=24.
203209
MIN_PER_HOUR=60.
@@ -445,14 +451,20 @@ def julian2num(j):
445451
Parameters
446452
----------
447453
j : float or sequence of floats
448-
Julian date(s)
454+
Julian dates (days relative to 4713 BC Jan 1, 12:00:00 Julian
455+
calendar or 4714 BC Nov 24, 12:00:00, proleptic Gregorian calendar).
449456
450457
Returns
451458
-------
452459
float or sequence of floats
453-
Matplotlibdate(s)
460+
Matplotlibdates (days relative to `.get_epoch`).
454461
"""
455-
returnnp.subtract(j,JULIAN_OFFSET)# Handles both scalar & nonscalar j.
462+
ep=np.datetime64(get_epoch(),'h').astype(float)/24.
463+
ep0=np.datetime64('0000-12-31T00:00:00','h').astype(float)/24.
464+
# Julian offset defined above is relative to 0000-12-31, but we need
465+
# relative to our current epoch:
466+
dt=JULIAN_OFFSET-ep0+ep
467+
returnnp.subtract(j,dt)# Handles both scalar & nonscalar j.
456468

457469

458470
defnum2julian(n):
@@ -462,14 +474,19 @@ def num2julian(n):
462474
Parameters
463475
----------
464476
n : float or sequence of floats
465-
Matplotlibdate(s)
477+
Matplotlibdates (days relative to `.get_epoch`).
466478
467479
Returns
468480
-------
469481
float or sequence of floats
470-
Julian date(s)
471-
"""
472-
returnnp.add(n,JULIAN_OFFSET)# Handles both scalar & nonscalar j.
482+
Julian dates (days relative to 4713 BC Jan 1, 12:00:00).
483+
"""
484+
ep=np.datetime64(get_epoch(),'h').astype(float)/24.
485+
ep0=np.datetime64('0000-12-31T00:00:00','h').astype(float)/24.
486+
# Julian offset defined above is relative to 0000-12-31, but we need
487+
# relative to our current epoch:
488+
dt=JULIAN_OFFSET-ep0+ep
489+
returnnp.add(n,dt)# Handles both scalar & nonscalar j.
473490

474491

475492
defnum2date(x,tz=None):

‎lib/matplotlib/tests/test_dates.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1010,3 +1010,17 @@ def test_epoch2num():
10101010
mdates.set_epoch('1970-01-01T00:00:00')
10111011
assertmdates.epoch2num(86400)==1.0
10121012
assertmdates.num2epoch(2.0)==86400*2
1013+
1014+
1015+
deftest_julian2num():
1016+
mdates._reset_epoch_test_example()
1017+
mdates.set_epoch('0000-12-31')
1018+
# 2440587.5 is julian date for 1970-01-01T00:00:00
1019+
# https://en.wikipedia.org/wiki/Julian_day
1020+
assertmdates.julian2num(2440588.5)==719164.0
1021+
assertmdates.num2julian(719165.0)==2440589.5
1022+
# set back to the default
1023+
mdates._reset_epoch_test_example()
1024+
mdates.set_epoch('1970-01-01T00:00:00')
1025+
assertmdates.julian2num(2440588.5)==1.0
1026+
assertmdates.num2julian(2.0)==2440589.5

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp