Skip to content

feat: (df|s).hist(), (df|s).line(), (df|s).area(), (df|s).bar(), df.scatter() #1320

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 1 commit into from
Jan 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions bigframes/dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -4313,6 +4313,56 @@ def get_right_id(id):
def plot(self):
return plotting.PlotAccessor(self)

def hist(
self, by: typing.Optional[typing.Sequence[str]] = None, bins: int = 10, **kwargs
):
return self.plot.hist(by=by, bins=bins, **kwargs)

hist.__doc__ = inspect.getdoc(plotting.PlotAccessor.hist)

def line(
self,
x: typing.Optional[typing.Hashable] = None,
y: typing.Optional[typing.Hashable] = None,
**kwargs,
):
return self.plot.line(x=x, y=y, **kwargs)

line.__doc__ = inspect.getdoc(plotting.PlotAccessor.line)

def area(
self,
x: typing.Optional[typing.Hashable] = None,
y: typing.Optional[typing.Hashable] = None,
stacked: bool = True,
**kwargs,
):
return self.plot.area(x=x, y=y, stacked=stacked, **kwargs)

area.__doc__ = inspect.getdoc(plotting.PlotAccessor.area)

def bar(
self,
x: typing.Optional[typing.Hashable] = None,
y: typing.Optional[typing.Hashable] = None,
**kwargs,
):
return self.plot.bar(x=x, y=y, **kwargs)

bar.__doc__ = inspect.getdoc(plotting.PlotAccessor.bar)

def scatter(
self,
x: typing.Optional[typing.Hashable] = None,
y: typing.Optional[typing.Hashable] = None,
s: typing.Union[typing.Hashable, typing.Sequence[typing.Hashable]] = None,
c: typing.Union[typing.Hashable, typing.Sequence[typing.Hashable]] = None,
**kwargs,
):
return self.plot.scatter(x=x, y=y, s=s, c=c, **kwargs)

scatter.__doc__ = inspect.getdoc(plotting.PlotAccessor.scatter)

def __matmul__(self, other) -> DataFrame:
return self.dot(other)

