Skip to content

Add error when plotting cftime #289

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

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Changes from all commits
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
25 changes: 21 additions & 4 deletions ultraplot/internals/inputs.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,24 +139,41 @@

def _to_numpy_array(data, strip_units=False):
"""
Convert arbitrary input to numpy array. Preserve masked arrays and unit arrays.
Convert arbitrary input to a numpy array. Preserve masked arrays and unit arrays.
If the array contains cftime objects, require nc-time-axis to be installed.
"""
_load_objects()
if data is None:
raise ValueError("Invalid data None.")

if isinstance(data, ndarray):
pass
elif isinstance(data, DataArray):
data = data.data # support pint quantities that get unit-stripped later
elif isinstance(data, (DataFrame, Series, Index)):
data = data.values

if Quantity is not ndarray and isinstance(data, Quantity):
if strip_units:
return np.atleast_1d(data.magnitude)
result = np.atleast_1d(data.magnitude)

Check warning on line 158 in ultraplot/internals/inputs.py

View check run for this annotation

Codecov / codecov/patch

ultraplot/internals/inputs.py#L158

Added line #L158 was not covered by tests
else:
return np.atleast_1d(data.magnitude) * data.units
result = np.atleast_1d(data.magnitude) * data.units
else:
return np.atleast_1d(data) # natively preserves masked arrays
result = np.atleast_1d(data) # natively preserves masked arrays
# If the array contains cftime objects, ensure nc-time-axis is available.
if result.size:
first_elem = result.flatten()[0]
if hasattr(
first_elem, "__class__"
) and first_elem.__class__.__module__.startswith("cftime"):
try:
import nc_time_axis # This module registers its converter automatically.
except ImportError:
raise ImportError(

Check warning on line 172 in ultraplot/internals/inputs.py

View check run for this annotation

Codecov / codecov/patch

ultraplot/internals/inputs.py#L169-L172

Added lines #L169 - L172 were not covered by tests
"The input data contains cftime objects, but nc_time_axis is not available. "
"Please install nc_time_axis to handle these dates correctly."
)
return result


def _to_masked_array(data, *, copy=False):
Expand Down