Skip to content

docs: add docs for DataFrame and Series dunder methods #562

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 19 commits into from
Apr 12, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
f6f18b7
docs: add docs for `DataFrame.{radd,__add__,__radd__}`
shobsi Apr 2, 2024
db4a566
fix rendering, revert ineffective changes, add __eq__
shobsi Apr 2, 2024
57a7e20
Merge remote-tracking branch 'refs/remotes/github/main' into shobs-do…
shobsi Apr 2, 2024
3f572e0
newline
shobsi Apr 2, 2024
3b616e4
docs for more df dunders
shobsi Apr 3, 2024
8c1bc21
fix mypy errors and couple of wordings
shobsi Apr 3, 2024
9528054
Merge remote-tracking branch 'refs/remotes/github/main' into shobs-do…
shobsi Apr 3, 2024
8b7e533
fix sub and rmod, add docs for __bool__, __nonzero__, __getattr__
shobsi Apr 3, 2024
8975c0b
Merge remote-tracking branch 'refs/remotes/github/main' into shobs-do…
shobsi Apr 3, 2024
47030a3
add documentation for Series dunders
shobsi Apr 4, 2024
328c4fc
Merge remote-tracking branch 'refs/remotes/github/main' into shobs-do…
shobsi Apr 4, 2024
f93dc09
fix doctest failure with python 3.12
shobsi Apr 4, 2024
5bf202d
Merge remote-tracking branch 'refs/remotes/github/main' into shobs-do…
shobsi Apr 5, 2024
d870a1c
Merge remote-tracking branch 'refs/remotes/github/main' into shobs-do…
shobsi Apr 11, 2024
afa2197
move docstrings to third_party for compliance safety
shobsi Apr 11, 2024
e1d5ca3
Merge remote-tracking branch 'refs/remotes/github/main' into shobs-do…
shobsi Apr 11, 2024
c3b3114
add DataFrame.__getitem__ docstring and code samples
shobsi Apr 11, 2024
591c978
Merge remote-tracking branch 'refs/remotes/github/main' into shobs-do…
shobsi Apr 11, 2024
5d1d535
add dunder doc overrides from third_party
shobsi Apr 12, 2024
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
Prev Previous commit
Next Next commit
fix rendering, revert ineffective changes, add __eq__
  • Loading branch information