Expand Down
50 changes: 44 additions & 6 deletions bigframes/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -1984,16 +1984,48 @@ def __array_ufunc__(

return NotImplemented

# Keep this at the bottom of the Series class to avoid
# confusing type checker by overriding str
@property
def str(self) -> strings.StringMethods:
return strings.StringMethods(self._block)

@property
def plot(self):
return plotting.PlotAccessor(self)

def hist(
self, by: typing.Optional[typing.Sequence[str]] = None, bins: int = 10, **kwargs
):
return self.plot.hist(by=by, bins=bins, **kwargs)

hist.__doc__ = inspect.getdoc(plotting.PlotAccessor.hist)

def line(
self,
x: typing.Optional[typing.Hashable] = None,
y: typing.Optional[typing.Hashable] = None,
**kwargs,
):
return self.plot.line(x=x, y=y, **kwargs)

line.__doc__ = inspect.getdoc(plotting.PlotAccessor.line)

def area(
self,
x: typing.Optional[typing.Hashable] = None,
y: typing.Optional[typing.Hashable] = None,
stacked: bool = True,
**kwargs,
):
return self.plot.area(x=x, y=y, stacked=stacked, **kwargs)

area.__doc__ = inspect.getdoc(plotting.PlotAccessor.area)

def bar(
self,
x: typing.Optional[typing.Hashable] = None,
y: typing.Optional[typing.Hashable] = None,
**kwargs,
):
return self.plot.bar(x=x, y=y, **kwargs)

bar.__doc__ = inspect.getdoc(plotting.PlotAccessor.bar)

def _slice(
self,
start: typing.Optional[int] = None,
Expand Down Expand Up @@ -2022,6 +2054,12 @@ def _cached(self, *, force: bool = True, session_aware: bool = True) -> Series:
self._block.cached(force=force, session_aware=session_aware)
return self

# Keep this at the bottom of the Series class to avoid
# confusing type checker by overriding str
@property
def str(self) -> strings.StringMethods:
return strings.StringMethods(self._block)


def _is_list_like(obj: typing.Any) -> typing_extensions.TypeGuard[typing.Sequence]:
return pandas.api.types.is_list_like(obj)
102 changes: 86 additions & 16 deletions tests/system/small/operations/test_plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,20 @@ def _check_legend_labels(ax, labels):
assert label == e


def test_series_hist_bins(scalars_dfs):
@pytest.mark.parametrize(
("alias"),
[
pytest.param(True),
pytest.param(False),
],
)
def test_series_hist_bins(scalars_dfs, alias):
scalars_df, scalars_pandas_df = scalars_dfs
bins = 5
ax = scalars_df["int64_col"].plot.hist(bins=bins)
if alias:
ax = scalars_df["int64_col"].hist(bins=bins)
else:
ax = scalars_df["int64_col"].plot.hist(bins=bins)
pd_ax = scalars_pandas_df["int64_col"].plot.hist(bins=bins)

# Compares axis values and height between bigframes and pandas histograms.
Expand All @@ -49,11 +59,21 @@ def test_series_hist_bins(scalars_dfs):
assert ax.patches[i]._height == pd_ax.patches[i]._height


def test_dataframes_hist_bins(scalars_dfs):
@pytest.mark.parametrize(
("alias"),
[
pytest.param(True),
pytest.param(False),
],
)
def test_dataframes_hist_bins(scalars_dfs, alias):
scalars_df, scalars_pandas_df = scalars_dfs
bins = 7
columns = ["int64_col", "int64_too", "float64_col"]
ax = scalars_df[columns].plot.hist(bins=bins)
if alias:
ax = scalars_df[columns].hist(bins=bins)
else:
ax = scalars_df[columns].plot.hist(bins=bins)
pd_ax = scalars_pandas_df[columns].plot.hist(bins=bins)

# Compares axis values and height between bigframes and pandas histograms.
Expand Down Expand Up @@ -171,10 +191,25 @@ def test_hist_kwargs_ticks_props(scalars_dfs):
tm.assert_almost_equal(ylabels[i].get_rotation(), pd_ylables[i].get_rotation())


def test_line(scalars_dfs):
@pytest.mark.parametrize(
("col_names", "alias"),
[
pytest.param(
["int64_col", "float64_col", "int64_too", "bool_col"], True, id="df_alias"
),
pytest.param(
["int64_col", "float64_col", "int64_too", "bool_col"], False, id="df"
),
pytest.param(["int64_col"], True, id="series_alias"),
pytest.param(["int64_col"], False, id="series"),
],
)
def test_line(scalars_dfs, col_names, alias):
scalars_df, scalars_pandas_df = scalars_dfs
col_names = ["int64_col", "float64_col", "int64_too", "bool_col"]
ax = scalars_df[col_names].plot.line()
if alias:
ax = scalars_df[col_names].line()
else:
ax = scalars_df[col_names].plot.line()
pd_ax = scalars_pandas_df[col_names].plot.line()
tm.assert_almost_equal(ax.get_xticks(), pd_ax.get_xticks())
tm.assert_almost_equal(ax.get_yticks(), pd_ax.get_yticks())
Expand All @@ -183,10 +218,21 @@ def test_line(scalars_dfs):
tm.assert_almost_equal(line.get_data()[1], pd_line.get_data()[1])


def test_area(scalars_dfs):
@pytest.mark.parametrize(
("col_names", "alias"),
[
pytest.param(["int64_col", "float64_col", "int64_too"], True, id="df_alias"),
pytest.param(["int64_col", "float64_col", "int64_too"], False, id="df"),
pytest.param(["int64_col"], True, id="series_alias"),
pytest.param(["int64_col"], False, id="series"),
],
)
def test_area(scalars_dfs, col_names, alias):
scalars_df, scalars_pandas_df = scalars_dfs
col_names = ["int64_col", "float64_col", "int64_too"]
ax = scalars_df[col_names].plot.area(stacked=False)
if alias:
ax = scalars_df[col_names].area(stacked=False)
else:
ax = scalars_df[col_names].plot.area(stacked=False)
pd_ax = scalars_pandas_df[col_names].plot.area(stacked=False)
tm.assert_almost_equal(ax.get_xticks(), pd_ax.get_xticks())
tm.assert_almost_equal(ax.get_yticks(), pd_ax.get_yticks())
Expand All @@ -195,10 +241,21 @@ def test_area(scalars_dfs):
tm.assert_almost_equal(line.get_data()[1], pd_line.get_data()[1])


def test_bar(scalars_dfs):
@pytest.mark.parametrize(
("col_names", "alias"),
[
pytest.param(["int64_col", "float64_col", "int64_too"], True, id="df_alias"),
pytest.param(["int64_col", "float64_col", "int64_too"], False, id="df"),
pytest.param(["int64_col"], True, id="series_alias"),
pytest.param(["int64_col"], False, id="series"),
],
)
def test_bar(scalars_dfs, col_names, alias):
scalars_df, scalars_pandas_df = scalars_dfs
col_names = ["int64_col", "float64_col", "int64_too"]
ax = scalars_df[col_names].plot.bar()
if alias:
ax = scalars_df[col_names].bar()
else:
ax = scalars_df[col_names].plot.bar()
pd_ax = scalars_pandas_df[col_names].plot.bar()
tm.assert_almost_equal(ax.get_xticks(), pd_ax.get_xticks())
tm.assert_almost_equal(ax.get_yticks(), pd_ax.get_yticks())
Expand All @@ -207,10 +264,23 @@ def test_bar(scalars_dfs):
tm.assert_almost_equal(line.get_data()[1], pd_line.get_data()[1])


def test_scatter(scalars_dfs):
@pytest.mark.parametrize(
("col_names", "alias"),
[
pytest.param(
["int64_col", "float64_col", "int64_too", "bool_col"], True, id="df_alias"
),
pytest.param(
["int64_col", "float64_col", "int64_too", "bool_col"], False, id="df"
),
],
)
def test_scatter(scalars_dfs, col_names, alias):
scalars_df, scalars_pandas_df = scalars_dfs
col_names = ["int64_col", "float64_col", "int64_too", "bool_col"]
ax = scalars_df[col_names].plot.scatter(x="int64_col", y="float64_col")
if alias:
ax = scalars_df[col_names].scatter(x="int64_col", y="float64_col")
else:
ax = scalars_df[col_names].plot.scatter(x="int64_col", y="float64_col")
pd_ax = scalars_pandas_df[col_names].plot.scatter(x="int64_col", y="float64_col")
tm.assert_almost_equal(ax.get_xticks(), pd_ax.get_xticks())
tm.assert_almost_equal(ax.get_yticks(), pd_ax.get_yticks())
Expand Down
Loading