Skip to content

CLN: Exception in pd.plotting #28350

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

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
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
20 changes: 10 additions & 10 deletions pandas/plotting/_matplotlib/converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,9 +264,11 @@ def convert(values, unit, axis):
def _convert_1d(values, unit, axis):
def try_parse(values):
try:
return _dt_to_float_ordinal(tools.to_datetime(values))
except Exception:
dtvalues = tools.to_datetime(values)
except (ValueError, TypeError):
return values
else:
return _dt_to_float_ordinal(dtvalues)

if isinstance(values, (datetime, pydt.date)):
return _dt_to_float_ordinal(values)
Expand All @@ -293,12 +295,13 @@ def try_parse(values):

try:
values = tools.to_datetime(values)
except (ValueError, TypeError):
values = _dt_to_float_ordinal(values)
else:
if isinstance(values, Index):
values = _dt_to_float_ordinal(values)
else:
values = [_dt_to_float_ordinal(x) for x in values]
except Exception:
values = _dt_to_float_ordinal(values)

return values

Expand Down Expand Up @@ -426,12 +429,9 @@ def __call__(self):
ed = _from_ordinal(dates.date2num(dmax))
all_dates = date_range(start=st, end=ed, freq=freq, tz=tz).astype(object)

try:
if len(all_dates) > 0:
locs = self.raise_if_exceeds(dates.date2num(all_dates))
return locs
except Exception: # pragma: no cover
pass
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems to be a change in behaviour? Is this tested? Or do you think this could never raise?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the len check I dont think can raise. the self.raise_if_exceeds seems like the kind of thing that could raise, but I think thats a matplotlib method so I'll have to dig into that to find a failing case.

if len(all_dates) > 0:
locs = self.raise_if_exceeds(dates.date2num(all_dates))
return locs

lims = dates.date2num([dmin, dmax])
return lims
Expand Down
12 changes: 10 additions & 2 deletions pandas/plotting/_matplotlib/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from pandas.util._decorators import cache_readonly

from pandas.core.dtypes.common import (
is_float,
is_hashable,
is_integer,
is_iterator,
Expand Down Expand Up @@ -1196,10 +1197,17 @@ def _post_plot_logic(self, ax, data):
from matplotlib.ticker import FixedLocator

def get_label(i):
if is_float(i) and i == int(i):
i = int(i)
if not is_integer(i):
# TODO: is getting here indicative of a larger problem?
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@datapythonista I think you've worked in this area recently, any idea if this is something to worry about?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a test (or example code) that ended up here?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Didn't work in this exact part, but this seems reasonable. I'm assuming this only affects the values in the axis, and it removes the decimals if they are zero, right?

I'm wondering if instead of i == int(i) it'd be better to use i.apply(float.is_integer).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i had completely forgotten about float.is_integer; neat. But I don't think we in general have an apply do we?

return ""
try:
return pprint_thing(data.index[i])
except Exception:
val = data.index[i]
except IndexError:
# In tests we get here with both positive and negative `i`
return ""
return pprint_thing(val)

if self._need_to_set_index:
xticks = ax.get_xticks()
Expand Down