From a955ca13b29cc8f290f5c5a872ab60fa51b98ff4 Mon Sep 17 00:00:00 2001 From: Jessica Moore Date: Mon, 2 Jan 2023 08:49:13 -0500 Subject: [PATCH 1/9] Add documentation to __add__ --- pandas/core/arraylike.py | 47 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/pandas/core/arraylike.py b/pandas/core/arraylike.py index 4bed021cabea1..8840a4c4e8a62 100644 --- a/pandas/core/arraylike.py +++ b/pandas/core/arraylike.py @@ -97,6 +97,53 @@ def _arith_method(self, other, op): @unpack_zerodim_and_defer("__add__") def __add__(self, other): + """ + Get addition of DataFrame and other, column-wise. + + This is equivalent to df.add(other, axis=1). + + Parameters + ---------- + other : array-like + Object to be added to the DataFrame. + + Returns + ------- + DataFrame + The result of adding ``other`` to DataFrame. + + See Also + -------- + DataFrame.add : Add a DataFrame and another object, with option for index- + or column-oriented addition. + + Examples + -------- + >>> df = pd.DataFrame({"A": [1, 2], "B": [2, 0]}, index=["AA", "BB"]) + >>> df[["A", "B"]] + 1.5 + A B + AA 2.5 3.5 + BB 3.5 1.5 + + When `other` is a :class:`Series`, the index of `other` is aligned with the + columns of the DataFrame: + + >>> s = pd.Series([1.5, 2.5], index=['A', 'B']) + >>> df[["A", "B"]] + s + A B + AA 2.5 4.5 + BB 3.5 2.5 + + Even when the index of `other` is the same as the index of the DataFrame, + the :class:`Series` will not be reoriented. If index-wise alignment is desired, + :meth:`DataFrame.add` should be used. + + >>> s = pd.Series([1.5, 2.5], index=['AA', 'BB']) + >>> df[["A", "B"]] + s + A AA B BB + AA NaN NaN NaN NaN + BB NaN NaN NaN NaN + """ return self._arith_method(other, operator.add) @unpack_zerodim_and_defer("__radd__") From e2b9e6d023a6cf372b0585d3e2ed07ff90264b30 Mon Sep 17 00:00:00 2001 From: Jessica Moore Date: Wed, 4 Jan 2023 17:40:34 -0500 Subject: [PATCH 2/9] Update examples for consistency with other docs --- pandas/core/arraylike.py | 31 ++++++++++++++++--------------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/pandas/core/arraylike.py b/pandas/core/arraylike.py index 8840a4c4e8a62..6055b69545927 100644 --- a/pandas/core/arraylike.py +++ b/pandas/core/arraylike.py @@ -119,30 +119,31 @@ def __add__(self, other): Examples -------- - >>> df = pd.DataFrame({"A": [1, 2], "B": [2, 0]}, index=["AA", "BB"]) - >>> df[["A", "B"]] + 1.5 - A B - AA 2.5 3.5 - BB 3.5 1.5 + >>> df = pd.DataFrame({'height': [1.5, 2.6], 'weight': [500, 800]}, + ... index=['elk', 'moose']) + >>> df[['height', 'weight']] + 1.5 + height weight + elk 3.0 501.5 + moose 4.1 801.5 When `other` is a :class:`Series`, the index of `other` is aligned with the columns of the DataFrame: - >>> s = pd.Series([1.5, 2.5], index=['A', 'B']) - >>> df[["A", "B"]] + s - A B - AA 2.5 4.5 - BB 3.5 2.5 + >>> s = pd.Series([0.5, 1.5], index=['height', 'weight']) + >>> df[['height', 'weight']] + s + height weight + elk 2.0 501.5 + moose 3.1 801.5 Even when the index of `other` is the same as the index of the DataFrame, the :class:`Series` will not be reoriented. If index-wise alignment is desired, :meth:`DataFrame.add` should be used. - >>> s = pd.Series([1.5, 2.5], index=['AA', 'BB']) - >>> df[["A", "B"]] + s - A AA B BB - AA NaN NaN NaN NaN - BB NaN NaN NaN NaN + >>> s = pd.Series([0.5, 1.5], index=['elk', 'moose']) + >>> df[['height', 'weight']] + s + elk height moose weight + elk NaN NaN NaN NaN + moose NaN NaN NaN NaN """ return self._arith_method(other, operator.add) From 599de57a7d88e32ca29e3bc9d73dcf11f00d4aca Mon Sep 17 00:00:00 2001 From: Jessica Moore Date: Sat, 7 Jan 2023 10:19:26 -0500 Subject: [PATCH 3/9] Minor clean-up --- pandas/core/arraylike.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/pandas/core/arraylike.py b/pandas/core/arraylike.py index 6055b69545927..0a8a35b28dee8 100644 --- a/pandas/core/arraylike.py +++ b/pandas/core/arraylike.py @@ -98,13 +98,13 @@ def _arith_method(self, other, op): @unpack_zerodim_and_defer("__add__") def __add__(self, other): """ - Get addition of DataFrame and other, column-wise. + Get Addition of DataFrame and other, column-wise. - This is equivalent to df.add(other, axis=1). + Equivalent to ``DataFrame.add(other)``. Parameters ---------- - other : array-like + other : scalar, sequence, Series, dict or DataFrame Object to be added to the DataFrame. Returns @@ -137,7 +137,7 @@ def __add__(self, other): Even when the index of `other` is the same as the index of the DataFrame, the :class:`Series` will not be reoriented. If index-wise alignment is desired, - :meth:`DataFrame.add` should be used. + :meth:`DataFrame.add` should be used with `axis='index'`. >>> s = pd.Series([0.5, 1.5], index=['elk', 'moose']) >>> df[['height', 'weight']] + s From d97c2a2bf39d468e41095807175c3a5018b239c5 Mon Sep 17 00:00:00 2001 From: Jessica Moore Date: Sat, 7 Jan 2023 12:02:52 -0500 Subject: [PATCH 4/9] Update docs --- doc/source/reference/frame.rst | 1 + pandas/core/arraylike.py | 66 ++++++++++++++++++++++++++-------- 2 files changed, 53 insertions(+), 14 deletions(-) diff --git a/doc/source/reference/frame.rst b/doc/source/reference/frame.rst index ea19bb6d85aed..ec29f1dc0d67d 100644 --- a/doc/source/reference/frame.rst +++ b/doc/source/reference/frame.rst @@ -83,6 +83,7 @@ Binary operator functions .. autosummary:: :toctree: api/ + DataFrame.__add__ DataFrame.add DataFrame.sub DataFrame.mul diff --git a/pandas/core/arraylike.py b/pandas/core/arraylike.py index 0a8a35b28dee8..1ca47c0ae3e06 100644 --- a/pandas/core/arraylike.py +++ b/pandas/core/arraylike.py @@ -121,29 +121,67 @@ def __add__(self, other): -------- >>> df = pd.DataFrame({'height': [1.5, 2.6], 'weight': [500, 800]}, ... index=['elk', 'moose']) + >>> df + height weight + elk 1.5 500 + moose 2.6 800 + + Adding a scalar affects all rows and columns. + >>> df[['height', 'weight']] + 1.5 - height weight - elk 3.0 501.5 - moose 4.1 801.5 + height weight + elk 3.0 501.5 + moose 4.1 801.5 + + Each element of a list is added to a column of the DataFrame, in order. + + >>> df[['height', 'weight']] + [0.5, 1.5] + height weight + elk 2.0 501.5 + moose 3.1 801.5 + + Keys of a dictionary are aligned to the DataFrame, based on column names; + each value in the dictionary is added to the corresponding column. + + >>> df + {'height': 0.5, 'weight': 1.5} + height weight + elk 2.0 501.5 + moose 3.1 801.5 When `other` is a :class:`Series`, the index of `other` is aligned with the - columns of the DataFrame: + columns of the DataFrame. - >>> s = pd.Series([0.5, 1.5], index=['height', 'weight']) - >>> df[['height', 'weight']] + s - height weight - elk 2.0 501.5 - moose 3.1 801.5 + >>> s1 = pd.Series([0.5, 1.5], index=['weight', 'height']) + >>> df[['height', 'weight']] + s1 + height weight + elk 3.0 500.5 + moose 4.1 800.5 Even when the index of `other` is the same as the index of the DataFrame, the :class:`Series` will not be reoriented. If index-wise alignment is desired, :meth:`DataFrame.add` should be used with `axis='index'`. - >>> s = pd.Series([0.5, 1.5], index=['elk', 'moose']) - >>> df[['height', 'weight']] + s - elk height moose weight - elk NaN NaN NaN NaN - moose NaN NaN NaN NaN + >>> s2 = pd.Series([0.5, 1.5], index=['elk', 'moose']) + >>> df[['height', 'weight']] + s2 + elk height moose weight + elk NaN NaN NaN NaN + moose NaN NaN NaN NaN + + >>> df[['height', 'weight']].add(s2, axis='index') + height weight + elk 2.0 500.5 + moose 4.1 801.5 + + When `other` is a :class:`DataFrame`, both columns names and the + index are aligned. + + >>> other = pd.DataFrame({'height': [0.2, 0.4, 0.6]}, + ... index=['elk', 'moose', 'deer']) + >>> df + other + height weight + deer NaN NaN + elk 1.7 NaN + moose 3.0 NaN """ return self._arith_method(other, operator.add) From d2158e3dd37ca37d562933d8aaa86fc973bde6aa Mon Sep 17 00:00:00 2001 From: Jessica Moore Date: Thu, 19 Jan 2023 08:47:49 -0500 Subject: [PATCH 5/9] Clean up indentation --- pandas/core/arraylike.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/pandas/core/arraylike.py b/pandas/core/arraylike.py index 1ca47c0ae3e06..12ea1df909635 100644 --- a/pandas/core/arraylike.py +++ b/pandas/core/arraylike.py @@ -123,21 +123,21 @@ def __add__(self, other): ... index=['elk', 'moose']) >>> df height weight - elk 1.5 500 + elk 1.5 500 moose 2.6 800 Adding a scalar affects all rows and columns. >>> df[['height', 'weight']] + 1.5 height weight - elk 3.0 501.5 + elk 3.0 501.5 moose 4.1 801.5 Each element of a list is added to a column of the DataFrame, in order. >>> df[['height', 'weight']] + [0.5, 1.5] height weight - elk 2.0 501.5 + elk 2.0 501.5 moose 3.1 801.5 Keys of a dictionary are aligned to the DataFrame, based on column names; @@ -145,7 +145,7 @@ def __add__(self, other): >>> df + {'height': 0.5, 'weight': 1.5} height weight - elk 2.0 501.5 + elk 2.0 501.5 moose 3.1 801.5 When `other` is a :class:`Series`, the index of `other` is aligned with the @@ -154,7 +154,7 @@ def __add__(self, other): >>> s1 = pd.Series([0.5, 1.5], index=['weight', 'height']) >>> df[['height', 'weight']] + s1 height weight - elk 3.0 500.5 + elk 3.0 500.5 moose 4.1 800.5 Even when the index of `other` is the same as the index of the DataFrame, @@ -164,7 +164,7 @@ def __add__(self, other): >>> s2 = pd.Series([0.5, 1.5], index=['elk', 'moose']) >>> df[['height', 'weight']] + s2 elk height moose weight - elk NaN NaN NaN NaN + elk NaN NaN NaN NaN moose NaN NaN NaN NaN >>> df[['height', 'weight']].add(s2, axis='index') @@ -180,7 +180,7 @@ def __add__(self, other): >>> df + other height weight deer NaN NaN - elk 1.7 NaN + elk 1.7 NaN moose 3.0 NaN """ return self._arith_method(other, operator.add) From 87bff2e530986036c189f522f535b34518c4e073 Mon Sep 17 00:00:00 2001 From: Jessica Moore Date: Sun, 22 Jan 2023 14:50:59 -0500 Subject: [PATCH 6/9] Additional indentation fixing --- pandas/core/arraylike.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/arraylike.py b/pandas/core/arraylike.py index 12ea1df909635..84599377e76d0 100644 --- a/pandas/core/arraylike.py +++ b/pandas/core/arraylike.py @@ -169,7 +169,7 @@ def __add__(self, other): >>> df[['height', 'weight']].add(s2, axis='index') height weight - elk 2.0 500.5 + elk 2.0 500.5 moose 4.1 801.5 When `other` is a :class:`DataFrame`, both columns names and the From 8292d5feb5def80481801fda3fad0ef81643c5a3 Mon Sep 17 00:00:00 2001 From: Jessica Moore Date: Sun, 22 Jan 2023 14:59:58 -0500 Subject: [PATCH 7/9] Indentation --- pandas/core/arraylike.py | 50 ++++++++++++++++++++-------------------- 1 file changed, 25 insertions(+), 25 deletions(-) diff --git a/pandas/core/arraylike.py b/pandas/core/arraylike.py index 84599377e76d0..072964af83d75 100644 --- a/pandas/core/arraylike.py +++ b/pandas/core/arraylike.py @@ -122,40 +122,40 @@ def __add__(self, other): >>> df = pd.DataFrame({'height': [1.5, 2.6], 'weight': [500, 800]}, ... index=['elk', 'moose']) >>> df - height weight - elk 1.5 500 - moose 2.6 800 + height weight + elk 1.5 500 + moose 2.6 800 Adding a scalar affects all rows and columns. >>> df[['height', 'weight']] + 1.5 - height weight - elk 3.0 501.5 - moose 4.1 801.5 + height weight + elk 3.0 501.5 + moose 4.1 801.5 Each element of a list is added to a column of the DataFrame, in order. >>> df[['height', 'weight']] + [0.5, 1.5] - height weight - elk 2.0 501.5 - moose 3.1 801.5 + height weight + elk 2.0 501.5 + moose 3.1 801.5 Keys of a dictionary are aligned to the DataFrame, based on column names; each value in the dictionary is added to the corresponding column. >>> df + {'height': 0.5, 'weight': 1.5} - height weight - elk 2.0 501.5 - moose 3.1 801.5 + height weight + elk 2.0 501.5 + moose 3.1 801.5 When `other` is a :class:`Series`, the index of `other` is aligned with the columns of the DataFrame. >>> s1 = pd.Series([0.5, 1.5], index=['weight', 'height']) >>> df[['height', 'weight']] + s1 - height weight - elk 3.0 500.5 - moose 4.1 800.5 + height weight + elk 3.0 500.5 + moose 4.1 800.5 Even when the index of `other` is the same as the index of the DataFrame, the :class:`Series` will not be reoriented. If index-wise alignment is desired, @@ -163,14 +163,14 @@ def __add__(self, other): >>> s2 = pd.Series([0.5, 1.5], index=['elk', 'moose']) >>> df[['height', 'weight']] + s2 - elk height moose weight - elk NaN NaN NaN NaN - moose NaN NaN NaN NaN + elk height moose weight + elk NaN NaN NaN NaN + moose NaN NaN NaN NaN >>> df[['height', 'weight']].add(s2, axis='index') - height weight - elk 2.0 500.5 - moose 4.1 801.5 + height weight + elk 2.0 500.5 + moose 4.1 801.5 When `other` is a :class:`DataFrame`, both columns names and the index are aligned. @@ -178,10 +178,10 @@ def __add__(self, other): >>> other = pd.DataFrame({'height': [0.2, 0.4, 0.6]}, ... index=['elk', 'moose', 'deer']) >>> df + other - height weight - deer NaN NaN - elk 1.7 NaN - moose 3.0 NaN + height weight + deer NaN NaN + elk 1.7 NaN + moose 3.0 NaN """ return self._arith_method(other, operator.add) From 87af7d67778aa49dc1b86be1a7c5a726798fc7a7 Mon Sep 17 00:00:00 2001 From: Jessica Moore Date: Sun, 22 Jan 2023 15:08:37 -0500 Subject: [PATCH 8/9] Indentation --- pandas/core/arraylike.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/pandas/core/arraylike.py b/pandas/core/arraylike.py index 072964af83d75..8ea9f644017d9 100644 --- a/pandas/core/arraylike.py +++ b/pandas/core/arraylike.py @@ -122,21 +122,21 @@ def __add__(self, other): >>> df = pd.DataFrame({'height': [1.5, 2.6], 'weight': [500, 800]}, ... index=['elk', 'moose']) >>> df - height weight + height weight elk 1.5 500 moose 2.6 800 Adding a scalar affects all rows and columns. >>> df[['height', 'weight']] + 1.5 - height weight + height weight elk 3.0 501.5 moose 4.1 801.5 Each element of a list is added to a column of the DataFrame, in order. >>> df[['height', 'weight']] + [0.5, 1.5] - height weight + height weight elk 2.0 501.5 moose 3.1 801.5 @@ -144,7 +144,7 @@ def __add__(self, other): each value in the dictionary is added to the corresponding column. >>> df + {'height': 0.5, 'weight': 1.5} - height weight + height weight elk 2.0 501.5 moose 3.1 801.5 @@ -153,7 +153,7 @@ def __add__(self, other): >>> s1 = pd.Series([0.5, 1.5], index=['weight', 'height']) >>> df[['height', 'weight']] + s1 - height weight + height weight elk 3.0 500.5 moose 4.1 800.5 @@ -163,12 +163,12 @@ def __add__(self, other): >>> s2 = pd.Series([0.5, 1.5], index=['elk', 'moose']) >>> df[['height', 'weight']] + s2 - elk height moose weight + elk height moose weight elk NaN NaN NaN NaN moose NaN NaN NaN NaN >>> df[['height', 'weight']].add(s2, axis='index') - height weight + height weight elk 2.0 500.5 moose 4.1 801.5 @@ -178,7 +178,7 @@ def __add__(self, other): >>> other = pd.DataFrame({'height': [0.2, 0.4, 0.6]}, ... index=['elk', 'moose', 'deer']) >>> df + other - height weight + height weight deer NaN NaN elk 1.7 NaN moose 3.0 NaN From 74c178e2aaab18c7191d7ee2d96153b63106ebbc Mon Sep 17 00:00:00 2001 From: Jessica Moore Date: Sun, 29 Jan 2023 20:47:25 -0500 Subject: [PATCH 9/9] Column reference consistency --- pandas/core/arraylike.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/core/arraylike.py b/pandas/core/arraylike.py index 8ea9f644017d9..1d10d797866f4 100644 --- a/pandas/core/arraylike.py +++ b/pandas/core/arraylike.py @@ -143,7 +143,7 @@ def __add__(self, other): Keys of a dictionary are aligned to the DataFrame, based on column names; each value in the dictionary is added to the corresponding column. - >>> df + {'height': 0.5, 'weight': 1.5} + >>> df[['height', 'weight']] + {'height': 0.5, 'weight': 1.5} height weight elk 2.0 501.5 moose 3.1 801.5 @@ -177,7 +177,7 @@ def __add__(self, other): >>> other = pd.DataFrame({'height': [0.2, 0.4, 0.6]}, ... index=['elk', 'moose', 'deer']) - >>> df + other + >>> df[['height', 'weight']] + other height weight deer NaN NaN elk 1.7 NaN