From acab08eb0a79d32344b19d0deb315cac7fb2d8cd Mon Sep 17 00:00:00 2001 From: ParfaitG Date: Sun, 11 Mar 2018 11:25:31 -0500 Subject: [PATCH 01/23] DOC: Add examples for DataFrame.gt() and DataFrame.ge() --- pandas/core/ops.py | 64 ++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 62 insertions(+), 2 deletions(-) diff --git a/pandas/core/ops.py b/pandas/core/ops.py index 6c6a54993b669..cb73318927355 100644 --- a/pandas/core/ops.py +++ b/pandas/core/ops.py @@ -370,6 +370,66 @@ def _get_op_name(op, special): e NaN 2.0 """ +_gt_example_FRAME = """ +>>> df1 = pd.DataFrame({'num1': range(1,6), + 'num2': range(2,11,2), + 'num3': range(1,20,4)}) +>>> df1 + num1 num2 num3 +0 1 2 1 +1 2 4 5 +2 3 6 9 +3 4 8 13 +4 5 10 17 +>>> df2 = pd.DataFrame({'num1': range(6,11), + 'num2': range(1,10,2), + 'num3': range(1,20,4)}) +>>> df2 + num1 num2 num3 +0 6 1 1 +1 7 3 5 +2 8 5 9 +3 9 7 13 +4 10 9 17 +>>> df1.gt(df2) + num1 num2 num3 +0 False True False +1 False True False +2 False True False +3 False True False +4 False True False +""" + +_ge_example_FRAME = """ +>>> df1 = pd.DataFrame({'num1': range(1,6), + 'num2': range(2,11,2), + 'num3': range(1,20,4)}) +>>> df1 + num1 num2 num3 +0 1 2 1 +1 2 4 5 +2 3 6 9 +3 4 8 13 +4 5 10 17 +>>> df2 = pd.DataFrame({'num1': range(6,11), + 'num2': range(1,10,2), + 'num3': range(1,20,4)}) +>>> df2 + num1 num2 num3 +0 6 1 1 +1 7 3 5 +2 8 5 9 +3 9 7 13 +4 10 9 17 +>>> df1.ge(df2) + num1 num2 num3 +0 False True True +1 False True True +2 False True True +3 False True True +4 False True True +""" + _op_descriptions = { # Arithmetic Operators 'add': {'op': '+', @@ -425,11 +485,11 @@ def _get_op_name(op, special): 'gt': {'op': '>', 'desc': 'Greater than', 'reverse': None, - 'df_examples': None}, + 'df_examples': _gt_example_FRAME}, 'ge': {'op': '>=', 'desc': 'Greater than or equal to', 'reverse': None, - 'df_examples': None}} + 'df_examples': _ge_example_FRAME}} _op_names = list(_op_descriptions.keys()) for key in _op_names: From 13fed5f2ac7a0e517e695634bdb707ccc94f697a Mon Sep 17 00:00:00 2001 From: ParfaitG Date: Mon, 19 Mar 2018 20:39:25 -0500 Subject: [PATCH 02/23] DOC: Add examples to docstring of DataFrame.ge() and .gt() --- pandas/core/ops.py | 93 +++++++++++++++++++++++++++------------------- 1 file changed, 55 insertions(+), 38 deletions(-) diff --git a/pandas/core/ops.py b/pandas/core/ops.py index 7fb81f1f6dea0..b725f3a956657 100644 --- a/pandas/core/ops.py +++ b/pandas/core/ops.py @@ -370,11 +370,37 @@ def _get_op_name(op, special): e NaN 2.0 """ -<<<<<<< HEAD +_sub_example_FRAME = """ +>>> a = pd.DataFrame([2, 1, 1, np.nan], index=['a', 'b', 'c', 'd'], +... columns=['one']) +>>> a + one +a 2.0 +b 1.0 +c 1.0 +d NaN +>>> b = pd.DataFrame(dict(one=[1, np.nan, 1, np.nan], +... two=[3, 2, np.nan, 2]), +... index=['a', 'b', 'd', 'e']) +>>> b + one two +a 1.0 3.0 +b NaN 2.0 +d 1.0 NaN +e NaN 2.0 +>>> a.sub(b, fill_value=0) + one two +a 1.0 -3.0 +b 1.0 -2.0 +c 1.0 NaN +d -1.0 NaN +e NaN -2.0 +""" + _gt_example_FRAME = """ >>> df1 = pd.DataFrame({'num1': range(1,6), - 'num2': range(2,11,2), - 'num3': range(1,20,4)}) +... 'num2': range(2,11,2), +... 'num3': range(1,20,4)}) >>> df1 num1 num2 num3 0 1 2 1 @@ -383,8 +409,8 @@ def _get_op_name(op, special): 3 4 8 13 4 5 10 17 >>> df2 = pd.DataFrame({'num1': range(6,11), - 'num2': range(1,10,2), - 'num3': range(1,20,4)}) +... 'num2': range(1,10,2), +... 'num3': range(1,20,4)}) >>> df2 num1 num2 num3 0 6 1 1 @@ -403,8 +429,8 @@ def _get_op_name(op, special): _ge_example_FRAME = """ >>> df1 = pd.DataFrame({'num1': range(1,6), - 'num2': range(2,11,2), - 'num3': range(1,20,4)}) +... 'num2': range(2,11,2), +... 'num3': range(1,20,4)}) >>> df1 num1 num2 num3 0 1 2 1 @@ -413,8 +439,8 @@ def _get_op_name(op, special): 3 4 8 13 4 5 10 17 >>> df2 = pd.DataFrame({'num1': range(6,11), - 'num2': range(1,10,2), - 'num3': range(1,20,4)}) +... 'num2': range(1,10,2), +... 'num3': range(1,20,4)}) >>> df2 num1 num2 num3 0 6 1 1 @@ -429,33 +455,6 @@ def _get_op_name(op, special): 2 False True True 3 False True True 4 False True True -======= -_sub_example_FRAME = """ ->>> a = pd.DataFrame([2, 1, 1, np.nan], index=['a', 'b', 'c', 'd'], -... columns=['one']) ->>> a - one -a 2.0 -b 1.0 -c 1.0 -d NaN ->>> b = pd.DataFrame(dict(one=[1, np.nan, 1, np.nan], -... two=[3, 2, np.nan, 2]), -... index=['a', 'b', 'd', 'e']) ->>> b - one two -a 1.0 3.0 -b NaN 2.0 -d 1.0 NaN -e NaN 2.0 ->>> a.sub(b, fill_value=0) - one two -a 1.0 -3.0 -b 1.0 -2.0 -c 1.0 NaN -d -1.0 NaN -e NaN -2.0 ->>>>>>> 699a48bcd71da54da05caee85e5d006afabc3df6 """ _op_descriptions = { @@ -643,6 +642,14 @@ def _get_op_name(op, special): DataFrame.{reverse} """ +_flex_comp_doc_FRAME = """ +Wrapper for flexible comparison methods {name} + +Examples +-------- +{df_examples} +""" + _flex_doc_PANEL = """ {desc} of series and other, element-wise (binary operator `{op_name}`). Equivalent to ``{equiv}``. @@ -1607,8 +1614,18 @@ def na_op(x, y): result = mask_cmp_op(x, y, op, (np.ndarray, ABCSeries)) return result - @Appender('Wrapper for flexible comparison methods {name}' - .format(name=op_name)) + doc = ('Wrapper for flexible comparison methods {name}' + .format(name=op_name)) + + if op_name in _op_descriptions: + op_desc = _op_descriptions[op_name] + + if op_desc['df_examples'] is not None: + base_doc = _flex_comp_doc_FRAME + doc = base_doc.format(name=op_name, + df_examples=op_desc['df_examples']) + + @Appender(doc) def f(self, other, axis=default_axis, level=None): other = _align_method_FRAME(self, other, axis) From 8bdbc145b10294a91be106bec53b80db08bfac74 Mon Sep 17 00:00:00 2001 From: ParfaitG Date: Mon, 19 Mar 2018 20:45:02 -0500 Subject: [PATCH 03/23] DOC: Add examples to docstring of DataFrame.ge() and .gt() --- pandas/core/ops.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/core/ops.py b/pandas/core/ops.py index b725f3a956657..c725c3fb62da1 100644 --- a/pandas/core/ops.py +++ b/pandas/core/ops.py @@ -1620,8 +1620,8 @@ def na_op(x, y): if op_name in _op_descriptions: op_desc = _op_descriptions[op_name] - if op_desc['df_examples'] is not None: - base_doc = _flex_comp_doc_FRAME + if op_desc['df_examples'] is not None: + base_doc = _flex_comp_doc_FRAME doc = base_doc.format(name=op_name, df_examples=op_desc['df_examples']) From 4668c5fc824aa2df67860899b09e27cced225aac Mon Sep 17 00:00:00 2001 From: ParfaitG Date: Sun, 22 Jul 2018 15:20:50 -0500 Subject: [PATCH 04/23] DOC: Update ops.py to add docstring, parameters, and examples to comparison operators --- pandas/core/ops.py | 263 +++++++++++++++++++++++++++++++++------------ 1 file changed, 195 insertions(+), 68 deletions(-) diff --git a/pandas/core/ops.py b/pandas/core/ops.py index c725c3fb62da1..8908f232ccd0d 100644 --- a/pandas/core/ops.py +++ b/pandas/core/ops.py @@ -397,64 +397,167 @@ def _get_op_name(op, special): e NaN -2.0 """ +_eq_example_FRAME = """ +>>> df1 = pd.DataFrame({'tool': ['python', 'r', 'julia'], +... 'score': [100, 200, 300]}, +... columns=['tool', 'score']) +>>> df1 + tool score +0 python 100 +1 r 200 +2 julia 300 +>>> df2 = pd.DataFrame({'tool': ['python', 'r', 'julia'], +... 'score': [300, 200, 100]}, +... columns=['tool', 'score']) +>>> df2 + tool score +0 python 300 +1 r 200 +2 julia 100 +>>> df1.eq(df2) + tool score +0 True False +1 True True +2 True False +""" + +_ne_example_FRAME = """ +>>> df1 = pd.DataFrame({'tool': ['python', 'r', 'julia'], +... 'score': [100, 200, 300]}, +... columns=['tool', 'score']) +>>> df1 + tool score +0 python 100 +1 r 200 +2 julia 300 +>>> df2 = pd.DataFrame({'tool': ['python', 'r', 'julia'], +... 'score': [300, 200, 100]}, +... columns=['tool', 'score']) +>>> df2 + tool score +0 python 300 +1 r 200 +2 julia 100 +>>> df1.ne(df2) + tool score +0 False True +1 False False +2 False True +""" + +_lt_example_FRAME = """ +>>> df1 = pd.DataFrame({'tool': ['python', 'r', 'julia'], +... 'score': [100, 200, 300]}, +... columns=['tool', 'score']) +>>> df1 + tool score +0 python 100 +1 r 200 +2 julia 300 +>>> df2 = pd.DataFrame({'tool': ['python', 'r', 'julia'], +... 'score': [300, 200, 100]}, +... columns=['tool', 'score']) +>>> df2 + tool score +0 python 300 +1 r 200 +2 julia 100 +>>> df1.lt(df2) + tool score +0 False True +1 False False +2 False False +""" + +_le_example_FRAME = """ +>>> df1 = pd.DataFrame({'tool': ['python', 'r', 'julia'], +... 'score': [100, 200, 300]}, +... columns=['tool', 'score']) +>>> df1 + tool score +0 python 100 +1 r 200 +2 julia 300 +>>> df2 = pd.DataFrame({'tool': ['python', 'r', 'julia'], +... 'score': [300, 200, 100]}, +... columns=['tool', 'score']) +>>> df2 + tool score +0 python 300 +1 r 200 +2 julia 100 +>>> df1.le(df2) + tool score +0 True True +1 True True +2 True False +""" + _gt_example_FRAME = """ ->>> df1 = pd.DataFrame({'num1': range(1,6), -... 'num2': range(2,11,2), -... 'num3': range(1,20,4)}) +>>> df1 = pd.DataFrame({'tool': ['python', 'r', 'julia'], +... 'score': [100, 200, 300]}, +... columns=['tool', 'score']) >>> df1 - num1 num2 num3 -0 1 2 1 -1 2 4 5 -2 3 6 9 -3 4 8 13 -4 5 10 17 ->>> df2 = pd.DataFrame({'num1': range(6,11), -... 'num2': range(1,10,2), -... 'num3': range(1,20,4)}) + tool score +0 python 100 +1 r 200 +2 julia 300 +>>> df2 = pd.DataFrame({'tool': ['python', 'r', 'julia'], +... 'score': [300, 200, 100]}, +... columns=['tool', 'score']) >>> df2 - num1 num2 num3 -0 6 1 1 -1 7 3 5 -2 8 5 9 -3 9 7 13 -4 10 9 17 + tool score +0 python 300 +1 r 200 +2 julia 100 >>> df1.gt(df2) - num1 num2 num3 -0 False True False -1 False True False -2 False True False -3 False True False -4 False True False + tool score +0 False False +1 False False +2 False True """ _ge_example_FRAME = """ ->>> df1 = pd.DataFrame({'num1': range(1,6), -... 'num2': range(2,11,2), -... 'num3': range(1,20,4)}) +>>> df1 = pd.DataFrame({'tool': ['python', 'r', 'julia'], +... 'score': [100, 200, 300]}, +... columns=['tool', 'score']) >>> df1 - num1 num2 num3 -0 1 2 1 -1 2 4 5 -2 3 6 9 -3 4 8 13 -4 5 10 17 ->>> df2 = pd.DataFrame({'num1': range(6,11), -... 'num2': range(1,10,2), -... 'num3': range(1,20,4)}) + tool score +0 python 100 +1 r 200 +2 julia 300 +>>> df2 = pd.DataFrame({'tool': ['python', 'r', 'julia'], +... 'score': [300, 200, 100]}, +... columns=['tool', 'score']) >>> df2 - num1 num2 num3 -0 6 1 1 -1 7 3 5 -2 8 5 9 -3 9 7 13 -4 10 9 17 + tool score +0 python 300 +1 r 200 +2 julia 100 >>> df1.ge(df2) - num1 num2 num3 -0 False True True -1 False True True -2 False True True -3 False True True -4 False True True + tool score +0 True False +1 True True +2 True True +""" + +_comp_others = """ +DataFrame.eq : Return DataFrame of boolean values equal to the + elementwise rows or columns of one DataFrame to another +DataFrame.ne : Return DataFrame of boolean values not equal to + the elementwise rows or columns of one DataFrame to another +DataFrame.le : Return DataFrame of boolean values less than or + equal the elementwise rows or columns of one DataFrame + to another +DataFrame.lt : Return DataFrame of boolean values strictly less + than the elementwise rows or columns of one DataFrame to + another +DataFrame.ge : Return DataFrame of boolean values greater than or + equal to the elementwise rows or columns of one DataFrame to + another +DataFrame.gt : Return DataFrame of boolean values strictly greater + than to the elementwise rows or columns of one DataFrame to + another """ _op_descriptions = { @@ -495,28 +598,34 @@ def _get_op_name(op, special): # Comparison Operators 'eq': {'op': '==', 'desc': 'Equal to', - 'reverse': None, - 'df_examples': None}, + 'reverse': 'ne', + 'df_examples': _eq_example_FRAME, + 'others': _comp_others}, 'ne': {'op': '!=', 'desc': 'Not equal to', - 'reverse': None, - 'df_examples': None}, + 'reverse': 'eq', + 'df_examples': _ne_example_FRAME, + 'others': _comp_others}, 'lt': {'op': '<', 'desc': 'Less than', - 'reverse': None, - 'df_examples': None}, + 'reverse': 'ge', + 'df_examples': _lt_example_FRAME, + 'others': _comp_others}, 'le': {'op': '<=', 'desc': 'Less than or equal to', - 'reverse': None, - 'df_examples': None}, + 'reverse': 'gt', + 'df_examples': _le_example_FRAME, + 'others': _comp_others}, 'gt': {'op': '>', 'desc': 'Greater than', - 'reverse': None, - 'df_examples': _gt_example_FRAME}, + 'reverse': 'le', + 'df_examples': _gt_example_FRAME, + 'others': _comp_others}, 'ge': {'op': '>=', 'desc': 'Greater than or equal to', - 'reverse': None, - 'df_examples': _ge_example_FRAME}} + 'reverse': 'lt', + 'df_examples': _ge_example_FRAME, + 'others': _comp_others}} _op_names = list(_op_descriptions.keys()) for key in _op_names: @@ -643,11 +752,32 @@ def _get_op_name(op, special): """ _flex_comp_doc_FRAME = """ -Wrapper for flexible comparison methods {name} +Flexible wrappers to comparison operators (specifically ``{name}``). + +Wrappers (``eq``, ``ne``, ``le``, ``lt``, ``ge``, ``gt``) are equivalent to +operators (``==``, ``=!``, ``<=``, ``<``, ``>=``, ``>``) with support to choose +axis (rows or columns) for comparison. + +Parameters +---------- +other : DataFrame +axis : {{0, 1, 'columns', 'rows'}} +level : int or name + Broadcast across a level, matching Index values on the + passed MultiIndex level + +Returns +------- +result : DataFrame + Consisting of boolean values Examples -------- {df_examples} + +See also +-------- +{reverse} """ _flex_doc_PANEL = """ @@ -1614,16 +1744,13 @@ def na_op(x, y): result = mask_cmp_op(x, y, op, (np.ndarray, ABCSeries)) return result - doc = ('Wrapper for flexible comparison methods {name}' - .format(name=op_name)) - if op_name in _op_descriptions: op_desc = _op_descriptions[op_name] - if op_desc['df_examples'] is not None: - base_doc = _flex_comp_doc_FRAME - doc = base_doc.format(name=op_name, - df_examples=op_desc['df_examples']) + base_doc = _flex_comp_doc_FRAME + doc = base_doc.format(name=op_name, + df_examples=op_desc['df_examples'].strip(), + reverse=op_desc['others'].strip()) @Appender(doc) def f(self, other, axis=default_axis, level=None): From e6eb9b97d08da7e07b0020b9b4487967ef009eff Mon Sep 17 00:00:00 2001 From: ParfaitG Date: Sun, 22 Jul 2018 15:36:22 -0500 Subject: [PATCH 05/23] DOC: Update ops.py for operator methods - cleaning up whitespace --- pandas/core/ops.py | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/pandas/core/ops.py b/pandas/core/ops.py index 8908f232ccd0d..8329478c3c241 100644 --- a/pandas/core/ops.py +++ b/pandas/core/ops.py @@ -544,19 +544,19 @@ def _get_op_name(op, special): _comp_others = """ DataFrame.eq : Return DataFrame of boolean values equal to the elementwise rows or columns of one DataFrame to another -DataFrame.ne : Return DataFrame of boolean values not equal to +DataFrame.ne : Return DataFrame of boolean values not equal to the elementwise rows or columns of one DataFrame to another -DataFrame.le : Return DataFrame of boolean values less than or - equal the elementwise rows or columns of one DataFrame +DataFrame.le : Return DataFrame of boolean values less than or + equal the elementwise rows or columns of one DataFrame to another -DataFrame.lt : Return DataFrame of boolean values strictly less - than the elementwise rows or columns of one DataFrame to +DataFrame.lt : Return DataFrame of boolean values strictly less + than the elementwise rows or columns of one DataFrame to another -DataFrame.ge : Return DataFrame of boolean values greater than or - equal to the elementwise rows or columns of one DataFrame to +DataFrame.ge : Return DataFrame of boolean values greater than or + equal to the elementwise rows or columns of one DataFrame to another -DataFrame.gt : Return DataFrame of boolean values strictly greater - than to the elementwise rows or columns of one DataFrame to +DataFrame.gt : Return DataFrame of boolean values strictly greater + than to the elementwise rows or columns of one DataFrame to another """ @@ -754,8 +754,8 @@ def _get_op_name(op, special): _flex_comp_doc_FRAME = """ Flexible wrappers to comparison operators (specifically ``{name}``). -Wrappers (``eq``, ``ne``, ``le``, ``lt``, ``ge``, ``gt``) are equivalent to -operators (``==``, ``=!``, ``<=``, ``<``, ``>=``, ``>``) with support to choose +Wrappers (``eq``, ``ne``, ``le``, ``lt``, ``ge``, ``gt``) are equivalent to +operators (``==``, ``=!``, ``<=``, ``<``, ``>=``, ``>``) with support to choose axis (rows or columns) for comparison. Parameters @@ -765,7 +765,7 @@ def _get_op_name(op, special): level : int or name Broadcast across a level, matching Index values on the passed MultiIndex level - + Returns ------- result : DataFrame @@ -1747,7 +1747,7 @@ def na_op(x, y): if op_name in _op_descriptions: op_desc = _op_descriptions[op_name] - base_doc = _flex_comp_doc_FRAME + base_doc = _flex_comp_doc_FRAME doc = base_doc.format(name=op_name, df_examples=op_desc['df_examples'].strip(), reverse=op_desc['others'].strip()) From db143c4fac8c95fc3460a7c285e9b148a910ca88 Mon Sep 17 00:00:00 2001 From: ParfaitG Date: Sun, 29 Jul 2018 19:18:27 -0500 Subject: [PATCH 06/23] DOC: Update ops.py to extend docstrings for comparison methods --- pandas/core/ops.py | 286 +++++++++++++++++++++++---------------------- 1 file changed, 146 insertions(+), 140 deletions(-) diff --git a/pandas/core/ops.py b/pandas/core/ops.py index 8329478c3c241..910240fbd4f8c 100644 --- a/pandas/core/ops.py +++ b/pandas/core/ops.py @@ -398,166 +398,166 @@ def _get_op_name(op, special): """ _eq_example_FRAME = """ ->>> df1 = pd.DataFrame({'tool': ['python', 'r', 'julia'], -... 'score': [100, 200, 300]}, -... columns=['tool', 'score']) +>>> df1 = pd.DataFrame({'company': ['A', 'B', 'C'], +... 'cost': [250, 150, 100], +... 'revenue': [100, 250, 300]}) >>> df1 - tool score -0 python 100 -1 r 200 -2 julia 300 ->>> df2 = pd.DataFrame({'tool': ['python', 'r', 'julia'], -... 'score': [300, 200, 100]}, -... columns=['tool', 'score']) + company cost revenue +0 A 250 100 +1 B 150 250 +2 C 100 300 +>>> df2 = pd.DataFrame({'company': ['A', 'B', 'C', 'D'], +... 'revenue': [300, 250, 100, 150]}) >>> df2 - tool score -0 python 300 -1 r 200 -2 julia 100 + company revenue +0 A 300 +1 B 250 +2 C 100 +3 D 150 >>> df1.eq(df2) - tool score -0 True False -1 True True -2 True False + company cost revenue +0 True False False +1 True False True +2 True False False +3 False False False """ _ne_example_FRAME = """ ->>> df1 = pd.DataFrame({'tool': ['python', 'r', 'julia'], -... 'score': [100, 200, 300]}, -... columns=['tool', 'score']) +>>> df1 = pd.DataFrame({'company': ['A', 'B', 'C'], +... 'cost': [250, 150, 100], +... 'revenue': [100, 250, 300]}) >>> df1 - tool score -0 python 100 -1 r 200 -2 julia 300 ->>> df2 = pd.DataFrame({'tool': ['python', 'r', 'julia'], -... 'score': [300, 200, 100]}, -... columns=['tool', 'score']) + company cost revenue +0 A 250 100 +1 B 150 250 +2 C 100 300 +>>> df2 = pd.DataFrame({'company': ['A', 'B', 'C', 'D'], +... 'revenue': [300, 250, 100, 150]}) >>> df2 - tool score -0 python 300 -1 r 200 -2 julia 100 + company revenue +0 A 300 +1 B 250 +2 C 100 +3 D 150 >>> df1.ne(df2) - tool score -0 False True -1 False False -2 False True + company cost revenue +0 False True True +1 False True False +2 False True True +3 True True True """ _lt_example_FRAME = """ ->>> df1 = pd.DataFrame({'tool': ['python', 'r', 'julia'], -... 'score': [100, 200, 300]}, -... columns=['tool', 'score']) +>>> df1 = pd.DataFrame({'company': ['A', 'B', 'C'], +... 'cost': [250, 150, 100], +... 'revenue': [100, 250, 300]}) >>> df1 - tool score -0 python 100 -1 r 200 -2 julia 300 ->>> df2 = pd.DataFrame({'tool': ['python', 'r', 'julia'], -... 'score': [300, 200, 100]}, -... columns=['tool', 'score']) + company cost revenue +0 A 250 100 +1 B 150 250 +2 C 100 300 +>>> df2 = pd.DataFrame({'company': ['A', 'B', 'C', 'D'], +... 'revenue': [300, 250, 100, 150]}) >>> df2 - tool score -0 python 300 -1 r 200 -2 julia 100 + company revenue +0 A 300 +1 B 250 +2 C 100 +3 D 150 >>> df1.lt(df2) - tool score -0 False True -1 False False -2 False False + company cost revenue +0 False False True +1 False False False +2 False False False +3 False False False """ _le_example_FRAME = """ ->>> df1 = pd.DataFrame({'tool': ['python', 'r', 'julia'], -... 'score': [100, 200, 300]}, -... columns=['tool', 'score']) +>>> df1 = pd.DataFrame({'company': ['A', 'B', 'C'], +... 'cost': [250, 150, 100], +... 'revenue': [100, 250, 300]}) >>> df1 - tool score -0 python 100 -1 r 200 -2 julia 300 ->>> df2 = pd.DataFrame({'tool': ['python', 'r', 'julia'], -... 'score': [300, 200, 100]}, -... columns=['tool', 'score']) + company cost revenue +0 A 250 100 +1 B 150 250 +2 C 100 300 +>>> df2 = pd.DataFrame({'company': ['A', 'B', 'C', 'D'], +... 'revenue': [300, 250, 100, 150]}) >>> df2 - tool score -0 python 300 -1 r 200 -2 julia 100 + company revenue +0 A 300 +1 B 250 +2 C 100 +3 D 150 >>> df1.le(df2) - tool score -0 True True -1 True True -2 True False + company cost revenue +0 True False True +1 True False True +2 True False False +3 False False False """ _gt_example_FRAME = """ ->>> df1 = pd.DataFrame({'tool': ['python', 'r', 'julia'], -... 'score': [100, 200, 300]}, -... columns=['tool', 'score']) +>>> df1 = pd.DataFrame({'company': ['A', 'B', 'C'], +... 'cost': [250, 150, 100], +... 'revenue': [100, 250, 300]}) >>> df1 - tool score -0 python 100 -1 r 200 -2 julia 300 ->>> df2 = pd.DataFrame({'tool': ['python', 'r', 'julia'], -... 'score': [300, 200, 100]}, -... columns=['tool', 'score']) + company cost revenue +0 A 250 100 +1 B 150 250 +2 C 100 300 +>>> df2 = pd.DataFrame({'company': ['A', 'B', 'C', 'D'], +... 'revenue': [300, 250, 100, 150]}) >>> df2 - tool score -0 python 300 -1 r 200 -2 julia 100 + company revenue +0 A 300 +1 B 250 +2 C 100 +3 D 150 >>> df1.gt(df2) - tool score -0 False False -1 False False -2 False True + company cost revenue +0 False False False +1 False False False +2 False False True +3 False False False """ _ge_example_FRAME = """ ->>> df1 = pd.DataFrame({'tool': ['python', 'r', 'julia'], -... 'score': [100, 200, 300]}, -... columns=['tool', 'score']) +>>> df1 = pd.DataFrame({'company': ['A', 'B', 'C'], +... 'cost': [250, 150, 100], +... 'revenue': [100, 250, 300]}) >>> df1 - tool score -0 python 100 -1 r 200 -2 julia 300 ->>> df2 = pd.DataFrame({'tool': ['python', 'r', 'julia'], -... 'score': [300, 200, 100]}, -... columns=['tool', 'score']) + company cost revenue +0 A 250 100 +1 B 150 250 +2 C 100 300 +>>> df2 = pd.DataFrame({'company': ['A', 'B', 'C', 'D'], +... 'revenue': [300, 250, 100, 150]}) >>> df2 - tool score -0 python 300 -1 r 200 -2 julia 100 + company revenue +0 A 300 +1 B 250 +2 C 100 +3 D 150 >>> df1.ge(df2) - tool score -0 True False -1 True True -2 True True + company cost revenue +0 True False False +1 True False True +2 True False True +3 False False False """ _comp_others = """ -DataFrame.eq : Return DataFrame of boolean values equal to the - elementwise rows or columns of one DataFrame to another -DataFrame.ne : Return DataFrame of boolean values not equal to - the elementwise rows or columns of one DataFrame to another -DataFrame.le : Return DataFrame of boolean values less than or - equal the elementwise rows or columns of one DataFrame - to another -DataFrame.lt : Return DataFrame of boolean values strictly less - than the elementwise rows or columns of one DataFrame to - another -DataFrame.ge : Return DataFrame of boolean values greater than or - equal to the elementwise rows or columns of one DataFrame to - another -DataFrame.gt : Return DataFrame of boolean values strictly greater - than to the elementwise rows or columns of one DataFrame to - another +DataFrame.eq : Compare DataFrames for equality elementwise +DataFrame.ne : Compare DataFrames for inequality elementwise +DataFrame.le : Compare DataFrames for less than inequality + or equality elementwise +DataFrame.lt : Compare DataFrames for strictly less than + inequality elementwise +DataFrame.ge : Compare DataFrames for greater than inequality + or equality elementwise +DataFrame.gt : Compare DataFrames for strictly greater than + inequality elementwise """ _op_descriptions = { @@ -598,17 +598,17 @@ def _get_op_name(op, special): # Comparison Operators 'eq': {'op': '==', 'desc': 'Equal to', - 'reverse': 'ne', + 'reverse': None, 'df_examples': _eq_example_FRAME, 'others': _comp_others}, 'ne': {'op': '!=', 'desc': 'Not equal to', - 'reverse': 'eq', + 'reverse': None, 'df_examples': _ne_example_FRAME, 'others': _comp_others}, 'lt': {'op': '<', 'desc': 'Less than', - 'reverse': 'ge', + 'reverse': None, 'df_examples': _lt_example_FRAME, 'others': _comp_others}, 'le': {'op': '<=', @@ -618,12 +618,12 @@ def _get_op_name(op, special): 'others': _comp_others}, 'gt': {'op': '>', 'desc': 'Greater than', - 'reverse': 'le', + 'reverse': None, 'df_examples': _gt_example_FRAME, 'others': _comp_others}, 'ge': {'op': '>=', 'desc': 'Greater than or equal to', - 'reverse': 'lt', + 'reverse': None, 'df_examples': _ge_example_FRAME, 'others': _comp_others}} @@ -754,30 +754,36 @@ def _get_op_name(op, special): _flex_comp_doc_FRAME = """ Flexible wrappers to comparison operators (specifically ``{name}``). -Wrappers (``eq``, ``ne``, ``le``, ``lt``, ``ge``, ``gt``) are equivalent to -operators (``==``, ``=!``, ``<=``, ``<``, ``>=``, ``>``) with support to choose +Equivalent to `==`, `=!`, `<=`, `<`, `>=`, `>` with support to choose axis (rows or columns) for comparison. Parameters ---------- other : DataFrame -axis : {{0, 1, 'columns', 'rows'}} + Any structured DataFrame. Can be different number of columns or rows. +axis : int or str, optional + Axis to target. Can be either the axis name ('index', 'rows', + 'columns') or number (0, 1). level : int or name - Broadcast across a level, matching Index values on the - passed MultiIndex level + Broadcast across a level, matching Index values on the passed + MultiIndex level. Returns ------- -result : DataFrame - Consisting of boolean values - -Examples --------- -{df_examples} +result : DataFrame of bool + Result of the comparison. See also -------- {reverse} + +Notes +-------- +Mismatched indices will be unioned together. + +Examples +-------- +{df_examples} """ _flex_doc_PANEL = """ From 33ff1e443a0621ca3ac146fcb4812d6780e037a1 Mon Sep 17 00:00:00 2001 From: ParfaitG Date: Sun, 5 Aug 2018 15:50:38 -0500 Subject: [PATCH 07/23] DOC: Create single, generalized docstring for comparison methods --- pandas/core/ops.py | 317 +++++++++++++++++++-------------------------- 1 file changed, 130 insertions(+), 187 deletions(-) diff --git a/pandas/core/ops.py b/pandas/core/ops.py index 910240fbd4f8c..ec7702a5c4e6a 100644 --- a/pandas/core/ops.py +++ b/pandas/core/ops.py @@ -397,169 +397,6 @@ def _get_op_name(op, special): e NaN -2.0 """ -_eq_example_FRAME = """ ->>> df1 = pd.DataFrame({'company': ['A', 'B', 'C'], -... 'cost': [250, 150, 100], -... 'revenue': [100, 250, 300]}) ->>> df1 - company cost revenue -0 A 250 100 -1 B 150 250 -2 C 100 300 ->>> df2 = pd.DataFrame({'company': ['A', 'B', 'C', 'D'], -... 'revenue': [300, 250, 100, 150]}) ->>> df2 - company revenue -0 A 300 -1 B 250 -2 C 100 -3 D 150 ->>> df1.eq(df2) - company cost revenue -0 True False False -1 True False True -2 True False False -3 False False False -""" - -_ne_example_FRAME = """ ->>> df1 = pd.DataFrame({'company': ['A', 'B', 'C'], -... 'cost': [250, 150, 100], -... 'revenue': [100, 250, 300]}) ->>> df1 - company cost revenue -0 A 250 100 -1 B 150 250 -2 C 100 300 ->>> df2 = pd.DataFrame({'company': ['A', 'B', 'C', 'D'], -... 'revenue': [300, 250, 100, 150]}) ->>> df2 - company revenue -0 A 300 -1 B 250 -2 C 100 -3 D 150 ->>> df1.ne(df2) - company cost revenue -0 False True True -1 False True False -2 False True True -3 True True True -""" - -_lt_example_FRAME = """ ->>> df1 = pd.DataFrame({'company': ['A', 'B', 'C'], -... 'cost': [250, 150, 100], -... 'revenue': [100, 250, 300]}) ->>> df1 - company cost revenue -0 A 250 100 -1 B 150 250 -2 C 100 300 ->>> df2 = pd.DataFrame({'company': ['A', 'B', 'C', 'D'], -... 'revenue': [300, 250, 100, 150]}) ->>> df2 - company revenue -0 A 300 -1 B 250 -2 C 100 -3 D 150 ->>> df1.lt(df2) - company cost revenue -0 False False True -1 False False False -2 False False False -3 False False False -""" - -_le_example_FRAME = """ ->>> df1 = pd.DataFrame({'company': ['A', 'B', 'C'], -... 'cost': [250, 150, 100], -... 'revenue': [100, 250, 300]}) ->>> df1 - company cost revenue -0 A 250 100 -1 B 150 250 -2 C 100 300 ->>> df2 = pd.DataFrame({'company': ['A', 'B', 'C', 'D'], -... 'revenue': [300, 250, 100, 150]}) ->>> df2 - company revenue -0 A 300 -1 B 250 -2 C 100 -3 D 150 ->>> df1.le(df2) - company cost revenue -0 True False True -1 True False True -2 True False False -3 False False False -""" - -_gt_example_FRAME = """ ->>> df1 = pd.DataFrame({'company': ['A', 'B', 'C'], -... 'cost': [250, 150, 100], -... 'revenue': [100, 250, 300]}) ->>> df1 - company cost revenue -0 A 250 100 -1 B 150 250 -2 C 100 300 ->>> df2 = pd.DataFrame({'company': ['A', 'B', 'C', 'D'], -... 'revenue': [300, 250, 100, 150]}) ->>> df2 - company revenue -0 A 300 -1 B 250 -2 C 100 -3 D 150 ->>> df1.gt(df2) - company cost revenue -0 False False False -1 False False False -2 False False True -3 False False False -""" - -_ge_example_FRAME = """ ->>> df1 = pd.DataFrame({'company': ['A', 'B', 'C'], -... 'cost': [250, 150, 100], -... 'revenue': [100, 250, 300]}) ->>> df1 - company cost revenue -0 A 250 100 -1 B 150 250 -2 C 100 300 ->>> df2 = pd.DataFrame({'company': ['A', 'B', 'C', 'D'], -... 'revenue': [300, 250, 100, 150]}) ->>> df2 - company revenue -0 A 300 -1 B 250 -2 C 100 -3 D 150 ->>> df1.ge(df2) - company cost revenue -0 True False False -1 True False True -2 True False True -3 False False False -""" - -_comp_others = """ -DataFrame.eq : Compare DataFrames for equality elementwise -DataFrame.ne : Compare DataFrames for inequality elementwise -DataFrame.le : Compare DataFrames for less than inequality - or equality elementwise -DataFrame.lt : Compare DataFrames for strictly less than - inequality elementwise -DataFrame.ge : Compare DataFrames for greater than inequality - or equality elementwise -DataFrame.gt : Compare DataFrames for strictly greater than - inequality elementwise -""" - _op_descriptions = { # Arithmetic Operators 'add': {'op': '+', @@ -599,33 +436,33 @@ def _get_op_name(op, special): 'eq': {'op': '==', 'desc': 'Equal to', 'reverse': None, - 'df_examples': _eq_example_FRAME, - 'others': _comp_others}, + 'df_examples': None, + 'others': None}, 'ne': {'op': '!=', 'desc': 'Not equal to', 'reverse': None, - 'df_examples': _ne_example_FRAME, - 'others': _comp_others}, + 'df_examples': None, + 'others': None}, 'lt': {'op': '<', 'desc': 'Less than', 'reverse': None, - 'df_examples': _lt_example_FRAME, - 'others': _comp_others}, + 'df_examples': None, + 'others': None}, 'le': {'op': '<=', 'desc': 'Less than or equal to', 'reverse': 'gt', - 'df_examples': _le_example_FRAME, - 'others': _comp_others}, + 'df_examples': None, + 'others': None}, 'gt': {'op': '>', 'desc': 'Greater than', 'reverse': None, - 'df_examples': _gt_example_FRAME, - 'others': _comp_others}, + 'df_examples': None, + 'others': None}, 'ge': {'op': '>=', 'desc': 'Greater than or equal to', 'reverse': None, - 'df_examples': _ge_example_FRAME, - 'others': _comp_others}} + 'df_examples': None, + 'others': None}} _op_names = list(_op_descriptions.keys()) for key in _op_names: @@ -752,15 +589,15 @@ def _get_op_name(op, special): """ _flex_comp_doc_FRAME = """ -Flexible wrappers to comparison operators (specifically ``{name}``). +Flexible wrappers to comparison operators (`eq`, `ne`, `le`, `lt`, `ge`, `gt`). Equivalent to `==`, `=!`, `<=`, `<`, `>=`, `>` with support to choose -axis (rows or columns) for comparison. +axis (rows or columns) and level for comparison. Parameters ---------- -other : DataFrame - Any structured DataFrame. Can be different number of columns or rows. +other : constant, list, tuple, ndarray, Series, or DataFrame + Any single or multiple element data structure, or list-like object. axis : int or str, optional Axis to target. Can be either the axis name ('index', 'rows', 'columns') or number (0, 1). @@ -775,7 +612,16 @@ def _get_op_name(op, special): See also -------- -{reverse} +DataFrame.eq : Compare DataFrames for equality elementwise +DataFrame.ne : Compare DataFrames for inequality elementwise +DataFrame.le : Compare DataFrames for less than inequality + or equality elementwise +DataFrame.lt : Compare DataFrames for strictly less than + inequality elementwise +DataFrame.ge : Compare DataFrames for greater than inequality + or equality elementwise +DataFrame.gt : Compare DataFrames for strictly greater than + inequality elementwise Notes -------- @@ -783,7 +629,107 @@ def _get_op_name(op, special): Examples -------- -{df_examples} +>>> df1 = pd.DataFrame({'company': ['A', 'B', 'C'], +... 'cost': [250, 150, 100], +... 'revenue': [100, 250, 300]}) +>>> df1 + company cost revenue +0 A 250 100 +1 B 150 250 +2 C 100 300 +>>> df2 = pd.DataFrame({'company': ['A', 'B', 'C', 'D'], +... 'revenue': [300, 250, 100, 150]}) +>>> df2 + company revenue +0 A 300 +1 B 250 +2 C 100 +3 D 150 +>>> df3 = pd.DataFrame({'cost': [250, 150, 100, 150, 300, 220], +... 'revenue': [100, 250, 300, 200, 175, 225]}, +... index = [['Q1', 'Q1', 'Q1', 'Q2', 'Q2', 'Q2'], +... ['A', 'B', 'C', 'A', 'B' ,'C']]) +>>> df3 + cost revenue +Q1 A 250 100 + B 150 250 + C 100 300 +Q2 A 150 200 + B 300 175 + C 220 225 + +Compare to a constant and operator version + +>>> df1 == 100 + company cost revenue +0 False False True +1 False False False +2 False True False +>>> df1.eq(100) + company cost revenue +0 False False True +1 False False False +2 False True False + +Compare to a Series by axis and operator version + +>>> df1 != [100, 250, 300] + company cost revenue +0 True True False +1 True True False +2 True True False +>>> df1.ne([100, 250, 300], axis=0) + company cost revenue +0 True True False +1 True True False +2 True True False +>>> df1 != pd.Series([100, 250, 300]) + company cost revenue 0 1 2 +0 True True True True True True +1 True True True True True True +2 True True True True True True +>>> df1.ne(pd.Series([100, 250, 300]), axis=1) + company cost revenue 0 1 2 +0 True True True True True True +1 True True True True True True +2 True True True True True True + +Compare to a DataFrame by axis and operator version + +>>> df1.reindex(['company', 'revenue'], axis='columns') > df2.iloc[:-1] + company revenue +0 False False +1 False False +2 False True +>>> df1.gt(df2, axis=0) + company cost revenue +0 False False False +1 False False False +2 False False True +3 False False False +>>> df1.gt(df2, axis=1) + company cost revenue +0 False False False +1 False False False +2 False False True +3 False False False + +Compare to a MultiIndex by level and operator version + +>>> df1.set_index('company') <= df3.loc['Q1'] + cost revenue +company +A True True +B True True +C True True +>>> df1.set_index('company').le(df3, level=1) + cost revenue +Q1 A True True + B True True + C True True +Q2 A False True + B True False + C True False """ _flex_doc_PANEL = """ @@ -1751,12 +1697,9 @@ def na_op(x, y): return result if op_name in _op_descriptions: - op_desc = _op_descriptions[op_name] - - base_doc = _flex_comp_doc_FRAME - doc = base_doc.format(name=op_name, - df_examples=op_desc['df_examples'].strip(), - reverse=op_desc['others'].strip()) + doc = _flex_comp_doc_FRAME + else: + doc = "Flexible wrappers to comparison methods" @Appender(doc) def f(self, other, axis=default_axis, level=None): From e138d92152fca5364215837a8fb15e557ad4e443 Mon Sep 17 00:00:00 2001 From: ParfaitG Date: Sun, 12 Aug 2018 10:11:00 -0500 Subject: [PATCH 08/23] DOC: Examples and summary updates to comparison operators --- pandas/core/ops.py | 126 ++++++++++++++++++++++----------------------- 1 file changed, 61 insertions(+), 65 deletions(-) diff --git a/pandas/core/ops.py b/pandas/core/ops.py index ec7702a5c4e6a..c98090cf9cc89 100644 --- a/pandas/core/ops.py +++ b/pandas/core/ops.py @@ -435,34 +435,23 @@ def _get_op_name(op, special): # Comparison Operators 'eq': {'op': '==', 'desc': 'Equal to', - 'reverse': None, - 'df_examples': None, - 'others': None}, + 'reverse': None}, 'ne': {'op': '!=', 'desc': 'Not equal to', - 'reverse': None, - 'df_examples': None, - 'others': None}, + 'reverse': None}, 'lt': {'op': '<', 'desc': 'Less than', - 'reverse': None, - 'df_examples': None, - 'others': None}, + 'reverse': None}, 'le': {'op': '<=', 'desc': 'Less than or equal to', - 'reverse': 'gt', - 'df_examples': None, - 'others': None}, + 'reverse': None}, 'gt': {'op': '>', 'desc': 'Greater than', - 'reverse': None, - 'df_examples': None, - 'others': None}, + 'reverse': None}, 'ge': {'op': '>=', 'desc': 'Greater than or equal to', - 'reverse': None, - 'df_examples': None, - 'others': None}} + 'reverse': None} +} _op_names = list(_op_descriptions.keys()) for key in _op_names: @@ -589,28 +578,31 @@ def _get_op_name(op, special): """ _flex_comp_doc_FRAME = """ -Flexible wrappers to comparison operators (`eq`, `ne`, `le`, `lt`, `ge`, `gt`). +{desc} of dataframe and other, element-wise (binary operator `{op_name}`). + +Among flexible wrappers (`eq`, `ne`, `le`, `lt`, `ge`, `gt`) to comparison +operators. -Equivalent to `==`, `=!`, `<=`, `<`, `>=`, `>` with support to choose -axis (rows or columns) and level for comparison. +Equivalent to `==`, `=!`, `<=`, `<`, `>=`, `>` with support to choose axis +(rows or columns) and level for comparison. Parameters ---------- -other : constant, list, tuple, ndarray, Series, or DataFrame +other : scalar, sequence, Series, or DataFrame Any single or multiple element data structure, or list-like object. axis : int or str, optional Axis to target. Can be either the axis name ('index', 'rows', 'columns') or number (0, 1). -level : int or name +level : int or object Broadcast across a level, matching Index values on the passed MultiIndex level. Returns ------- -result : DataFrame of bool +DataFrame of bool Result of the comparison. -See also +See Also -------- DataFrame.eq : Compare DataFrames for equality elementwise DataFrame.ne : Compare DataFrames for inequality elementwise @@ -629,100 +621,102 @@ def _get_op_name(op, special): Examples -------- ->>> df1 = pd.DataFrame({'company': ['A', 'B', 'C'], -... 'cost': [250, 150, 100], -... 'revenue': [100, 250, 300]}) ->>> df1 +>>> df = pd.DataFrame({{'company': ['A', 'B', 'C'], +... 'cost': [250, 150, 100], +... 'revenue': [100, 250, 300]}}) +>>> df company cost revenue 0 A 250 100 1 B 150 250 2 C 100 300 ->>> df2 = pd.DataFrame({'company': ['A', 'B', 'C', 'D'], -... 'revenue': [300, 250, 100, 150]}) ->>> df2 - company revenue -0 A 300 -1 B 250 -2 C 100 -3 D 150 ->>> df3 = pd.DataFrame({'cost': [250, 150, 100, 150, 300, 220], -... 'revenue': [100, 250, 300, 200, 175, 225]}, -... index = [['Q1', 'Q1', 'Q1', 'Q2', 'Q2', 'Q2'], -... ['A', 'B', 'C', 'A', 'B' ,'C']]) ->>> df3 - cost revenue -Q1 A 250 100 - B 150 250 - C 100 300 -Q2 A 150 200 - B 300 175 - C 220 225 -Compare to a constant and operator version +Compare to a scalar and operator version which return equivalent +results. ->>> df1 == 100 +>>> df == 100 company cost revenue 0 False False True 1 False False False 2 False True False ->>> df1.eq(100) +>>> df.eq(100) company cost revenue 0 False False True 1 False False False 2 False True False -Compare to a Series by axis and operator version +Compare to a list and Series by axis and operator version. ->>> df1 != [100, 250, 300] +>>> df != [100, 250, 300] company cost revenue 0 True True False 1 True True False 2 True True False ->>> df1.ne([100, 250, 300], axis=0) +>>> df.ne([100, 250, 300], axis='index') company cost revenue 0 True True False 1 True True False 2 True True False ->>> df1 != pd.Series([100, 250, 300]) +>>> df != pd.Series([100, 250, 300]) company cost revenue 0 1 2 0 True True True True True True 1 True True True True True True 2 True True True True True True ->>> df1.ne(pd.Series([100, 250, 300]), axis=1) +>>> df.ne(pd.Series([100, 250, 300]), axis='columns') company cost revenue 0 1 2 0 True True True True True True 1 True True True True True True 2 True True True True True True -Compare to a DataFrame by axis and operator version +Compare to a DataFrame of different shape by axis and operator version. ->>> df1.reindex(['company', 'revenue'], axis='columns') > df2.iloc[:-1] +>>> other = pd.DataFrame({{'company': ['A', 'B', 'C', 'D'], +... 'revenue': [300, 250, 100, 150]}}) +>>> other + company revenue +0 A 300 +1 B 250 +2 C 100 +3 D 150 +>>> df.reindex(['company', 'revenue'], axis='columns') > other.iloc[:-1] company revenue 0 False False 1 False False 2 False True ->>> df1.gt(df2, axis=0) +>>> df.gt(other, axis=0) company cost revenue 0 False False False 1 False False False 2 False False True 3 False False False ->>> df1.gt(df2, axis=1) +>>> df.gt(other, axis=1) company cost revenue 0 False False False 1 False False False 2 False False True 3 False False False -Compare to a MultiIndex by level and operator version +Compare to a MultiIndex by level and operator version. + +>>> df_multindex = pd.DataFrame({{'cost': [250, 150, 100, 150, 300, 220], +... 'revenue': [100, 250, 300, 200, 175, 225]}}, +... index = [['Q1', 'Q1', 'Q1', 'Q2', 'Q2', 'Q2'], +... ['A', 'B', 'C', 'A', 'B' ,'C']]) +>>> df_multindex + cost revenue +Q1 A 250 100 + B 150 250 + C 100 300 +Q2 A 150 200 + B 300 175 + C 220 225 ->>> df1.set_index('company') <= df3.loc['Q1'] +>>> df.set_index('company') <= df_multindex.loc['Q1'] cost revenue company A True True B True True C True True ->>> df1.set_index('company').le(df3, level=1) +>>> df.set_index('company').le(df_multindex, level=1) cost revenue Q1 A True True B True True @@ -1697,7 +1691,9 @@ def na_op(x, y): return result if op_name in _op_descriptions: - doc = _flex_comp_doc_FRAME + op_desc = _op_descriptions[op_name] + doc = _flex_comp_doc_FRAME.format(desc=op_desc['desc'], + op_name=op_name) else: doc = "Flexible wrappers to comparison methods" From 50e9d981231de523438d6947330dc7a933d91947 Mon Sep 17 00:00:00 2001 From: ParfaitG Date: Wed, 15 Aug 2018 20:01:34 -0500 Subject: [PATCH 09/23] DOC: further update to parameters and examples for comparison methods --- pandas/core/ops.py | 128 +++++++++++++++++++++------------------------ 1 file changed, 59 insertions(+), 69 deletions(-) diff --git a/pandas/core/ops.py b/pandas/core/ops.py index c98090cf9cc89..69f521425e863 100644 --- a/pandas/core/ops.py +++ b/pandas/core/ops.py @@ -590,9 +590,9 @@ def _get_op_name(op, special): ---------- other : scalar, sequence, Series, or DataFrame Any single or multiple element data structure, or list-like object. -axis : int or str, optional - Axis to target. Can be either the axis name ('index', 'rows', - 'columns') or number (0, 1). +axis : {{0 or 'index', 1 or 'columns'}}, default 'columns' + Whether to compare by the index (0 or 'index') or columns + (1 or 'columns'). level : int or object Broadcast across a level, matching Index values on the passed MultiIndex level. @@ -621,81 +621,77 @@ def _get_op_name(op, special): Examples -------- ->>> df = pd.DataFrame({{'company': ['A', 'B', 'C'], -... 'cost': [250, 150, 100], -... 'revenue': [100, 250, 300]}}) +>>> df = pd.DataFrame({{'cost': [250, 150, 100], +... 'revenue': [100, 250, 300]}}, +... index = ['A', 'B', 'C']) >>> df - company cost revenue -0 A 250 100 -1 B 150 250 -2 C 100 300 + cost revenue +A 250 100 +B 150 250 +C 100 300 -Compare to a scalar and operator version which return equivalent +Compare to a scalar and operator version which return the same results. >>> df == 100 - company cost revenue -0 False False True -1 False False False -2 False True False + cost revenue +A False True +B False False +C True False >>> df.eq(100) - company cost revenue -0 False False True -1 False False False -2 False True False + cost revenue +A False True +B False False +C True False Compare to a list and Series by axis and operator version. >>> df != [100, 250, 300] - company cost revenue -0 True True False -1 True True False -2 True True False + cost revenue +A True False +B True False +C True False >>> df.ne([100, 250, 300], axis='index') - company cost revenue -0 True True False -1 True True False -2 True True False + cost revenue +A True False +B True False +C True False >>> df != pd.Series([100, 250, 300]) - company cost revenue 0 1 2 -0 True True True True True True -1 True True True True True True -2 True True True True True True + cost revenue 0 1 2 +A True True True True True +B True True True True True +C True True True True True >>> df.ne(pd.Series([100, 250, 300]), axis='columns') - company cost revenue 0 1 2 -0 True True True True True True -1 True True True True True True -2 True True True True True True + cost revenue 0 1 2 +A True True True True True +B True True True True True +C True True True True True -Compare to a DataFrame of different shape by axis and operator version. +Compare to a DataFrame of different shape by axis where both 'index' and +'columns' return same results. ->>> other = pd.DataFrame({{'company': ['A', 'B', 'C', 'D'], -... 'revenue': [300, 250, 100, 150]}}) +>>> other = pd.DataFrame({{'revenue': [300, 250, 100, 150]}}, +... index = ['A', 'B', 'C', 'D']) >>> other - company revenue -0 A 300 -1 B 250 -2 C 100 -3 D 150 ->>> df.reindex(['company', 'revenue'], axis='columns') > other.iloc[:-1] - company revenue -0 False False -1 False False -2 False True ->>> df.gt(other, axis=0) - company cost revenue -0 False False False -1 False False False -2 False False True -3 False False False ->>> df.gt(other, axis=1) - company cost revenue -0 False False False -1 False False False -2 False False True -3 False False False - -Compare to a MultiIndex by level and operator version. + revenue +A 300 +B 250 +C 100 +D 150 +>>> df.gt(other, axis='index') + cost revenue +A False False +B False False +C False True +D False False +>>> df.gt(other, axis='columns') + cost revenue +A False False +B False False +C False True +D False False + +Compare to a MultiIndex by level. >>> df_multindex = pd.DataFrame({{'cost': [250, 150, 100, 150, 300, 220], ... 'revenue': [100, 250, 300, 200, 175, 225]}}, @@ -710,13 +706,7 @@ def _get_op_name(op, special): B 300 175 C 220 225 ->>> df.set_index('company') <= df_multindex.loc['Q1'] - cost revenue -company -A True True -B True True -C True True ->>> df.set_index('company').le(df_multindex, level=1) +>>> df.le(df_multindex, level=1) cost revenue Q1 A True True B True True From c2cc0370c9bb05d8fdb7fa44b3aa44966871a10d Mon Sep 17 00:00:00 2001 From: ParfaitG Date: Thu, 16 Aug 2018 22:20:59 -0500 Subject: [PATCH 10/23] DOC: Adjusted notes and examples for comparison methods --- pandas/core/ops.py | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/pandas/core/ops.py b/pandas/core/ops.py index 6379266789701..833b3f4149a89 100644 --- a/pandas/core/ops.py +++ b/pandas/core/ops.py @@ -676,6 +676,7 @@ def _get_op_name(op, special): Notes -------- Mismatched indices will be unioned together. +`NaN` values are considered different (i.e. `NaN` != `NaN`). Examples -------- @@ -725,8 +726,7 @@ def _get_op_name(op, special): B True True True True True C True True True True True -Compare to a DataFrame of different shape by axis where both 'index' and -'columns' return same results. +Compare to a DataFrame of different shape. >>> other = pd.DataFrame({{'revenue': [300, 250, 100, 150]}}, ... index = ['A', 'B', 'C', 'D']) @@ -736,13 +736,7 @@ def _get_op_name(op, special): B 250 C 100 D 150 ->>> df.gt(other, axis='index') - cost revenue -A False False -B False False -C False True -D False False ->>> df.gt(other, axis='columns') +>>> df.gt(other) cost revenue A False False B False False From 644273bff082b1d2d6e9806a11a7cd016cd09fac Mon Sep 17 00:00:00 2001 From: ParfaitG Date: Tue, 21 Aug 2018 19:19:24 -0500 Subject: [PATCH 11/23] DOC: Adjusted _flex_comp_doc_FRAME assignment logic --- pandas/core/ops.py | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/pandas/core/ops.py b/pandas/core/ops.py index 833b3f4149a89..f115b360f5a47 100644 --- a/pandas/core/ops.py +++ b/pandas/core/ops.py @@ -1892,12 +1892,8 @@ def na_op(x, y): result = mask_cmp_op(x, y, op, (np.ndarray, ABCSeries)) return result - if op_name in _op_descriptions: - op_desc = _op_descriptions[op_name] - doc = _flex_comp_doc_FRAME.format(desc=op_desc['desc'], - op_name=op_name) - else: - doc = "Flexible wrappers to comparison methods" + doc = _flex_comp_doc_FRAME.format(op_name=op_name, + desc=_op_descriptions[op_name]['desc']) @Appender(doc) def f(self, other, axis=default_axis, level=None): From 240a5027397f4fde471976001d7b3334d8ea336e Mon Sep 17 00:00:00 2001 From: ParfaitG Date: Wed, 22 Aug 2018 22:28:49 -0500 Subject: [PATCH 12/23] DOC: Extended arithmetic operator docstring to resemble comparison operators --- pandas/core/ops.py | 255 +++++++++++++++++++++++---------------------- 1 file changed, 131 insertions(+), 124 deletions(-) diff --git a/pandas/core/ops.py b/pandas/core/ops.py index f115b360f5a47..ea71b6d1e1f5b 100644 --- a/pandas/core/ops.py +++ b/pandas/core/ops.py @@ -355,124 +355,21 @@ def _get_op_name(op, special): # ----------------------------------------------------------------------------- # Docstring Generation and Templates -_add_example_FRAME = """ ->>> a = pd.DataFrame([1, 1, 1, np.nan], index=['a', 'b', 'c', 'd'], -... columns=['one']) ->>> a - one -a 1.0 -b 1.0 -c 1.0 -d NaN ->>> b = pd.DataFrame(dict(one=[1, np.nan, 1, np.nan], -... two=[np.nan, 2, np.nan, 2]), -... index=['a', 'b', 'd', 'e']) ->>> b - one two -a 1.0 NaN -b NaN 2.0 -d 1.0 NaN -e NaN 2.0 ->>> a.add(b, fill_value=0) - one two -a 2.0 NaN -b 1.0 2.0 -c 1.0 NaN -d 1.0 NaN -e NaN 2.0 -""" - -_sub_example_FRAME = """ ->>> a = pd.DataFrame([2, 1, 1, np.nan], index=['a', 'b', 'c', 'd'], -... columns=['one']) ->>> a - one -a 2.0 -b 1.0 -c 1.0 -d NaN ->>> b = pd.DataFrame(dict(one=[1, np.nan, 1, np.nan], -... two=[3, 2, np.nan, 2]), -... index=['a', 'b', 'd', 'e']) ->>> b - one two -a 1.0 3.0 -b NaN 2.0 -d 1.0 NaN -e NaN 2.0 ->>> a.sub(b, fill_value=0) - one two -a 1.0 -3.0 -b 1.0 -2.0 -c 1.0 NaN -d -1.0 NaN -e NaN -2.0 -""" - -_mod_example_FRAME = """ -**Using a scalar argument** - ->>> df = pd.DataFrame([2, 4, np.nan, 6.2], index=["a", "b", "c", "d"], -... columns=['one']) ->>> df - one -a 2.0 -b 4.0 -c NaN -d 6.2 ->>> df.mod(3, fill_value=-1) - one -a 2.0 -b 1.0 -c 2.0 -d 0.2 - -**Using a DataFrame argument** - ->>> df = pd.DataFrame(dict(one=[np.nan, 2, 3, 14], two=[np.nan, 1, 1, 3]), -... index=['a', 'b', 'c', 'd']) ->>> df - one two -a NaN NaN -b 2.0 1.0 -c 3.0 1.0 -d 14.0 3.0 ->>> other = pd.DataFrame(dict(one=[np.nan, np.nan, 6, np.nan], -... three=[np.nan, 10, np.nan, -7]), -... index=['a', 'b', 'd', 'e']) ->>> other - one three -a NaN NaN -b NaN 10.0 -d 6.0 NaN -e NaN -7.0 ->>> df.mod(other, fill_value=3) - one three two -a NaN NaN NaN -b 2.0 3.0 1.0 -c 0.0 NaN 1.0 -d 2.0 NaN 0.0 -e NaN -4.0 NaN -""" - _op_descriptions = { # Arithmetic Operators 'add': {'op': '+', 'desc': 'Addition', - 'reverse': 'radd', - 'df_examples': _add_example_FRAME}, + 'reverse': 'radd'}, 'sub': {'op': '-', 'desc': 'Subtraction', - 'reverse': 'rsub', - 'df_examples': _sub_example_FRAME}, + 'reverse': 'rsub'}, 'mul': {'op': '*', 'desc': 'Multiplication', 'reverse': 'rmul', 'df_examples': None}, 'mod': {'op': '%', 'desc': 'Modulo', - 'reverse': 'rmod', - 'df_examples': _mod_example_FRAME}, + 'reverse': 'rmod'}, 'pow': {'op': '**', 'desc': 'Exponential power', 'reverse': 'rpow', @@ -599,24 +496,30 @@ def _get_op_name(op, special): """ _flex_doc_FRAME = """ -{desc} of dataframe and other, element-wise (binary operator `{op_name}`). +{desc} of dataframe and other, element-wise (binary operator `{op_name}` +and reverse version `{reverse}`). -Equivalent to ``{equiv}``, but with support to substitute a fill_value for -missing data in one of the inputs. +Among flexible wrappers (`add`, `sub`, `mul`, `div`, `mod`, `pow`) to +arithmetic operators. + +Equivalent to `+`, `-`, `*`, `/`, `//`, `%`, `^` but with support to +substitute a fill_value for missing data in one of the inputs. Parameters ---------- -other : Series, DataFrame, or constant -axis : {{0, 1, 'index', 'columns'}} - For Series input, axis to match Series index on -level : int or name +other : scalar, sequence, Series, or DataFrame + Any single or multiple element data structure, or list-like object. +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. +level : int or object Broadcast across a level, matching Index values on the - passed MultiIndex level + passed MultiIndex level. fill_value : None or float value, default None Fill existing missing (NaN) values, and any new element needed for successful DataFrame alignment, with this value before computation. If data in both corresponding DataFrame locations is missing - the result will be missing + the result will be missing. Notes ----- @@ -624,15 +527,113 @@ def _get_op_name(op, special): Returns ------- -result : DataFrame +DataFrame + Result of the arithmetic operation. -Examples +See Also -------- -{df_examples} +DataFrame.add : Add DataFrames +DataFrame.sub : Subtract DataFrames +DataFrame.mul : Multiply DataFrames +DataFrame.div : Divide Datafames (float division) +DataFrame.truediv : Divide Datafames (float division) +DataFrame.floordiv : Divide Datafames (integer division) +DataFrame.mod : Calculate modulo (remainder after division) of DataFrames +DataFrame.pow : Calculate exponential power of Datafames -See also +Examples -------- -DataFrame.{reverse} +>>> df = pd.DataFrame({{'assets': [400, 250, 100], +... 'liability': [120, 360, 280]}}, +... index = ['A', 'B', 'C']) +>>> df + assets liability +A 400 120 +B 250 360 +C 100 280 + +Add a scalar with operator version which return the same results. + +>>> df + 100 + assets liability +A 500 220 +B 350 460 +C 200 380 + +>>> df.add(100) + assets liability +A 500 220 +B 350 460 +C 200 380 + +Subtract a list and Series by axis with operator version. + +>>> df - [100, 250] + assets liability +A 300 -130 +B 150 110 +C 0 30 + +>>> df.sub([100, 250], axis='columns') + assets liability +A 300 -130 +B 150 110 +C 0 30 + +>>> df.sub(pd.Series([100, 250, 300], index=['A', 'B', 'C']), axis='index') + assets liability +A 300 20 +B 0 110 +C -200 -20 + +Multiply a DataFrame of different shape with operator version. + +>>> other = pd.DataFrame({{'assets': [2, 5, 3, 1]}}, +... index = ['A', 'B', 'C', 'D']) +>>> other + assets +A 2 +B 5 +C 3 +D 1 + +>>> df * other + assets liability +A 800.0 NaN +B 1250.0 NaN +C 300.0 NaN +D NaN NaN + +>>> df.mul(other, fill_value=0) + assets liability +A 800.0 0.0 +B 1250.0 0.0 +C 300.0 0.0 +D 0.0 NaN + +Divide by a Multindex + +>>> df_multindex = pd.DataFrame({{'assets': [250, 150, 100, 150, 300, 220], +... 'liability': [100, 250, 300, 200, 175, 225]}}, +... index = [['Q1', 'Q1', 'Q1', 'Q2', 'Q2', 'Q2'], +... ['A', 'B', 'C', 'A', 'B' ,'C']]) +>>> df_multindex + assets liability +Q1 A 250 100 + B 150 250 + C 100 300 +Q2 A 150 200 + B 300 175 + C 220 225 + +>>> df.div(df_multindex, level=1) + assets liability +Q1 A 1.600000 1.200000 + B 1.666667 1.440000 + C 1.000000 0.933333 +Q2 A 2.666667 0.600000 + B 0.833333 2.057143 + C 0.454545 1.244444 """ _flex_comp_doc_FRAME = """ @@ -697,29 +698,35 @@ def _get_op_name(op, special): A False True B False False C True False + >>> df.eq(100) cost revenue A False True B False False C True False -Compare to a list and Series by axis and operator version. +Compare to a list and Series by axis and operator version. As shown, +for list axis is by default 'index', but for Series axis is by +default 'columns'. >>> df != [100, 250, 300] cost revenue A True False B True False C True False + >>> df.ne([100, 250, 300], axis='index') - cost revenue + cost revenue A True False B True False C True False + >>> df != pd.Series([100, 250, 300]) cost revenue 0 1 2 A True True True True True B True True True True True C True True True True True + >>> df.ne(pd.Series([100, 250, 300]), axis='columns') cost revenue 0 1 2 A True True True True True @@ -736,6 +743,7 @@ def _get_op_name(op, special): B 250 C 100 D 150 + >>> df.gt(other) cost revenue A False False @@ -833,8 +841,7 @@ def _make_flex_doc(op_name, typ): elif typ == 'dataframe': base_doc = _flex_doc_FRAME doc = base_doc.format(desc=op_desc['desc'], op_name=op_name, - equiv=equiv, reverse=op_desc['reverse'], - df_examples=op_desc['df_examples']) + equiv=equiv, reverse=op_desc['reverse']) elif typ == 'panel': base_doc = _flex_doc_PANEL doc = base_doc.format(desc=op_desc['desc'], op_name=op_name, From bbcdcbe3a0b458af2bebcdf662c38975e80fb144 Mon Sep 17 00:00:00 2001 From: ParfaitG Date: Thu, 23 Aug 2018 22:01:26 -0500 Subject: [PATCH 13/23] DOC: Updated df arithmetic operators, extended series arithmetic and comparison docstrings --- pandas/core/ops.py | 489 ++++++++++++++++++++++++++++++++++----------- 1 file changed, 370 insertions(+), 119 deletions(-) diff --git a/pandas/core/ops.py b/pandas/core/ops.py index ea71b6d1e1f5b..9ebfa93821f35 100644 --- a/pandas/core/ops.py +++ b/pandas/core/ops.py @@ -418,54 +418,265 @@ def _get_op_name(op, special): _op_descriptions[reverse_op]['reverse'] = key _flex_doc_SERIES = """ -{desc} of series and other, element-wise (binary operator `{op_name}`). +{desc} of series and other, element-wise (binary operator `{op_name}`) +and reverse version `{reverse}`). + +Among flexible wrappers (`add`, `sub`, `mul`, `div`, `mod`, `pow`) to +arithmetic operators. -Equivalent to ``{equiv}``, but with support to substitute a fill_value for -missing data in one of the inputs. +Equivalent to `+`, `-`, `*`, `/`, `//`, `%`, `**` but with support to +choose axis (index or columns) and level for calculation and substitute +a fill_value for missing data in one of the inputs. Parameters ---------- -other : Series or scalar value +other : scalar, sequence, Series + Any single or multiple element data structure, or list-like object. +level : int or name + Broadcast across a level, matching Index values on the + passed MultiIndex level. fill_value : None or float value, default None (NaN) Fill existing missing (NaN) values, and any new element needed for successful Series alignment, with this value before computation. If data in both corresponding Series locations is missing - the result will be missing -level : int or name - Broadcast across a level, matching Index values on the - passed MultiIndex level + the result will be missing. +axis : {{0 or 'index', 1 or 'columns'}}, default 0 + The index or the name of the axis. Returns ------- -result : Series +Series + Result of the arithmetic operation. + +See also +-------- +Series.add : Add Series. +Series.sub : Subtract Series. +Series.mul : Multiply Series. +Series.div : Divide Series (float division). +Series.truediv : Divide Series (float division). +Series.floordiv : Divide Series (integer division). +Series.mod : Calculate modulo (remainder after division) of + Series. +Series.pow : Calculate exponential power of Series. Examples -------- ->>> a = pd.Series([1, 1, 1, np.nan], index=['a', 'b', 'c', 'd']) ->>> a -a 1.0 -b 1.0 -c 1.0 -d NaN +>>> ser = pd.Series([4, 6, 8]) +>>> ser +0 4 +1 6 +2 8 +dtype: int64 + +Add a scalar with operator and reverse version which return the same +results. + +>>> ser + 2 +0 6 +1 8 +2 10 +dtype: int64 + +>>> ser.add(2) +0 6 +1 8 +2 10 +dtype: int64 + +>>> ser.pow(2) +0 16 +1 36 +2 64 +dtype: int64 + +>>> ser.rpow(2) +0 16 +1 64 +2 256 +dtype: int64 + +Subtract a list with operator version. + +>>> ser - [1, 2, 3] +0 3 +1 4 +2 5 +dtype: int64 + +>>> ser.sub([1, 2, 3]) +0 3 +1 4 +2 5 +dtype: int64 + +Multiply a Series of different length with operator version. + +>>> other = pd.Series([1, 3, 5, 7]) +>>> other +0 1 +1 3 +2 5 +3 7 +dtype: int64 + +>>> ser * other +0 4.0 +1 18.0 +2 40.0 +3 NaN dtype: float64 ->>> b = pd.Series([1, np.nan, 1, np.nan], index=['a', 'b', 'd', 'e']) ->>> b -a 1.0 -b NaN -d 1.0 -e NaN + +>>> ser.mul(other, fill_value = 0) +0 4.0 +1 18.0 +2 40.0 +3 0.0 dtype: float64 ->>> a.add(b, fill_value=0) -a 2.0 -b 1.0 -c 1.0 -d 1.0 -e NaN + +Divide a multindex. + +>>> ser_multindex = pd.Series([5, 6, 7, 8, 2, 3], +... index = [['Q1', 'Q1', 'Q1', 'Q2', 'Q2', 'Q2'], +... [0, 1, 2, 0, 1, 2]]) +>>> ser_multindex +Q1 0 5 + 1 6 + 2 7 +Q2 0 8 + 1 2 + 2 3 +dtype: int64 + +>>> ser.div(ser_multindex, level=1) +Q1 0 0.800000 + 1 1.000000 + 2 1.142857 +Q2 0 0.500000 + 1 3.000000 + 2 2.666667 dtype: float64 +""" + +_flex_comp_doc_SERIES = """ +{desc} of series and other, element-wise (binary operator `{op_name}`). + +Among flexible wrappers (`eq`, `ne`, `le`, `lt`, `ge`, `gt`) to comparison +operators. + +Equivalent to `==`, `=!`, `<=`, `<`, `>=`, `>` with support to choose axis +(index or columns) and level for comparison. + +Parameters +---------- +other : scalar, sequence, Series + Any single or multiple element data structure, or list-like object. +level : int or name + Broadcast across a level, matching Index values on the + passed MultiIndex level. +fill_value : None or float value, default None + Fill existing missing (NaN) values, and any new element needed for + successful DataFrame alignment, with this value before computation. + If data in both corresponding DataFrame locations is missing + the result will be missing. +axis : {{0 or 'index', 1 or 'columns'}}, default 0 + The index or the name of the axis. + +Returns +------- +Series of bool + Result of the comparison. See also -------- -Series.{reverse} +Series.eq : Compare Series for equality elementwise. +Series.ne : Compare Series for inequality elementwise. +Series.le : Compare Series for less than inequality. + or equality elementwise. +Series.lt : Compare Series for strictly less than + inequality elementwise. +Series.ge : Compare Series for greater than inequality + or equality elementwise. +Series.gt : Compare Series for strictly greater than + inequality elementwise. + +Examples +-------- +>>> ser = pd.Series([250, 150, 100], index = ['A', 'B', 'C']) +>>> ser +A 250 +B 150 +C 100 +dtype: int64 + +Compare to a scalar and operator version which return the same +results. + +>>> ser == 100 +A False +B False +C True +dtype: bool + +>>> ser.eq(100) +A False +B False +C True +dtype: bool + +Compare to a list with operator version. + +>>> ser != [100, 250, 300] +A True +B True +C True +dtype: bool + +>>> ser.ne([100, 250, 300]) +A True +B True +C True +dtype: bool + +Compare to a Series of different length. + +>>> other = pd.Series([200, 150, 100, 500] , index = ['A', 'B', 'C', 'D']) +>>> other +A 200 +B 150 +C 100 +D 500 +dtype: int64 + +>>> ser.ge(other) +A True +B True +C True +D False +dtype: bool + +Compare to a Mulitindex. + +>>> ser_multindex = pd.Series([100, 250, 300, 200, 175, 225], +... index = [['Q1', 'Q1', 'Q1', 'Q2', 'Q2', 'Q2'], +... ['A', 'B', 'C', 'A', 'B' ,'C']]) +>>> ser_multindex +Q1 A 100 + B 250 + C 300 +Q2 A 200 + B 175 + C 225 +dtype: int64 + +>>> ser.le(ser_multindex, level=1) +Q1 A False + B True + C True +Q2 A False + B True + C True +dtype: bool """ _arith_doc_FRAME = """ @@ -502,8 +713,9 @@ def _get_op_name(op, special): Among flexible wrappers (`add`, `sub`, `mul`, `div`, `mod`, `pow`) to arithmetic operators. -Equivalent to `+`, `-`, `*`, `/`, `//`, `%`, `^` but with support to -substitute a fill_value for missing data in one of the inputs. +Equivalent to `+`, `-`, `*`, `/`, `//`, `%`, `**` but with support to +choose axis (index or columns) and level for calculation and substitute +a fill_value for missing data in one of the inputs. Parameters ---------- @@ -512,10 +724,10 @@ def _get_op_name(op, special): 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. -level : int or object +level : int or label Broadcast across a level, matching Index values on the passed MultiIndex level. -fill_value : None or float value, default None +fill_value : float or None, default None Fill existing missing (NaN) values, and any new element needed for successful DataFrame alignment, with this value before computation. If data in both corresponding DataFrame locations is missing @@ -532,108 +744,120 @@ def _get_op_name(op, special): See Also -------- -DataFrame.add : Add DataFrames -DataFrame.sub : Subtract DataFrames -DataFrame.mul : Multiply DataFrames -DataFrame.div : Divide Datafames (float division) -DataFrame.truediv : Divide Datafames (float division) -DataFrame.floordiv : Divide Datafames (integer division) -DataFrame.mod : Calculate modulo (remainder after division) of DataFrames -DataFrame.pow : Calculate exponential power of Datafames +DataFrame.add : Add DataFrames. +DataFrame.sub : Subtract DataFrames. +DataFrame.mul : Multiply DataFrames. +DataFrame.div : Divide DataFrames (float division). +DataFrame.truediv : Divide DataFrames (float division). +DataFrame.floordiv : Divide DataFrames (integer division). +DataFrame.mod : Calculate modulo (remainder after division) of + DataFrames. +DataFrame.pow : Calculate exponential power of Datafames. Examples -------- ->>> df = pd.DataFrame({{'assets': [400, 250, 100], -... 'liability': [120, 360, 280]}}, -... index = ['A', 'B', 'C']) +>>> df = pd.DataFrame({{'A': [4, 6, 8], +... 'B': [3, 5, 9]}}) >>> df - assets liability -A 400 120 -B 250 360 -C 100 280 - -Add a scalar with operator version which return the same results. + A B +0 4 3 +1 6 5 +2 8 9 ->>> df + 100 - assets liability -A 500 220 -B 350 460 -C 200 380 +Add a scalar with operator and reverse version which return the same +results. ->>> df.add(100) - assets liability -A 500 220 -B 350 460 -C 200 380 +>>> df + 5 + A B +0 9 8 +1 11 10 +2 13 14 + +>>> df.add(5) + A B +0 9 8 +1 11 10 +2 13 14 + +>>> df.div(10) + A B +0 0.4 0.3 +1 0.6 0.5 +2 0.8 0.9 + +>> df.rdiv(10) + A B +0 2.500000 3.333333 +1 1.666667 2.000000 +2 1.250000 1.111111 Subtract a list and Series by axis with operator version. ->>> df - [100, 250] - assets liability -A 300 -130 -B 150 110 -C 0 30 +>>> df - [1, 2] + A B +0 3 1 +1 5 3 +2 7 7 ->>> df.sub([100, 250], axis='columns') - assets liability -A 300 -130 -B 150 110 -C 0 30 +>>> df.sub([1, 2], axis='columns') + A B +0 3 1 +1 5 3 +2 7 7 ->>> df.sub(pd.Series([100, 250, 300], index=['A', 'B', 'C']), axis='index') - assets liability -A 300 20 -B 0 110 -C -200 -20 +>>> df.sub(pd.Series([1, 2, 4]), axis='index') + A B +0 3 2 +1 4 3 +2 4 5 Multiply a DataFrame of different shape with operator version. ->>> other = pd.DataFrame({{'assets': [2, 5, 3, 1]}}, -... index = ['A', 'B', 'C', 'D']) +>>> other = pd.DataFrame({{'A': [2, 5, 3, 1]}}) >>> other - assets -A 2 -B 5 -C 3 -D 1 + A +0 2 +1 5 +2 3 +3 1 >>> df * other - assets liability -A 800.0 NaN -B 1250.0 NaN -C 300.0 NaN -D NaN NaN + A B +0 8.0 NaN +1 30.0 NaN +2 24.0 NaN +3 NaN NaN >>> df.mul(other, fill_value=0) - assets liability -A 800.0 0.0 -B 1250.0 0.0 -C 300.0 0.0 -D 0.0 NaN + A B +0 8.0 0.0 +1 30.0 0.0 +2 24.0 0.0 +3 0.0 NaN -Divide by a Multindex +Divide by a Multindex. ->>> df_multindex = pd.DataFrame({{'assets': [250, 150, 100, 150, 300, 220], -... 'liability': [100, 250, 300, 200, 175, 225]}}, +>>> df_multindex = pd.DataFrame({{'A': [2, 4, 6, 8, 3, 4], +... 'B': [1, 3, 5, 7, 5, 6]}}, ... index = [['Q1', 'Q1', 'Q1', 'Q2', 'Q2', 'Q2'], -... ['A', 'B', 'C', 'A', 'B' ,'C']]) +... [0, 1, 2, 0, 1, 2]]) >>> df_multindex - assets liability -Q1 A 250 100 - B 150 250 - C 100 300 -Q2 A 150 200 - B 300 175 - C 220 225 + A B +Q1 0 2 1 + 1 4 3 + 2 6 5 +Q2 0 8 7 + 1 3 5 + 2 4 6 >>> df.div(df_multindex, level=1) - assets liability -Q1 A 1.600000 1.200000 - B 1.666667 1.440000 - C 1.000000 0.933333 -Q2 A 2.666667 0.600000 - B 0.833333 2.057143 - C 0.454545 1.244444 + A B +Q1 0 2.000000 3.000000 + 1 1.500000 1.666667 + 2 1.333333 1.800000 +Q2 0 0.500000 0.428571 + 1 2.000000 1.000000 + 2 2.000000 1.500000 """ _flex_comp_doc_FRAME = """ @@ -643,7 +867,7 @@ def _get_op_name(op, special): operators. Equivalent to `==`, `=!`, `<=`, `<`, `>=`, `>` with support to choose axis -(rows or columns) and level for comparison. +(index or columns) and level for comparison. Parameters ---------- @@ -652,7 +876,7 @@ def _get_op_name(op, special): axis : {{0 or 'index', 1 or 'columns'}}, default 'columns' Whether to compare by the index (0 or 'index') or columns (1 or 'columns'). -level : int or object +level : int or label Broadcast across a level, matching Index values on the passed MultiIndex level. @@ -663,16 +887,16 @@ def _get_op_name(op, special): See Also -------- -DataFrame.eq : Compare DataFrames for equality elementwise -DataFrame.ne : Compare DataFrames for inequality elementwise -DataFrame.le : Compare DataFrames for less than inequality - or equality elementwise +DataFrame.eq : Compare DataFrames for equality elementwise. +DataFrame.ne : Compare DataFrames for inequality elementwise. +DataFrame.le : Compare DataFrames for less than inequality. + or equality elementwise. DataFrame.lt : Compare DataFrames for strictly less than - inequality elementwise + inequality elementwise. DataFrame.ge : Compare DataFrames for greater than inequality - or equality elementwise + or equality elementwise. DataFrame.gt : Compare DataFrames for strictly greater than - inequality elementwise + inequality elementwise. Notes -------- @@ -1041,7 +1265,7 @@ def _get_method_wrappers(cls): elif issubclass(cls, ABCSeries): # Just Series; SparseSeries is caught above arith_flex = _flex_method_SERIES - comp_flex = _flex_method_SERIES + comp_flex = _flex_comp_method_SERIES arith_special = _arith_method_SERIES comp_special = _comp_method_SERIES bool_special = _bool_method_SERIES @@ -1711,6 +1935,33 @@ def flex_wrapper(self, other, level=None, fill_value=None, axis=0): return flex_wrapper +def _flex_comp_method_SERIES(cls, op, special): + op_name = _get_op_name(op, special) + doc = _flex_comp_doc_SERIES.format(op_name=op_name, + desc=_op_descriptions[op_name]['desc']) + + @Appender(doc) + def flex_wrapper(self, other, level=None, fill_value=None, axis=0): + # validate axis + if axis is not None: + self._get_axis_number(axis) + if isinstance(other, ABCSeries): + return self._binop(other, op, level=level, fill_value=fill_value) + elif isinstance(other, (np.ndarray, list, tuple)): + if len(other) != len(self): + raise ValueError('Lengths must be equal') + other = self._constructor(other, self.index) + return self._binop(other, op, level=level, fill_value=fill_value) + else: + if fill_value is not None: + self = self.fillna(fill_value) + + return self._constructor(op(self, other), + self.index).__finalize__(self) + + flex_wrapper.__name__ = op_name + return flex_wrapper + # ----------------------------------------------------------------------------- # DataFrame From a33f003112cfebfc42b82dcfa9ba285c82e96b83 Mon Sep 17 00:00:00 2001 From: ParfaitG Date: Fri, 24 Aug 2018 22:51:54 -0500 Subject: [PATCH 14/23] Revert "DOC: Updated df arithmetic operators, extended series arithmetic and comparison docstrings" This reverts commit bbcdcbe3a0b458af2bebcdf662c38975e80fb144. --- pandas/core/ops.py | 489 +++++++++++---------------------------------- 1 file changed, 119 insertions(+), 370 deletions(-) diff --git a/pandas/core/ops.py b/pandas/core/ops.py index 9ebfa93821f35..ea71b6d1e1f5b 100644 --- a/pandas/core/ops.py +++ b/pandas/core/ops.py @@ -418,265 +418,54 @@ def _get_op_name(op, special): _op_descriptions[reverse_op]['reverse'] = key _flex_doc_SERIES = """ -{desc} of series and other, element-wise (binary operator `{op_name}`) -and reverse version `{reverse}`). - -Among flexible wrappers (`add`, `sub`, `mul`, `div`, `mod`, `pow`) to -arithmetic operators. +{desc} of series and other, element-wise (binary operator `{op_name}`). -Equivalent to `+`, `-`, `*`, `/`, `//`, `%`, `**` but with support to -choose axis (index or columns) and level for calculation and substitute -a fill_value for missing data in one of the inputs. +Equivalent to ``{equiv}``, but with support to substitute a fill_value for +missing data in one of the inputs. Parameters ---------- -other : scalar, sequence, Series - Any single or multiple element data structure, or list-like object. -level : int or name - Broadcast across a level, matching Index values on the - passed MultiIndex level. +other : Series or scalar value fill_value : None or float value, default None (NaN) Fill existing missing (NaN) values, and any new element needed for successful Series alignment, with this value before computation. If data in both corresponding Series locations is missing - the result will be missing. -axis : {{0 or 'index', 1 or 'columns'}}, default 0 - The index or the name of the axis. + the result will be missing +level : int or name + Broadcast across a level, matching Index values on the + passed MultiIndex level Returns ------- -Series - Result of the arithmetic operation. - -See also --------- -Series.add : Add Series. -Series.sub : Subtract Series. -Series.mul : Multiply Series. -Series.div : Divide Series (float division). -Series.truediv : Divide Series (float division). -Series.floordiv : Divide Series (integer division). -Series.mod : Calculate modulo (remainder after division) of - Series. -Series.pow : Calculate exponential power of Series. +result : Series Examples -------- ->>> ser = pd.Series([4, 6, 8]) ->>> ser -0 4 -1 6 -2 8 -dtype: int64 - -Add a scalar with operator and reverse version which return the same -results. - ->>> ser + 2 -0 6 -1 8 -2 10 -dtype: int64 - ->>> ser.add(2) -0 6 -1 8 -2 10 -dtype: int64 - ->>> ser.pow(2) -0 16 -1 36 -2 64 -dtype: int64 - ->>> ser.rpow(2) -0 16 -1 64 -2 256 -dtype: int64 - -Subtract a list with operator version. - ->>> ser - [1, 2, 3] -0 3 -1 4 -2 5 -dtype: int64 - ->>> ser.sub([1, 2, 3]) -0 3 -1 4 -2 5 -dtype: int64 - -Multiply a Series of different length with operator version. - ->>> other = pd.Series([1, 3, 5, 7]) ->>> other -0 1 -1 3 -2 5 -3 7 -dtype: int64 - ->>> ser * other -0 4.0 -1 18.0 -2 40.0 -3 NaN +>>> a = pd.Series([1, 1, 1, np.nan], index=['a', 'b', 'c', 'd']) +>>> a +a 1.0 +b 1.0 +c 1.0 +d NaN dtype: float64 - ->>> ser.mul(other, fill_value = 0) -0 4.0 -1 18.0 -2 40.0 -3 0.0 +>>> b = pd.Series([1, np.nan, 1, np.nan], index=['a', 'b', 'd', 'e']) +>>> b +a 1.0 +b NaN +d 1.0 +e NaN dtype: float64 - -Divide a multindex. - ->>> ser_multindex = pd.Series([5, 6, 7, 8, 2, 3], -... index = [['Q1', 'Q1', 'Q1', 'Q2', 'Q2', 'Q2'], -... [0, 1, 2, 0, 1, 2]]) ->>> ser_multindex -Q1 0 5 - 1 6 - 2 7 -Q2 0 8 - 1 2 - 2 3 -dtype: int64 - ->>> ser.div(ser_multindex, level=1) -Q1 0 0.800000 - 1 1.000000 - 2 1.142857 -Q2 0 0.500000 - 1 3.000000 - 2 2.666667 +>>> a.add(b, fill_value=0) +a 2.0 +b 1.0 +c 1.0 +d 1.0 +e NaN dtype: float64 -""" - -_flex_comp_doc_SERIES = """ -{desc} of series and other, element-wise (binary operator `{op_name}`). - -Among flexible wrappers (`eq`, `ne`, `le`, `lt`, `ge`, `gt`) to comparison -operators. - -Equivalent to `==`, `=!`, `<=`, `<`, `>=`, `>` with support to choose axis -(index or columns) and level for comparison. - -Parameters ----------- -other : scalar, sequence, Series - Any single or multiple element data structure, or list-like object. -level : int or name - Broadcast across a level, matching Index values on the - passed MultiIndex level. -fill_value : None or float value, default None - Fill existing missing (NaN) values, and any new element needed for - successful DataFrame alignment, with this value before computation. - If data in both corresponding DataFrame locations is missing - the result will be missing. -axis : {{0 or 'index', 1 or 'columns'}}, default 0 - The index or the name of the axis. - -Returns -------- -Series of bool - Result of the comparison. See also -------- -Series.eq : Compare Series for equality elementwise. -Series.ne : Compare Series for inequality elementwise. -Series.le : Compare Series for less than inequality. - or equality elementwise. -Series.lt : Compare Series for strictly less than - inequality elementwise. -Series.ge : Compare Series for greater than inequality - or equality elementwise. -Series.gt : Compare Series for strictly greater than - inequality elementwise. - -Examples --------- ->>> ser = pd.Series([250, 150, 100], index = ['A', 'B', 'C']) ->>> ser -A 250 -B 150 -C 100 -dtype: int64 - -Compare to a scalar and operator version which return the same -results. - ->>> ser == 100 -A False -B False -C True -dtype: bool - ->>> ser.eq(100) -A False -B False -C True -dtype: bool - -Compare to a list with operator version. - ->>> ser != [100, 250, 300] -A True -B True -C True -dtype: bool - ->>> ser.ne([100, 250, 300]) -A True -B True -C True -dtype: bool - -Compare to a Series of different length. - ->>> other = pd.Series([200, 150, 100, 500] , index = ['A', 'B', 'C', 'D']) ->>> other -A 200 -B 150 -C 100 -D 500 -dtype: int64 - ->>> ser.ge(other) -A True -B True -C True -D False -dtype: bool - -Compare to a Mulitindex. - ->>> ser_multindex = pd.Series([100, 250, 300, 200, 175, 225], -... index = [['Q1', 'Q1', 'Q1', 'Q2', 'Q2', 'Q2'], -... ['A', 'B', 'C', 'A', 'B' ,'C']]) ->>> ser_multindex -Q1 A 100 - B 250 - C 300 -Q2 A 200 - B 175 - C 225 -dtype: int64 - ->>> ser.le(ser_multindex, level=1) -Q1 A False - B True - C True -Q2 A False - B True - C True -dtype: bool +Series.{reverse} """ _arith_doc_FRAME = """ @@ -713,9 +502,8 @@ def _get_op_name(op, special): Among flexible wrappers (`add`, `sub`, `mul`, `div`, `mod`, `pow`) to arithmetic operators. -Equivalent to `+`, `-`, `*`, `/`, `//`, `%`, `**` but with support to -choose axis (index or columns) and level for calculation and substitute -a fill_value for missing data in one of the inputs. +Equivalent to `+`, `-`, `*`, `/`, `//`, `%`, `^` but with support to +substitute a fill_value for missing data in one of the inputs. Parameters ---------- @@ -724,10 +512,10 @@ def _get_op_name(op, special): 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. -level : int or label +level : int or object Broadcast across a level, matching Index values on the passed MultiIndex level. -fill_value : float or None, default None +fill_value : None or float value, default None Fill existing missing (NaN) values, and any new element needed for successful DataFrame alignment, with this value before computation. If data in both corresponding DataFrame locations is missing @@ -744,120 +532,108 @@ def _get_op_name(op, special): See Also -------- -DataFrame.add : Add DataFrames. -DataFrame.sub : Subtract DataFrames. -DataFrame.mul : Multiply DataFrames. -DataFrame.div : Divide DataFrames (float division). -DataFrame.truediv : Divide DataFrames (float division). -DataFrame.floordiv : Divide DataFrames (integer division). -DataFrame.mod : Calculate modulo (remainder after division) of - DataFrames. -DataFrame.pow : Calculate exponential power of Datafames. +DataFrame.add : Add DataFrames +DataFrame.sub : Subtract DataFrames +DataFrame.mul : Multiply DataFrames +DataFrame.div : Divide Datafames (float division) +DataFrame.truediv : Divide Datafames (float division) +DataFrame.floordiv : Divide Datafames (integer division) +DataFrame.mod : Calculate modulo (remainder after division) of DataFrames +DataFrame.pow : Calculate exponential power of Datafames Examples -------- ->>> df = pd.DataFrame({{'A': [4, 6, 8], -... 'B': [3, 5, 9]}}) +>>> df = pd.DataFrame({{'assets': [400, 250, 100], +... 'liability': [120, 360, 280]}}, +... index = ['A', 'B', 'C']) >>> df - A B -0 4 3 -1 6 5 -2 8 9 + assets liability +A 400 120 +B 250 360 +C 100 280 -Add a scalar with operator and reverse version which return the same -results. +Add a scalar with operator version which return the same results. ->>> df + 5 - A B -0 9 8 -1 11 10 -2 13 14 - ->>> df.add(5) - A B -0 9 8 -1 11 10 -2 13 14 - ->>> df.div(10) - A B -0 0.4 0.3 -1 0.6 0.5 -2 0.8 0.9 - ->> df.rdiv(10) - A B -0 2.500000 3.333333 -1 1.666667 2.000000 -2 1.250000 1.111111 +>>> df + 100 + assets liability +A 500 220 +B 350 460 +C 200 380 + +>>> df.add(100) + assets liability +A 500 220 +B 350 460 +C 200 380 Subtract a list and Series by axis with operator version. ->>> df - [1, 2] - A B -0 3 1 -1 5 3 -2 7 7 +>>> df - [100, 250] + assets liability +A 300 -130 +B 150 110 +C 0 30 ->>> df.sub([1, 2], axis='columns') - A B -0 3 1 -1 5 3 -2 7 7 +>>> df.sub([100, 250], axis='columns') + assets liability +A 300 -130 +B 150 110 +C 0 30 ->>> df.sub(pd.Series([1, 2, 4]), axis='index') - A B -0 3 2 -1 4 3 -2 4 5 +>>> df.sub(pd.Series([100, 250, 300], index=['A', 'B', 'C']), axis='index') + assets liability +A 300 20 +B 0 110 +C -200 -20 Multiply a DataFrame of different shape with operator version. ->>> other = pd.DataFrame({{'A': [2, 5, 3, 1]}}) +>>> other = pd.DataFrame({{'assets': [2, 5, 3, 1]}}, +... index = ['A', 'B', 'C', 'D']) >>> other - A -0 2 -1 5 -2 3 -3 1 + assets +A 2 +B 5 +C 3 +D 1 >>> df * other - A B -0 8.0 NaN -1 30.0 NaN -2 24.0 NaN -3 NaN NaN + assets liability +A 800.0 NaN +B 1250.0 NaN +C 300.0 NaN +D NaN NaN >>> df.mul(other, fill_value=0) - A B -0 8.0 0.0 -1 30.0 0.0 -2 24.0 0.0 -3 0.0 NaN + assets liability +A 800.0 0.0 +B 1250.0 0.0 +C 300.0 0.0 +D 0.0 NaN -Divide by a Multindex. +Divide by a Multindex ->>> df_multindex = pd.DataFrame({{'A': [2, 4, 6, 8, 3, 4], -... 'B': [1, 3, 5, 7, 5, 6]}}, +>>> df_multindex = pd.DataFrame({{'assets': [250, 150, 100, 150, 300, 220], +... 'liability': [100, 250, 300, 200, 175, 225]}}, ... index = [['Q1', 'Q1', 'Q1', 'Q2', 'Q2', 'Q2'], -... [0, 1, 2, 0, 1, 2]]) +... ['A', 'B', 'C', 'A', 'B' ,'C']]) >>> df_multindex - A B -Q1 0 2 1 - 1 4 3 - 2 6 5 -Q2 0 8 7 - 1 3 5 - 2 4 6 + assets liability +Q1 A 250 100 + B 150 250 + C 100 300 +Q2 A 150 200 + B 300 175 + C 220 225 >>> df.div(df_multindex, level=1) - A B -Q1 0 2.000000 3.000000 - 1 1.500000 1.666667 - 2 1.333333 1.800000 -Q2 0 0.500000 0.428571 - 1 2.000000 1.000000 - 2 2.000000 1.500000 + assets liability +Q1 A 1.600000 1.200000 + B 1.666667 1.440000 + C 1.000000 0.933333 +Q2 A 2.666667 0.600000 + B 0.833333 2.057143 + C 0.454545 1.244444 """ _flex_comp_doc_FRAME = """ @@ -867,7 +643,7 @@ def _get_op_name(op, special): operators. Equivalent to `==`, `=!`, `<=`, `<`, `>=`, `>` with support to choose axis -(index or columns) and level for comparison. +(rows or columns) and level for comparison. Parameters ---------- @@ -876,7 +652,7 @@ def _get_op_name(op, special): axis : {{0 or 'index', 1 or 'columns'}}, default 'columns' Whether to compare by the index (0 or 'index') or columns (1 or 'columns'). -level : int or label +level : int or object Broadcast across a level, matching Index values on the passed MultiIndex level. @@ -887,16 +663,16 @@ def _get_op_name(op, special): See Also -------- -DataFrame.eq : Compare DataFrames for equality elementwise. -DataFrame.ne : Compare DataFrames for inequality elementwise. -DataFrame.le : Compare DataFrames for less than inequality. - or equality elementwise. +DataFrame.eq : Compare DataFrames for equality elementwise +DataFrame.ne : Compare DataFrames for inequality elementwise +DataFrame.le : Compare DataFrames for less than inequality + or equality elementwise DataFrame.lt : Compare DataFrames for strictly less than - inequality elementwise. + inequality elementwise DataFrame.ge : Compare DataFrames for greater than inequality - or equality elementwise. + or equality elementwise DataFrame.gt : Compare DataFrames for strictly greater than - inequality elementwise. + inequality elementwise Notes -------- @@ -1265,7 +1041,7 @@ def _get_method_wrappers(cls): elif issubclass(cls, ABCSeries): # Just Series; SparseSeries is caught above arith_flex = _flex_method_SERIES - comp_flex = _flex_comp_method_SERIES + comp_flex = _flex_method_SERIES arith_special = _arith_method_SERIES comp_special = _comp_method_SERIES bool_special = _bool_method_SERIES @@ -1935,33 +1711,6 @@ def flex_wrapper(self, other, level=None, fill_value=None, axis=0): return flex_wrapper -def _flex_comp_method_SERIES(cls, op, special): - op_name = _get_op_name(op, special) - doc = _flex_comp_doc_SERIES.format(op_name=op_name, - desc=_op_descriptions[op_name]['desc']) - - @Appender(doc) - def flex_wrapper(self, other, level=None, fill_value=None, axis=0): - # validate axis - if axis is not None: - self._get_axis_number(axis) - if isinstance(other, ABCSeries): - return self._binop(other, op, level=level, fill_value=fill_value) - elif isinstance(other, (np.ndarray, list, tuple)): - if len(other) != len(self): - raise ValueError('Lengths must be equal') - other = self._constructor(other, self.index) - return self._binop(other, op, level=level, fill_value=fill_value) - else: - if fill_value is not None: - self = self.fillna(fill_value) - - return self._constructor(op(self, other), - self.index).__finalize__(self) - - flex_wrapper.__name__ = op_name - return flex_wrapper - # ----------------------------------------------------------------------------- # DataFrame From 70950c042345da4fe2ec102264e4d2c527da1db8 Mon Sep 17 00:00:00 2001 From: ParfaitG Date: Fri, 24 Aug 2018 23:07:29 -0500 Subject: [PATCH 15/23] DOC: Update DataFrame arithmetic docstring --- pandas/core/ops.py | 188 ++++++++++++++++++++++++--------------------- 1 file changed, 101 insertions(+), 87 deletions(-) diff --git a/pandas/core/ops.py b/pandas/core/ops.py index ea71b6d1e1f5b..21ff354fe854a 100644 --- a/pandas/core/ops.py +++ b/pandas/core/ops.py @@ -502,7 +502,7 @@ def _get_op_name(op, special): Among flexible wrappers (`add`, `sub`, `mul`, `div`, `mod`, `pow`) to arithmetic operators. -Equivalent to `+`, `-`, `*`, `/`, `//`, `%`, `^` but with support to +Equivalent to `+`, `-`, `*`, `/`, `//`, `%`, **` but with support to substitute a fill_value for missing data in one of the inputs. Parameters @@ -512,10 +512,10 @@ def _get_op_name(op, special): 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. -level : int or object +level : int or label Broadcast across a level, matching Index values on the passed MultiIndex level. -fill_value : None or float value, default None +fill_value : float or None, default None Fill existing missing (NaN) values, and any new element needed for successful DataFrame alignment, with this value before computation. If data in both corresponding DataFrame locations is missing @@ -523,7 +523,7 @@ def _get_op_name(op, special): Notes ----- -Mismatched indices will be unioned together +Mismatched indices will be unioned together. Returns ------- @@ -532,108 +532,122 @@ def _get_op_name(op, special): See Also -------- -DataFrame.add : Add DataFrames -DataFrame.sub : Subtract DataFrames -DataFrame.mul : Multiply DataFrames -DataFrame.div : Divide Datafames (float division) -DataFrame.truediv : Divide Datafames (float division) -DataFrame.floordiv : Divide Datafames (integer division) -DataFrame.mod : Calculate modulo (remainder after division) of DataFrames -DataFrame.pow : Calculate exponential power of Datafames +DataFrame.add : Add DataFrames. +DataFrame.sub : Subtract DataFrames. +DataFrame.mul : Multiply DataFrames. +DataFrame.div : Divide DataFrames (float division). +DataFrame.truediv : Divide DataFrames (float division). +DataFrame.floordiv : Divide DataFrames (integer division). +DataFrame.mod : Calculate modulo (remainder after division) of + DataFrames. +DataFrame.pow : Calculate exponential power of DataFrames. Examples -------- ->>> df = pd.DataFrame({{'assets': [400, 250, 100], -... 'liability': [120, 360, 280]}}, -... index = ['A', 'B', 'C']) +>>> df = pd.DataFrame({{'A': [4, 6, 8], +... 'B': [3, 5, 9]}}) >>> df - assets liability -A 400 120 -B 250 360 -C 100 280 + A B +0 4 3 +1 6 5 +2 8 9 + +Add a scalar with operator version which return the same +results. + +>>> df + 5 + A B +0 9 8 +1 11 10 +2 13 14 + +>>> df.add(5) + A B +0 9 8 +1 11 10 +2 13 14 -Add a scalar with operator version which return the same results. +Divide by constant with reverse version. ->>> df + 100 - assets liability -A 500 220 -B 350 460 -C 200 380 +>>> df.div(10) + A B +0 0.4 0.3 +1 0.6 0.5 +2 0.8 0.9 ->>> df.add(100) - assets liability -A 500 220 -B 350 460 -C 200 380 +>>> df.rdiv(10) + A B +0 2.500000 3.333333 +1 1.666667 2.000000 +2 1.250000 1.111111 Subtract a list and Series by axis with operator version. ->>> df - [100, 250] - assets liability -A 300 -130 -B 150 110 -C 0 30 +>>> df - [1, 2] + A B +0 3 1 +1 5 3 +2 7 7 ->>> df.sub([100, 250], axis='columns') - assets liability -A 300 -130 -B 150 110 -C 0 30 +>>> df.sub([1, 2], axis='columns') + A B +0 3 1 +1 5 3 +2 7 7 ->>> df.sub(pd.Series([100, 250, 300], index=['A', 'B', 'C']), axis='index') - assets liability -A 300 20 -B 0 110 -C -200 -20 +>>> df.sub(pd.Series([1, 2, 4]), axis='index') + A B +0 3 2 +1 4 3 +2 4 5 Multiply a DataFrame of different shape with operator version. ->>> other = pd.DataFrame({{'assets': [2, 5, 3, 1]}}, -... index = ['A', 'B', 'C', 'D']) +>>> other = pd.DataFrame({{'A': [2, 5, 3, 1]}}) >>> other - assets -A 2 -B 5 -C 3 -D 1 + A +0 2 +1 5 +2 3 +3 1 >>> df * other - assets liability -A 800.0 NaN -B 1250.0 NaN -C 300.0 NaN -D NaN NaN + A B +0 8.0 NaN +1 30.0 NaN +2 24.0 NaN +3 NaN NaN >>> df.mul(other, fill_value=0) - assets liability -A 800.0 0.0 -B 1250.0 0.0 -C 300.0 0.0 -D 0.0 NaN + A B +0 8.0 0.0 +1 30.0 0.0 +2 24.0 0.0 +3 0.0 NaN -Divide by a Multindex +Divide by a MultiIndex by level. ->>> df_multindex = pd.DataFrame({{'assets': [250, 150, 100, 150, 300, 220], -... 'liability': [100, 250, 300, 200, 175, 225]}}, +>>> df_multindex = pd.DataFrame({{'A': [2, 4, 6, 8, 3, 4], +... 'B': [1, 3, 5, 7, 5, 6]}}, ... index = [['Q1', 'Q1', 'Q1', 'Q2', 'Q2', 'Q2'], -... ['A', 'B', 'C', 'A', 'B' ,'C']]) +... [0, 1, 2, 0, 1, 2]]) >>> df_multindex - assets liability -Q1 A 250 100 - B 150 250 - C 100 300 -Q2 A 150 200 - B 300 175 - C 220 225 + A B +Q1 0 2 1 + 1 4 3 + 2 6 5 +Q2 0 8 7 + 1 3 5 + 2 4 6 >>> df.div(df_multindex, level=1) - assets liability -Q1 A 1.600000 1.200000 - B 1.666667 1.440000 - C 1.000000 0.933333 -Q2 A 2.666667 0.600000 - B 0.833333 2.057143 - C 0.454545 1.244444 + A B +Q1 0 2.000000 3.000000 + 1 1.500000 1.666667 + 2 1.333333 1.800000 +Q2 0 0.500000 0.428571 + 1 2.000000 1.000000 + 2 2.000000 1.500000 """ _flex_comp_doc_FRAME = """ @@ -652,7 +666,7 @@ def _get_op_name(op, special): axis : {{0 or 'index', 1 or 'columns'}}, default 'columns' Whether to compare by the index (0 or 'index') or columns (1 or 'columns'). -level : int or object +level : int or label Broadcast across a level, matching Index values on the passed MultiIndex level. @@ -663,16 +677,16 @@ def _get_op_name(op, special): See Also -------- -DataFrame.eq : Compare DataFrames for equality elementwise -DataFrame.ne : Compare DataFrames for inequality elementwise +DataFrame.eq : Compare DataFrames for equality elementwise. +DataFrame.ne : Compare DataFrames for inequality elementwise. DataFrame.le : Compare DataFrames for less than inequality - or equality elementwise + or equality elementwise. DataFrame.lt : Compare DataFrames for strictly less than - inequality elementwise + inequality elementwise. DataFrame.ge : Compare DataFrames for greater than inequality - or equality elementwise + or equality elementwise. DataFrame.gt : Compare DataFrames for strictly greater than - inequality elementwise + inequality elementwise. Notes -------- From 6bcb9b94fd264ca9b36acc8311ee5eb0b9ce00b1 Mon Sep 17 00:00:00 2001 From: ParfaitG Date: Tue, 25 Sep 2018 18:52:24 -0500 Subject: [PATCH 16/23] DOC: Updated examples in arithmetic operators --- pandas/core/ops.py | 154 ++++++++++++++++++++++----------------------- 1 file changed, 77 insertions(+), 77 deletions(-) diff --git a/pandas/core/ops.py b/pandas/core/ops.py index 21ff354fe854a..d1eb8780c24fb 100644 --- a/pandas/core/ops.py +++ b/pandas/core/ops.py @@ -538,116 +538,116 @@ def _get_op_name(op, special): DataFrame.div : Divide DataFrames (float division). DataFrame.truediv : Divide DataFrames (float division). DataFrame.floordiv : Divide DataFrames (integer division). -DataFrame.mod : Calculate modulo (remainder after division) of - DataFrames. -DataFrame.pow : Calculate exponential power of DataFrames. +DataFrame.mod : Calculate modulo (remainder after division). +DataFrame.pow : Calculate exponential power. Examples -------- ->>> df = pd.DataFrame({{'A': [4, 6, 8], -... 'B': [3, 5, 9]}}) +>>> df = pd.DataFrame({{'angles': [0, 3, 4], +... 'degrees': [360, 180, 360]}}, +... index=[ 'circle', 'triangle', 'rectangle']) >>> df - A B -0 4 3 -1 6 5 -2 8 9 + angles degrees +circle 0 360 +triangle 3 180 +rectangle 4 360 Add a scalar with operator version which return the same results. ->>> df + 5 - A B -0 9 8 -1 11 10 -2 13 14 +>>> df + 1 + angles degrees +circle 1 361 +triangle 4 181 +rectangle 5 361 ->>> df.add(5) - A B -0 9 8 -1 11 10 -2 13 14 +>>> df.add(1) + angles degrees +circle 1 361 +triangle 4 181 +rectangle 5 361 Divide by constant with reverse version. >>> df.div(10) - A B -0 0.4 0.3 -1 0.6 0.5 -2 0.8 0.9 + angles degrees +circle 0.0 36.0 +triangle 0.3 18.0 +rectangle 0.4 36.0 >>> df.rdiv(10) - A B -0 2.500000 3.333333 -1 1.666667 2.000000 -2 1.250000 1.111111 + angles degrees +circle inf 0.027778 +triangle 3.333333 0.055556 +rectangle 2.500000 0.027778 Subtract a list and Series by axis with operator version. >>> df - [1, 2] - A B -0 3 1 -1 5 3 -2 7 7 + angles degrees +circle -1 358 +triangle 2 178 +rectangle 3 358 >>> df.sub([1, 2], axis='columns') - A B -0 3 1 -1 5 3 -2 7 7 - ->>> df.sub(pd.Series([1, 2, 4]), axis='index') - A B -0 3 2 -1 4 3 -2 4 5 + angles degrees +circle -1 358 +triangle 2 178 +rectangle 3 358 + +>>> df.sub(pd.Series([1, 1, 1], index=['circle', 'triangle', 'rectangle']), +... axis='index') + angles degrees +circle -1 359 +triangle 2 179 +rectangle 3 359 Multiply a DataFrame of different shape with operator version. ->>> other = pd.DataFrame({{'A': [2, 5, 3, 1]}}) +>>> other = pd.DataFrame({{'angles': [0, 3, 4]}}, +... index=['circle', 'triangle', 'rectangle']) >>> other - A -0 2 -1 5 -2 3 -3 1 + angles +circle 0 +triangle 3 +rectangle 4 >>> df * other - A B -0 8.0 NaN -1 30.0 NaN -2 24.0 NaN -3 NaN NaN + angles degrees +circle 0 NaN +triangle 9 NaN +rectangle 16 NaN >>> df.mul(other, fill_value=0) - A B -0 8.0 0.0 -1 30.0 0.0 -2 24.0 0.0 -3 0.0 NaN + angles degrees +circle 0 0.0 +triangle 9 0.0 +rectangle 16 0.0 Divide by a MultiIndex by level. ->>> df_multindex = pd.DataFrame({{'A': [2, 4, 6, 8, 3, 4], -... 'B': [1, 3, 5, 7, 5, 6]}}, -... index = [['Q1', 'Q1', 'Q1', 'Q2', 'Q2', 'Q2'], -... [0, 1, 2, 0, 1, 2]]) +>>> df_multindex = pd.DataFrame({{'angles': [0, 3, 4, 4, 5, 6], +... 'degrees': [360, 180, 360, 360, 540, 720]}}, +... index = [['A', 'A', 'A', 'B', 'B', 'B'], +... ['circle', 'triangle', 'rectangle', +... 'square', 'pentagon', 'hexagon']]) >>> df_multindex - A B -Q1 0 2 1 - 1 4 3 - 2 6 5 -Q2 0 8 7 - 1 3 5 - 2 4 6 - ->>> df.div(df_multindex, level=1) - A B -Q1 0 2.000000 3.000000 - 1 1.500000 1.666667 - 2 1.333333 1.800000 -Q2 0 0.500000 0.428571 - 1 2.000000 1.000000 - 2 2.000000 1.500000 + angles degrees +A circle 0 360 + triangle 3 180 + rectangle 4 360 +B square 4 360 + pentagon 5 540 + hexagon 6 720 + +>>> df.div(df_multindex, level=1, fill_value=0) + angles degrees +A circle NaN 1.0 + triangle 1.0 1.0 + rectangle 1.0 1.0 +B square 0.0 0.0 + pentagon 0.0 0.0 + hexagon 0.0 0.0 """ _flex_comp_doc_FRAME = """ From 722ae81e803b437127dda4999997111738e91019 Mon Sep 17 00:00:00 2001 From: ParfaitG Date: Thu, 27 Sep 2018 18:43:11 -0500 Subject: [PATCH 17/23] Updated doctests with core/ops.py --- ci/df_info.txt | 8 ++++++++ ci/doctests.sh | 27 +++++++++++++++++---------- pandas/core/ops.py | 11 +++++------ setup.cfg | 1 - 4 files changed, 30 insertions(+), 17 deletions(-) create mode 100644 ci/df_info.txt diff --git a/ci/df_info.txt b/ci/df_info.txt new file mode 100644 index 0000000000000..fe045f094f7e7 --- /dev/null +++ b/ci/df_info.txt @@ -0,0 +1,8 @@ + +RangeIndex: 5 entries, 0 to 4 +Data columns (total 3 columns): +int_col 5 non-null int64 +text_col 5 non-null object +float_col 5 non-null float64 +dtypes: float64(1), int64(1), object(1) +memory usage: 200.0+ bytes diff --git a/ci/doctests.sh b/ci/doctests.sh index b3d7f6785815a..6a83f4013f326 100755 --- a/ci/doctests.sh +++ b/ci/doctests.sh @@ -20,22 +20,29 @@ if [ "$DOCTEST" ]; then # fi # DataFrame / Series docstrings - pytest --doctest-modules -v pandas/core/frame.py \ - -k"-axes -combine -itertuples -join -nlargest -nsmallest -nunique -pivot_table -quantile -query -reindex -reindex_axis -replace -round -set_index -stack -to_dict -to_stata" + pytest --doctest-modules -v ../pandas/core/frame.py \ + -k"-axes -combine -itertuples -join -nlargest -nsmallest -nunique -pivot_table -quantile -query -reindex -reindex_axis -replace -round -set_index -stack -to_dict -to_stata -to_excel -to_parquet" if [ $? -ne "0" ]; then RET=1 fi - pytest --doctest-modules -v pandas/core/series.py \ - -k"-nonzero -reindex -searchsorted -to_dict" + pytest --doctest-modules -v ../pandas/core/series.py \ + -k"-nonzero -reindex -searchsorted -to_dict -to_excel" if [ $? -ne "0" ]; then RET=1 fi - pytest --doctest-modules -v pandas/core/generic.py \ - -k"-_set_axis_name -_xs -describe -droplevel -groupby -interpolate -pct_change -pipe -reindex -reindex_axis -resample -sample -to_json -transpose -values -xs" + pytest --doctest-modules -v ../pandas/core/generic.py \ + -k"-_set_axis_name -_xs -describe -droplevel -groupby -interpolate -pct_change -pipe -reindex -reindex_axis -resample -sample -to_json -transpose -values -xs -to_hdf -to_xarray" + + if [ $? -ne "0" ]; then + RET=1 + fi + + pytest --doctest-modules -v ../pandas/core/ops.py \ + -k"-_gen_eval_kwargs" if [ $? -ne "0" ]; then RET=1 @@ -43,10 +50,10 @@ if [ "$DOCTEST" ]; then # top-level reshaping functions pytest --doctest-modules -v \ - pandas/core/reshape/concat.py \ - pandas/core/reshape/pivot.py \ - pandas/core/reshape/reshape.py \ - pandas/core/reshape/tile.py \ + ../pandas/core/reshape/concat.py \ + ../pandas/core/reshape/pivot.py \ + ../pandas/core/reshape/reshape.py \ + ../pandas/core/reshape/tile.py \ -k"-crosstab -pivot_table -cut" if [ $? -ne "0" ]; then diff --git a/pandas/core/ops.py b/pandas/core/ops.py index 6c369d810a231..9efc4acf1a11a 100644 --- a/pandas/core/ops.py +++ b/pandas/core/ops.py @@ -527,13 +527,12 @@ def _get_op_name(op, special): """ _flex_doc_FRAME = """ -{desc} of dataframe and other, element-wise (binary operator `{op_name}` -and reverse version `{reverse}`). +{desc} of dataframe and other, element-wise (binary operator `{op_name}`). -Among flexible wrappers (`add`, `sub`, `mul`, `div`, `mod`, `pow`) to -arithmetic operators. +With reverse version, `{reverse}`. Among flexible wrappers (`add`, `sub`, +`mul`, `div`, `mod`, `pow`) to arithmetic operators. -Equivalent to `+`, `-`, `*`, `/`, `//`, `%`, **` but with support to +Equivalent to `+`, `-`, `*`, `/`, `//`, `%`, `**` but with support to substitute a fill_value for missing data in one of the inputs. Parameters @@ -626,7 +625,7 @@ def _get_op_name(op, special): triangle 2 178 rectangle 3 358 ->>> df.sub(pd.Series([1, 1, 1], index=['circle', 'triangle', 'rectangle']), +>>> df.sub(pd.Series([1, 1, 1], index=['circle', 'triangle', 'rectangle']), ... axis='index') angles degrees circle -1 359 diff --git a/setup.cfg b/setup.cfg index e4a2357def474..2a40da49d3fc8 100644 --- a/setup.cfg +++ b/setup.cfg @@ -39,7 +39,6 @@ markers = high_memory: mark a test as a high-memory only clipboard: mark a pd.read_clipboard test doctest_optionflags = NORMALIZE_WHITESPACE IGNORE_EXCEPTION_DETAIL -addopts = --strict-data-files --durations=10 [coverage:run] branch = False From 4580f7a81714ec5248286410e8e2cc48983f2398 Mon Sep 17 00:00:00 2001 From: ParfaitG Date: Thu, 27 Sep 2018 19:52:06 -0500 Subject: [PATCH 18/23] Resetting doctests and setup files --- ci/doctests.sh | 16 ++++++++-------- setup.cfg | 1 + 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/ci/doctests.sh b/ci/doctests.sh index 6a83f4013f326..989eed12eedb8 100755 --- a/ci/doctests.sh +++ b/ci/doctests.sh @@ -20,28 +20,28 @@ if [ "$DOCTEST" ]; then # fi # DataFrame / Series docstrings - pytest --doctest-modules -v ../pandas/core/frame.py \ + pytest --doctest-modules -v pandas/core/frame.py \ -k"-axes -combine -itertuples -join -nlargest -nsmallest -nunique -pivot_table -quantile -query -reindex -reindex_axis -replace -round -set_index -stack -to_dict -to_stata -to_excel -to_parquet" if [ $? -ne "0" ]; then RET=1 fi - pytest --doctest-modules -v ../pandas/core/series.py \ + pytest --doctest-modules -v pandas/core/series.py \ -k"-nonzero -reindex -searchsorted -to_dict -to_excel" if [ $? -ne "0" ]; then RET=1 fi - pytest --doctest-modules -v ../pandas/core/generic.py \ + pytest --doctest-modules -v pandas/core/generic.py \ -k"-_set_axis_name -_xs -describe -droplevel -groupby -interpolate -pct_change -pipe -reindex -reindex_axis -resample -sample -to_json -transpose -values -xs -to_hdf -to_xarray" if [ $? -ne "0" ]; then RET=1 fi - pytest --doctest-modules -v ../pandas/core/ops.py \ + pytest --doctest-modules -v pandas/core/ops.py \ -k"-_gen_eval_kwargs" if [ $? -ne "0" ]; then @@ -50,10 +50,10 @@ if [ "$DOCTEST" ]; then # top-level reshaping functions pytest --doctest-modules -v \ - ../pandas/core/reshape/concat.py \ - ../pandas/core/reshape/pivot.py \ - ../pandas/core/reshape/reshape.py \ - ../pandas/core/reshape/tile.py \ + pandas/core/reshape/concat.py \ + pandas/core/reshape/pivot.py \ + pandas/core/reshape/reshape.py \ + pandas/core/reshape/tile.py \ -k"-crosstab -pivot_table -cut" if [ $? -ne "0" ]; then diff --git a/setup.cfg b/setup.cfg index 2a40da49d3fc8..e4a2357def474 100644 --- a/setup.cfg +++ b/setup.cfg @@ -39,6 +39,7 @@ markers = high_memory: mark a test as a high-memory only clipboard: mark a pd.read_clipboard test doctest_optionflags = NORMALIZE_WHITESPACE IGNORE_EXCEPTION_DETAIL +addopts = --strict-data-files --durations=10 [coverage:run] branch = False From 49c7b820ed0cf0c52f2251ceac04367ddb1e3073 Mon Sep 17 00:00:00 2001 From: ParfaitG Date: Fri, 28 Sep 2018 21:17:50 -0500 Subject: [PATCH 19/23] Updated arithmetic doctring to use equiv variable --- pandas/core/ops.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/pandas/core/ops.py b/pandas/core/ops.py index 9efc4acf1a11a..cad6c8e3b7a50 100644 --- a/pandas/core/ops.py +++ b/pandas/core/ops.py @@ -529,11 +529,11 @@ def _get_op_name(op, special): _flex_doc_FRAME = """ {desc} of dataframe and other, element-wise (binary operator `{op_name}`). -With reverse version, `{reverse}`. Among flexible wrappers (`add`, `sub`, -`mul`, `div`, `mod`, `pow`) to arithmetic operators. +Equivalent to ``{equiv}``, but with support to substitute a fill_value +for missing data in one of the inputs. With reverse version, `{reverse}`. -Equivalent to `+`, `-`, `*`, `/`, `//`, `%`, `**` but with support to -substitute a fill_value for missing data in one of the inputs. +Among flexible wrappers (`add`, `sub`, `mul`, `div`, `mod`, `pow`) to +arithmetic operators: `+`, `-`, `*`, `/`, `//`, `%`, `**. Parameters ---------- From 1e4e450ebf3e134e33b87a3df119a0b7beccf647 Mon Sep 17 00:00:00 2001 From: ParfaitG Date: Sat, 29 Sep 2018 16:27:11 -0500 Subject: [PATCH 20/23] Remove df_info.txt generated from doctests --- ci/df_info.txt | 8 -------- 1 file changed, 8 deletions(-) delete mode 100644 ci/df_info.txt diff --git a/ci/df_info.txt b/ci/df_info.txt deleted file mode 100644 index fe045f094f7e7..0000000000000 --- a/ci/df_info.txt +++ /dev/null @@ -1,8 +0,0 @@ - -RangeIndex: 5 entries, 0 to 4 -Data columns (total 3 columns): -int_col 5 non-null int64 -text_col 5 non-null object -float_col 5 non-null float64 -dtypes: float64(1), int64(1), object(1) -memory usage: 200.0+ bytes From ec71a0403e415235265801a688a12f6c0ef6ee08 Mon Sep 17 00:00:00 2001 From: ParfaitG Date: Sat, 29 Sep 2018 19:31:54 -0500 Subject: [PATCH 21/23] Updated _gen_eval_kwargs docstring in ops.py to avoid pytest skip --- ci/doctests.sh | 3 +-- pandas/core/ops.py | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/ci/doctests.sh b/ci/doctests.sh index 989eed12eedb8..88f661e8e03ec 100755 --- a/ci/doctests.sh +++ b/ci/doctests.sh @@ -41,8 +41,7 @@ if [ "$DOCTEST" ]; then RET=1 fi - pytest --doctest-modules -v pandas/core/ops.py \ - -k"-_gen_eval_kwargs" + pytest --doctest-modules -v pandas/core/ops.py if [ $? -ne "0" ]; then RET=1 diff --git a/pandas/core/ops.py b/pandas/core/ops.py index cad6c8e3b7a50..a60a43ff4fa5b 100644 --- a/pandas/core/ops.py +++ b/pandas/core/ops.py @@ -237,7 +237,7 @@ def _gen_eval_kwargs(name): {} >>> _gen_eval_kwargs("rtruediv") - {"reversed": True, "truediv": True} + {'reversed': True, 'truediv': True} """ kwargs = {} From d34468823a4e73817feccc9f5293d58f24bb9ac8 Mon Sep 17 00:00:00 2001 From: ParfaitG Date: Sun, 14 Oct 2018 18:49:16 -0500 Subject: [PATCH 22/23] Update docstrings to conform to PEP 8 syntax --- pandas/core/ops.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/pandas/core/ops.py b/pandas/core/ops.py index 9f094e8dc3f58..e2ef1c7b48aa1 100644 --- a/pandas/core/ops.py +++ b/pandas/core/ops.py @@ -575,7 +575,7 @@ def _get_op_name(op, special): -------- >>> df = pd.DataFrame({{'angles': [0, 3, 4], ... 'degrees': [360, 180, 360]}}, -... index=[ 'circle', 'triangle', 'rectangle']) +... index=['circle', 'triangle', 'rectangle']) >>> df angles degrees circle 0 360 @@ -658,9 +658,9 @@ def _get_op_name(op, special): >>> df_multindex = pd.DataFrame({{'angles': [0, 3, 4, 4, 5, 6], ... 'degrees': [360, 180, 360, 360, 540, 720]}}, -... index = [['A', 'A', 'A', 'B', 'B', 'B'], -... ['circle', 'triangle', 'rectangle', -... 'square', 'pentagon', 'hexagon']]) +... index=[['A', 'A', 'A', 'B', 'B', 'B'], +... ['circle', 'triangle', 'rectangle', +... 'square', 'pentagon', 'hexagon']]) >>> df_multindex angles degrees A circle 0 360 @@ -727,7 +727,7 @@ def _get_op_name(op, special): -------- >>> df = pd.DataFrame({{'cost': [250, 150, 100], ... 'revenue': [100, 250, 300]}}, -... index = ['A', 'B', 'C']) +... index=['A', 'B', 'C']) >>> df cost revenue A 250 100 @@ -780,7 +780,7 @@ def _get_op_name(op, special): Compare to a DataFrame of different shape. >>> other = pd.DataFrame({{'revenue': [300, 250, 100, 150]}}, -... index = ['A', 'B', 'C', 'D']) +... index=['A', 'B', 'C', 'D']) >>> other revenue A 300 @@ -799,8 +799,8 @@ def _get_op_name(op, special): >>> df_multindex = pd.DataFrame({{'cost': [250, 150, 100, 150, 300, 220], ... 'revenue': [100, 250, 300, 200, 175, 225]}}, -... index = [['Q1', 'Q1', 'Q1', 'Q2', 'Q2', 'Q2'], -... ['A', 'B', 'C', 'A', 'B' ,'C']]) +... index=[['Q1', 'Q1', 'Q1', 'Q2', 'Q2', 'Q2'], +... ['A', 'B', 'C', 'A', 'B' ,'C']]) >>> df_multindex cost revenue Q1 A 250 100 From eaaee0dcddee58a372753e9132d199513986c8f4 Mon Sep 17 00:00:00 2001 From: ParfaitG Date: Mon, 15 Oct 2018 22:01:58 -0500 Subject: [PATCH 23/23] Slight indentation fixes --- pandas/core/ops.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/pandas/core/ops.py b/pandas/core/ops.py index e2ef1c7b48aa1..86a9cd5f06cfb 100644 --- a/pandas/core/ops.py +++ b/pandas/core/ops.py @@ -727,7 +727,7 @@ def _get_op_name(op, special): -------- >>> df = pd.DataFrame({{'cost': [250, 150, 100], ... 'revenue': [100, 250, 300]}}, -... index=['A', 'B', 'C']) +... index=['A', 'B', 'C']) >>> df cost revenue A 250 100 @@ -780,7 +780,7 @@ def _get_op_name(op, special): Compare to a DataFrame of different shape. >>> other = pd.DataFrame({{'revenue': [300, 250, 100, 150]}}, -... index=['A', 'B', 'C', 'D']) +... index=['A', 'B', 'C', 'D']) >>> other revenue A 300 @@ -799,8 +799,8 @@ def _get_op_name(op, special): >>> df_multindex = pd.DataFrame({{'cost': [250, 150, 100, 150, 300, 220], ... 'revenue': [100, 250, 300, 200, 175, 225]}}, -... index=[['Q1', 'Q1', 'Q1', 'Q2', 'Q2', 'Q2'], -... ['A', 'B', 'C', 'A', 'B' ,'C']]) +... index=[['Q1', 'Q1', 'Q1', 'Q2', 'Q2', 'Q2'], +... ['A', 'B', 'C', 'A', 'B' ,'C']]) >>> df_multindex cost revenue Q1 A 250 100