shobsi committed Apr 2, 2024
commit db4a566afe685cc8854ad0126f485bf7913d5162
9 changes: 5 additions & 4 deletions bigframes/dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -582,8 +582,8 @@ def __setattr__(self, key: str, value):
def __repr__(self) -> str:
"""Converts a DataFrame to a string using pandas dataframe __repr__.

Only represents the first ``bigframes.options.display.max_rows``
and ``bigframes.options.display.max_columns``.
Only represents the first `bigframes.options.display.max_rows`
and `bigframes.options.display.max_columns`.
"""
if bigframes.options.display.repr_mode == "deferred":
return formatter.repr_query_job(self.query_job)
Expand Down Expand Up @@ -784,11 +784,12 @@ def _apply_dataframe_binop(
def eq(self, other: typing.Any, axis: str | int = "columns") -> DataFrame:
return self._apply_binop(other, ops.eq_op, axis=axis)

def __eq__(self, other) -> DataFrame:
return self.eq(other)

def ne(self, other: typing.Any, axis: str | int = "columns") -> DataFrame:
return self._apply_binop(other, ops.ne_op, axis=axis)

__eq__ = eq # type: ignore

__ne__ = ne # type: ignore

def le(self, other: typing.Any, axis: str | int = "columns") -> DataFrame:
Expand Down
58 changes: 45 additions & 13 deletions third_party/bigframes_vendored/pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,7 @@ def to_gbq(
[2 rows x 2 columns]

Write a DataFrame to a BigQuery table with clustering columns:

>>> df = bpd.DataFrame({'col1': [1, 2], 'col2': [3, 4], 'col3': [5, 6]})
>>> clustering_cols = ['col1', 'col3']
>>> df.to_gbq(
Expand Down Expand Up @@ -1208,7 +1209,6 @@ def rename_axis(self, mapper: Optional[str], **kwargs) -> DataFrame:
Set the name of the axis for the index.

.. note::

Currently only accepts a single string parameter (the new name of the index).

Args:
Expand Down Expand Up @@ -1862,7 +1862,7 @@ def sort_index(
raise NotImplementedError(constants.ABSTRACT_METHOD_ERROR_MESSAGE)

# ----------------------------------------------------------------------
# Arithmetic Methods
# Arithmetic and Logical Methods

def eq(self, other, axis: str | int = "columns") -> DataFrame:
"""
Expand Down Expand Up @@ -1890,7 +1890,8 @@ def eq(self, other, axis: str | int = "columns") -> DataFrame:
rectangle True
Name: degrees, dtype: boolean

You can also use arithmetic operator ``==``:
You can also use logical operator `==`:

>>> df["degrees"] == 360
circle True
triangle False
Expand All @@ -1909,6 +1910,38 @@ def eq(self, other, axis: str | int = "columns") -> DataFrame:
"""
raise NotImplementedError(constants.ABSTRACT_METHOD_ERROR_MESSAGE)

def __eq__(self, other) -> DataFrame:
"""Check equality of DataFrame and other, column-wise, using logical
operator `==`.

Equivalent to `DataFrame.eq(other)`.

**Examples:**

>>> import bigframes.pandas as bpd
>>> bpd.options.display.progress_bar = None

>>> df = bpd.DataFrame({
... 'a': [0, 3, 4],
... 'b': [360, 0, 180]
... })
>>> df == 0
a b
0 True False
1 False True
2 False False
<BLANKLINE>
[3 rows x 2 columns]

Args:
other (scalar or DataFrame):
Object to be compared to the DataFrame for equality.

Returns:
DataFrame: The result of comparing `other` to DataFrame.
"""
raise NotImplementedError(constants.ABSTRACT_METHOD_ERROR_MESSAGE)

def ne(self, other, axis: str | int = "columns") -> DataFrame:
"""
Get not equal to of DataFrame and other, element-wise (binary operator `ne`).
Expand Down Expand Up @@ -2215,9 +2248,6 @@ def __add__(self, other) -> DataFrame:
Args:
other (scalar or DataFrame):
Object to be added to the DataFrame.
axis ({0 or 'index', 1 or 'columns'}):
Whether to compare by the index (0 or 'index') or columns.
(1 or 'columns'). For Series input, axis to match Series index on.

Returns:
DataFrame: The result of adding `other` to DataFrame.
Expand Down Expand Up @@ -4172,7 +4202,6 @@ def nsmallest(self, n: int, columns, keep: str = "first"):
performant.

.. note::

This function cannot be used with all column types. For example, when
specifying columns with `object` or `category` dtypes, ``TypeError`` is
raised.
Expand Down Expand Up @@ -5062,6 +5091,7 @@ def eval(self, expr: str) -> DataFrame:
injection if you pass user input to this function.

**Examples:**

>>> import bigframes.pandas as bpd
>>> bpd.options.display.progress_bar = None

Expand All @@ -5083,11 +5113,11 @@ def eval(self, expr: str) -> DataFrame:
4 7
dtype: Int64

Assignment is allowed though by default the original DataFrame is not
modified.
Assignment is allowed though by default the original DataFrame is not
modified.

>>> df.eval('C = A + B')
A B C
A B C
0 1 10 11
1 2 8 10
2 3 6 9
Expand All @@ -5096,7 +5126,7 @@ def eval(self, expr: str) -> DataFrame:
<BLANKLINE>
[5 rows x 3 columns]
>>> df
A B
A B
0 1 10
1 2 8
2 3 6
Expand All @@ -5105,15 +5135,15 @@ def eval(self, expr: str) -> DataFrame:
<BLANKLINE>
[5 rows x 2 columns]

Multiple columns can be assigned to using multi-line expressions:
Multiple columns can be assigned to using multi-line expressions:

>>> df.eval(
... '''
... C = A + B
... D = A - B
... '''
... )
A B C D
A B C D
0 1 10 11 -9
1 2 8 10 -6
2 3 6 9 -3
Expand All @@ -5137,6 +5167,7 @@ def query(self, expr: str) -> DataFrame | None:
Query the columns of a DataFrame with a boolean expression.

**Examples:**

>>> import bigframes.pandas as bpd
>>> bpd.options.display.progress_bar = None

Expand Down Expand Up @@ -5461,6 +5492,7 @@ def dot(self, other):
DataFrame and the index of other must contain the same values, as they
will be aligned prior to the multiplication.

.. note::
The dot method for Series computes the inner product, instead of the
matrix product here.

Expand Down