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
[Bug]: TypeError when plotting against list of datetime.date where 0th element of list is None #23580
Comments
Unfortunately I think this is the expected behavior (I was actually a bit surprised it worked with In [7]: np.asarray(x, dtype=np.datetime64)
Out[7]:
array(['2022-08-07', '2022-08-08', '2022-08-09', '2022-08-10',
'2022-08-11', 'NaT', '2022-08-13', '2022-08-14',
'2022-08-15', '2022-08-16'], dtype='datetime64[D]')
In [8]: x
Out[8]:
[datetime.date(2022, 8, 7),
datetime.date(2022, 8, 8),
datetime.date(2022, 8, 9),
datetime.date(2022, 8, 10),
datetime.date(2022, 8, 11),
None,
datetime.date(2022, 8, 13),
datetime.date(2022, 8, 14),
datetime.date(2022, 8, 15),
datetime.date(2022, 8, 16)] The reason this fails with We could add a special case to that dispatch to keep walking down the sequence if a I could also see a case for exploding a lot earlier if there is a In the short term, the user-side fix is to use |
diff --git i/lib/matplotlib/cbook/__init__.py w/lib/matplotlib/cbook/__init__.py
index b9d5f5eb26..9cbf273259 100644
--- i/lib/matplotlib/cbook/__init__.py
+++ w/lib/matplotlib/cbook/__init__.py
@@ -1691,19 +1691,12 @@ def safe_first_element(obj):
This is an type-independent way of obtaining the first element, supporting
both index access and the iterator protocol.
"""
- if isinstance(obj, collections.abc.Iterator):
- # needed to accept `array.flat` as input.
- # np.flatiter reports as an instance of collections.Iterator
- # but can still be indexed via [].
- # This has the side effect of re-setting the iterator, but
- # that is acceptable.
- try:
- return obj[0]
- except TypeError:
- pass
- raise RuntimeError("matplotlib does not support generators "
- "as input")
- return next(iter(obj))
+ if isinstance(obj, np.flatiter):
+ return obj[0]
+ elif isinstance(obj, collections.abc.Iterator):
+ raise RuntimeError("matplotlib does not support generators as input")
+ else:
+ return next(val for val in obj if val is not None)
def sanitize_sequence(data): seems reasonable enough to me? (except that the function becomes a bit misnamed, but such is life). |
None working elsewhere in the list seems to me an implementation accident rather than something we purposely designed. I'm not aware of us using None to mean no data elsewhere in the library (but I could be wrong) so I am not sure we should special case it here. |
I think this is an "in for a penny, in for a pound" situation. We are very permissive in terms of what we take as input and currently do take lists of datetimes with I'm leaning towards "take the complexity" because that is our job. |
I'm labeling this as a good first issue because we have a plausbile looking patch and there is no (explicit) new public API. Steps:
|
Hi there, |
@MikiPWata Yes, just be aware we do not consider an issue "claimed" until there is a PR and that there is not complete consensus among the devs on this yet. |
I checked that None is also ignored in bare lists of floats, so I guess this is OK. We probably should document this somehow though... |
@jklymak |
well, we finally had two people take up a "good first issue" at about the same time. |
Is there anything I can/should do to fix the test failures from CI? |
Finally passed all checks! I am pretty sure that I should be using the new method for the test made for this issue. |
Closed by #23587. |
Bug summary
pyplot raises an error when a list of dates starts with None.
Code for reproduction
Actual outcome
Expected outcome
plot data starting at index 1.
Additional information
No response
Operating system
windows 10
Matplotlib Version
3.5.2
Matplotlib Backend
TkAgg
Python version
3.8.2
Jupyter version
No response
Installation
pip
The text was updated successfully, but these errors were encountered: