From a3ef0ee1cf3973ab4274bf59acb12749405e81f1 Mon Sep 17 00:00:00 2001 From: Scott Shambaugh Date: Tue, 1 Apr 2025 09:54:45 -0600 Subject: [PATCH 1/3] Fix build error --- labellines/core.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/labellines/core.py b/labellines/core.py index d105288..1bac62a 100644 --- a/labellines/core.py +++ b/labellines/core.py @@ -3,8 +3,9 @@ import matplotlib.pyplot as plt import numpy as np +from datetime import datetime from matplotlib.container import ErrorbarContainer -from matplotlib.dates import DateConverter, num2date +from matplotlib.dates import DateConverter, num2date, _SwitchableDateConverter from matplotlib.lines import Line2D from more_itertools import always_iterable @@ -245,9 +246,11 @@ def labelLines( converter = ax.xaxis.converter else: converter = ax.xaxis.get_converter() - if isinstance(converter, DateConverter): + if isinstance(converter, (DateConverter, _SwitchableDateConverter)): xvals = [ - num2date(x).replace(tzinfo=ax.xaxis.get_units()) + x # type: ignore + if isinstance(x, (np.datetime64, datetime)) + else num2date(x).replace(tzinfo=ax.xaxis.get_units()) for x in xvals # type: ignore ] From d92805a703d19c36cd46ce4b8a3b10d71ba48c8b Mon Sep 17 00:00:00 2001 From: Scott Shambaugh Date: Tue, 1 Apr 2025 10:16:20 -0600 Subject: [PATCH 2/3] handle datetime64 --- labellines/core.py | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/labellines/core.py b/labellines/core.py index b92ece3..538e80b 100644 --- a/labellines/core.py +++ b/labellines/core.py @@ -284,12 +284,16 @@ def labelLines( converter = ax.xaxis.get_converter() time_classes = (_SwitchableDateConverter, DateConverter, ConciseDateConverter) if isinstance(converter, time_classes): - xvals = [ - x # type: ignore - if isinstance(x, (np.datetime64, datetime)) - else num2date(x).replace(tzinfo=ax.xaxis.get_units()) - for x in xvals # type: ignore - ] + xvals_dates = [] + for x in xvals: # type: ignore + if isinstance(x, datetime): + x_datetime = x + elif isinstance(x, np.datetime64): + x_datetime = x.astype(datetime) + else: + x_datetime = num2date(x) + xvals_dates.append(x_datetime.replace(tzinfo=ax.xaxis.get_units())) + xvals = xvals_dates # type: ignore txts = [] try: From 0ebd0419258ede94044d177673dea17668688af8 Mon Sep 17 00:00:00 2001 From: Corentin Cadiou Date: Thu, 3 Apr 2025 13:39:17 +0200 Subject: [PATCH 3/3] Explicitly check for None-ness --- labellines/core.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/labellines/core.py b/labellines/core.py index 538e80b..5b32e13 100644 --- a/labellines/core.py +++ b/labellines/core.py @@ -283,9 +283,14 @@ def labelLines( else: converter = ax.xaxis.get_converter() time_classes = (_SwitchableDateConverter, DateConverter, ConciseDateConverter) - if isinstance(converter, time_classes): + if xvals is None: + raise ValueError( + "xvals must be a tuple of two floats or a list of floats." + f"Got {xvals} instead." + ) + elif isinstance(converter, time_classes): xvals_dates = [] - for x in xvals: # type: ignore + for x in xvals: if isinstance(x, datetime): x_datetime = x elif isinstance(x, np.datetime64): @@ -293,7 +298,7 @@ def labelLines( else: x_datetime = num2date(x) xvals_dates.append(x_datetime.replace(tzinfo=ax.xaxis.get_units())) - xvals = xvals_dates # type: ignore + xvals = xvals_dates txts = [] try: