Skip to content

REF: Ensure MPLPlot.data is a DataFrame after __init__ #55888

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

Merged
merged 5 commits into from
Nov 10, 2023
Merged
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
11 changes: 7 additions & 4 deletions pandas/plotting/_matplotlib/boxplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,10 @@ def _make_plot(self, fig: Figure) -> None:
else self.data
)

for i, (label, y) in enumerate(self._iter_data(data=data)):
# error: Argument "data" to "_iter_data" of "MPLPlot" has
# incompatible type "object"; expected "DataFrame |
# dict[Hashable, Series | DataFrame]"
for i, (label, y) in enumerate(self._iter_data(data=data)): # type: ignore[arg-type]
ax = self._get_ax(i)
kwds = self.kwds.copy()

Expand All @@ -216,9 +219,9 @@ def _make_plot(self, fig: Figure) -> None:

# When `by` is assigned, the ticklabels will become unique grouped
# values, instead of label which is used as subtitle in this case.
ticklabels = [
pprint_thing(col) for col in self.data.columns.levels[0]
]
# error: "Index" has no attribute "levels"; maybe "nlevels"?
levels = self.data.columns.levels # type: ignore[attr-defined]
ticklabels = [pprint_thing(col) for col in levels[0]]
else:
ticklabels = [pprint_thing(label)]

Expand Down
22 changes: 17 additions & 5 deletions pandas/plotting/_matplotlib/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,8 @@ def _kind(self) -> str:
def orientation(self) -> str | None:
return None

data: DataFrame

def __init__(
self,
data,
Expand Down Expand Up @@ -274,6 +276,7 @@ def __init__(
self.kwds = kwds

self._validate_color_args()
self.data = self._ensure_frame(self.data)

@final
@staticmethod
Expand Down Expand Up @@ -623,9 +626,7 @@ def _convert_to_ndarray(data):
return data

@final
def _compute_plot_data(self):
data = self.data

def _ensure_frame(self, data) -> DataFrame:
if isinstance(data, ABCSeries):
label = self.label
if label is None and data.name is None:
Expand All @@ -638,6 +639,11 @@ def _compute_plot_data(self):
elif self._kind in ("hist", "box"):
cols = self.columns if self.by is None else self.columns + self.by
data = data.loc[:, cols]
return data

@final
def _compute_plot_data(self):
data = self.data

# GH15079 reconstruct data if by is defined
if self.by is not None:
Expand Down Expand Up @@ -891,6 +897,7 @@ def _get_xticks(self, convert_period: bool = False):
index = self.data.index
is_datetype = index.inferred_type in ("datetime", "date", "datetime64", "time")

# TODO: be stricter about x?
x: list[int] | np.ndarray
if self.use_index:
if convert_period and isinstance(index, ABCPeriodIndex):
Expand Down Expand Up @@ -1439,7 +1446,10 @@ def _make_plot(self, fig: Figure) -> None:
# "Callable[[Any, Any, Any, Any, Any, Any, KwArg(Any)], Any]", variable has
# type "Callable[[Any, Any, Any, Any, KwArg(Any)], Any]")
plotf = self._plot # type: ignore[assignment]
it = self._iter_data(data=self.data)
# error: Incompatible types in assignment (expression has type
# "Iterator[tuple[Hashable, ndarray[Any, Any]]]", variable has
# type "Iterable[tuple[Hashable, Series]]")
it = self._iter_data(data=self.data) # type: ignore[assignment]

stacking_id = self._get_stacking_id()
is_errorbar = com.any_not_none(*self.errors.values())
Expand All @@ -1452,7 +1462,9 @@ def _make_plot(self, fig: Figure) -> None:
colors,
kwds,
i,
label, # pyright: ignore[reportGeneralTypeIssues]
# error: Argument 4 to "_apply_style_colors" of "MPLPlot" has
# incompatible type "Hashable"; expected "str"
label, # type: ignore[arg-type] # pyright: ignore[reportGeneralTypeIssues]
)

errors = self._get_errorbars(label=label, index=i)
Expand Down
4 changes: 3 additions & 1 deletion pandas/plotting/_matplotlib/hist.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,9 @@ def _make_plot(self, fig: Figure) -> None:
else self.data
)

for i, (label, y) in enumerate(self._iter_data(data=data)):
# error: Argument "data" to "_iter_data" of "MPLPlot" has incompatible
# type "object"; expected "DataFrame | dict[Hashable, Series | DataFrame]"
for i, (label, y) in enumerate(self._iter_data(data=data)): # type: ignore[arg-type]
ax = self._get_ax(i)

kwds = self.kwds.copy()
Expand Down