Skip to content

Commit 9fdf115

Browse files
committed
Add pandas-based test.
1 parent 957c36f commit 9fdf115

File tree

1 file changed

+41
-14
lines changed

1 file changed

+41
-14
lines changed

lib/matplotlib/tests/test_dates.py

+41-14
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,27 @@ def test_date_inverted_limit():
357357
fig.subplots_adjust(left=0.25)
358358

359359

360+
def _test_date2num_dst(date_range, tz_convert):
361+
# Timezones
362+
BRUSSELS = pytz.timezone('Europe/Brussels')
363+
UTC = pytz.UTC
364+
365+
# Create a list of timezone-aware datetime objects in UTC
366+
# Interval is 0b0.0000011 days, to prevent float rounding issues
367+
dtstart = datetime.datetime(2014, 3, 30, 0, 0, tzinfo=UTC)
368+
interval = datetime.timedelta(minutes=33, seconds=45)
369+
interval_days = 0.0234375 # 2025 / 86400 seconds
370+
N = 8
371+
372+
dt_utc = date_range(start=dtstart, freq=interval, periods=N)
373+
dt_bxl = tz_convert(dt_utc, BRUSSELS)
374+
375+
expected_ordinalf = [735322.0 + (i * interval_days) for i in range(N)]
376+
actual_ordinalf = list(mdates.date2num(dt_bxl))
377+
378+
assert_equal(actual_ordinalf, expected_ordinalf)
379+
380+
360381
def test_date2num_dst():
361382
# Test for github issue #3896, but in date2num around DST transitions
362383
# with a timezone-aware pandas date_range object.
@@ -408,25 +429,31 @@ def mk_tzaware(cls, datetime_obj):
408429

409430
return cls(**kwargs)
410431

411-
# Timezones
412-
BRUSSELS = pytz.timezone('Europe/Brussels')
413-
UTC = pytz.UTC
432+
# Define a date_range function similar to pandas.date_range
433+
def date_range(start, freq, periods):
434+
dtstart = dt_tzaware.mk_tzaware(start)
414435

415-
# Create a list of timezone-aware datetime objects in UTC
416-
# Interval is 0b0.0000011 days, to prevent float rounding issues
417-
dtstart = dt_tzaware(2014, 3, 30, 0, 0, tzinfo=UTC)
418-
interval = datetime.timedelta(minutes=33, seconds=45)
419-
interval_days = 0.0234375 # 2025 / 86400 seconds
420-
N = 8
436+
return [dtstart + (i * freq) for i in range(periods)]
421437

422-
dt_utc = [dtstart + i * interval for i in range(N)]
423-
dt_bxl = [d.astimezone(BRUSSELS) for d in dt_utc]
438+
# Define a tz_convert function that converts a list to a new time zone.
439+
def tz_convert(dt_list, tzinfo):
440+
return [d.astimezone(tzinfo) for d in dt_list]
424441

425-
expected_ordinalf = [735322.0 + (i * interval_days) for i in range(N)]
442+
_test_date2num_dst(date_range, tz_convert)
426443

427-
actual_ordinalf = list(mdates.date2num(dt_bxl))
428444

429-
assert_equal(actual_ordinalf, expected_ordinalf)
445+
def test_date2num_dst_pandas():
446+
# Test for github issue #3896, but in date2num around DST transitions
447+
# with a timezone-aware pandas date_range object.
448+
try:
449+
import pandas as pd
450+
except ImportError:
451+
raise SkipTest('pandas not installed')
452+
453+
def tz_convert(*args):
454+
return pd.DatetimeIndex.tz_convert(*args).astype(datetime.datetime)
455+
456+
_test_date2num_dst(pd.date_range, tz_convert)
430457

431458

432459
if __name__ == '__main__':

0 commit comments

Comments
 (0)