Skip to content

Commit

Permalink
Define Series.plot and Series.hist in class definition (pandas-dev#17199
Browse files Browse the repository at this point in the history
)
  • Loading branch information
jbrockmendel authored and jowens committed Sep 20, 2017
1 parent e8fab8a commit d089d44
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 23 deletions.
27 changes: 13 additions & 14 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@
from pandas._libs import index as libindex, tslib as libts, lib, iNaT
from pandas.core.config import get_option

import pandas.plotting._core as gfx

__all__ = ['Series']

_shared_doc_kwargs = dict(
Expand Down Expand Up @@ -2952,12 +2954,23 @@ def _dir_additions(self):
pass
return rv

# ----------------------------------------------------------------------
# Add plotting methods to Series
plot = base.AccessorProperty(gfx.SeriesPlotMethods,
gfx.SeriesPlotMethods)
hist = gfx.hist_series


Series._setup_axes(['index'], info_axis=0, stat_axis=0, aliases={'rows': 0})
Series._add_numeric_operations()
Series._add_series_only_operations()
Series._add_series_or_dataframe_operations()

# Add arithmetic!
ops.add_flex_arithmetic_methods(Series, **ops.series_flex_funcs)
ops.add_special_arithmetic_methods(Series, **ops.series_special_funcs)


# -----------------------------------------------------------------------------
# Supplementary functions

Expand Down Expand Up @@ -3129,17 +3142,3 @@ def create_from_value(value, index, dtype):
subarr = np.array(data, dtype=object, copy=copy)

return subarr


# ----------------------------------------------------------------------
# Add plotting methods to Series

import pandas.plotting._core as _gfx # noqa

Series.plot = base.AccessorProperty(_gfx.SeriesPlotMethods,
_gfx.SeriesPlotMethods)
Series.hist = _gfx.hist_series

# Add arithmetic!
ops.add_flex_arithmetic_methods(Series, **ops.series_flex_funcs)
ops.add_special_arithmetic_methods(Series, **ops.series_special_funcs)
4 changes: 2 additions & 2 deletions pandas/plotting/_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@
is_period_arraylike,
is_nested_list_like
)
from pandas.core.dtypes.generic import ABCSeries

from pandas.compat import lrange
import pandas.compat as compat
import pandas._libs.lib as lib
import pandas.core.common as com
from pandas.core.index import Index

from pandas.core.series import Series
from pandas.core.indexes.datetimes import date_range
import pandas.core.tools.datetimes as tools
import pandas.tseries.frequencies as frequencies
Expand Down Expand Up @@ -175,7 +175,7 @@ def _dt_to_float_ordinal(dt):
preserving hours, minutes, seconds and microseconds. Return value
is a :func:`float`.
"""
if (isinstance(dt, (np.ndarray, Index, Series)
if (isinstance(dt, (np.ndarray, Index, ABCSeries)
) and is_datetime64_ns_dtype(dt)):
base = dates.epoch2num(dt.asi8 / 1.0E9)
else:
Expand Down
10 changes: 7 additions & 3 deletions pandas/plotting/_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@
is_number,
is_hashable,
is_iterator)
from pandas.core.dtypes.generic import ABCSeries

from pandas.core.common import AbstractMethodError, _try_sort
from pandas.core.generic import _shared_docs, _shared_doc_kwargs
from pandas.core.index import Index, MultiIndex
from pandas.core.series import Series

from pandas.core.indexes.period import PeriodIndex
from pandas.compat import range, lrange, map, zip, string_types
import pandas.compat as compat
Expand Down Expand Up @@ -334,7 +336,7 @@ def result(self):
def _compute_plot_data(self):
data = self.data

if isinstance(data, Series):
if isinstance(data, ABCSeries):
label = self.label
if label is None and data.name is None:
label = 'None'
Expand Down Expand Up @@ -1575,6 +1577,7 @@ def maybe_color_bp(self, bp):

def _make_plot(self):
if self.subplots:
from pandas.core.series import Series
self._return_obj = Series()

for i, (label, y) in enumerate(self._iter_data()):
Expand Down Expand Up @@ -2338,6 +2341,7 @@ def boxplot_frame_groupby(grouped, subplots=True, column=None, fontsize=None,
figsize=figsize, layout=layout)
axes = _flatten(axes)

from pandas.core.series import Series
ret = Series()
for (key, group), ax in zip(grouped, axes):
d = group.boxplot(ax=ax, column=column, fontsize=fontsize,
Expand Down Expand Up @@ -2409,7 +2413,6 @@ def _grouped_plot_by_column(plotf, data, columns=None, by=None,

_axes = _flatten(axes)

result = Series()
ax_values = []

for i, col in enumerate(columns):
Expand All @@ -2422,6 +2425,7 @@ def _grouped_plot_by_column(plotf, data, columns=None, by=None,
ax_values.append(re_plotf)
ax.grid(grid)

from pandas.core.series import Series
result = Series(ax_values, index=columns)

# Return axes in multiplot case, maybe revisit later # 985
Expand Down
7 changes: 3 additions & 4 deletions pandas/plotting/_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
import numpy as np

from pandas.core.dtypes.common import is_list_like
from pandas.core.dtypes.generic import ABCSeries
from pandas.core.index import Index
from pandas.core.series import Series
from pandas.compat import range


Expand All @@ -25,8 +25,7 @@ def format_date_labels(ax, rot):
pass


def table(ax, data, rowLabels=None, colLabels=None,
**kwargs):
def table(ax, data, rowLabels=None, colLabels=None, **kwargs):
"""
Helper function to convert DataFrame and Series to matplotlib.table
Expand All @@ -45,7 +44,7 @@ def table(ax, data, rowLabels=None, colLabels=None,
matplotlib table object
"""
from pandas import DataFrame
if isinstance(data, Series):
if isinstance(data, ABCSeries):
data = DataFrame(data, columns=[data.name])
elif isinstance(data, DataFrame):
pass
Expand Down

0 comments on commit d089d44

Please sign in to comment.