From 24d704a81b1373bb13bee2107934e771823870a4 Mon Sep 17 00:00:00 2001 From: Hagai Hargil Date: Tue, 13 Feb 2018 14:41:06 +0200 Subject: [PATCH 1/9] Clarified fill_value parameter in arithmetic ops --- pandas/core/ops.py | 36 ++++++++++++++++++++++++++++++++---- 1 file changed, 32 insertions(+), 4 deletions(-) diff --git a/pandas/core/ops.py b/pandas/core/ops.py index 4c234ccb4dd47..4885a4447fa78 100644 --- a/pandas/core/ops.py +++ b/pandas/core/ops.py @@ -255,8 +255,10 @@ def _get_frame_op_default_axis(name): ---------- other : Series or scalar value fill_value : None or float value, default None (NaN) - Fill missing (NaN) values with this value. If both Series are - missing, the result will be missing + Fill existing missing (NaN) values, and any new element needed for + successful array alignment, with this value before computation. + If data in both corresponding DataFrame locations is missing + the result will be missing level : int or name Broadcast across a level, matching Index values on the passed MultiIndex level @@ -265,6 +267,18 @@ def _get_frame_op_default_axis(name): ------- result : Series +Examples +-------- +>>> a = pd.Series([1, 1, 1, np.nan], index=['a', 'b', 'c', 'd']) +>>> b = pd.Series([1, np.nan, 1, np.nan], index=['a', 'b', 'c_', 'd']) +>>> a.add(b, fill_value=0) +a 2.0 +b 1.0 +c 1.0 +c_ 1.0 +d NaN +dtype: float64 + See also -------- Series.{reverse} @@ -280,8 +294,10 @@ def _get_frame_op_default_axis(name): axis : {0, 1, 'index', 'columns'} For Series input, axis to match Series index on fill_value : None or float value, default None - Fill missing (NaN) values with this value. If both DataFrame locations are - missing, the result will be missing + Fill existing missing (NaN) values, and any new element needed for + successful array alignment, with this value before computation. + If data in both corresponding DataFrame locations is missing + the result will be missing level : int or name Broadcast across a level, matching Index values on the passed MultiIndex level @@ -293,6 +309,18 @@ def _get_frame_op_default_axis(name): Returns ------- result : DataFrame + +Examples +-------- +>>> a = pd.Series([1, 1, 1, np.nan], index=['a', 'b', 'c', 'd']) +>>> b = pd.Series([1, np.nan, 1, np.nan], index=['a', 'b', 'c_', 'd']) +>>> a.add(b, fill_value=0) +a 2.0 +b 1.0 +c 1.0 +c_ 1.0 +d NaN +dtype: float64 """ _flex_doc_FRAME = """ From b5d9d6faf5d862a330845322872d199558933166 Mon Sep 17 00:00:00 2001 From: Hagai Hargil Date: Tue, 13 Feb 2018 16:24:55 +0200 Subject: [PATCH 2/9] Add fill_value change to DF --- pandas/core/ops.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/pandas/core/ops.py b/pandas/core/ops.py index 4885a4447fa78..d8345ef1f9393 100644 --- a/pandas/core/ops.py +++ b/pandas/core/ops.py @@ -335,8 +335,10 @@ def _get_frame_op_default_axis(name): axis : {{0, 1, 'index', 'columns'}} For Series input, axis to match Series index on fill_value : None or float value, default None - Fill missing (NaN) values with this value. If both DataFrame - locations are missing, the result will be missing + Fill existing missing (NaN) values, and any new element needed for + successful array alignment, with this value before computation. + If data in both corresponding DataFrame locations is missing + the result will be missing level : int or name Broadcast across a level, matching Index values on the passed MultiIndex level From c45dbd9b5908a64ec4c879ee9c1b611f5b0819ce Mon Sep 17 00:00:00 2001 From: Hagai Hargil Date: Tue, 13 Feb 2018 16:30:10 +0200 Subject: [PATCH 3/9] Add fill_value example to DF --- pandas/core/ops.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/pandas/core/ops.py b/pandas/core/ops.py index d8345ef1f9393..a34b53be03625 100644 --- a/pandas/core/ops.py +++ b/pandas/core/ops.py @@ -351,6 +351,18 @@ def _get_frame_op_default_axis(name): ------- result : DataFrame +Examples +-------- +>>> a = pd.Series([1, 1, 1, np.nan], index=['a', 'b', 'c', 'd']) +>>> b = pd.Series([1, np.nan, 1, np.nan], index=['a', 'b', 'c_', 'd']) +>>> a.add(b, fill_value=0) + 0 +a 2.0 +b 1.0 +c 1.0 +c_ 1.0 +d NaN + See also -------- DataFrame.{reverse} From b2794d91a52faa667e69150b535f7270019dc8d9 Mon Sep 17 00:00:00 2001 From: Hagai Hargil Date: Tue, 13 Feb 2018 18:23:42 +0200 Subject: [PATCH 4/9] Rephrasing and PEP8 fixes --- pandas/core/ops.py | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/pandas/core/ops.py b/pandas/core/ops.py index a34b53be03625..afda73fa667a4 100644 --- a/pandas/core/ops.py +++ b/pandas/core/ops.py @@ -255,9 +255,9 @@ def _get_frame_op_default_axis(name): ---------- 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 array alignment, with this value before computation. - If data in both corresponding DataFrame locations is missing + Fill existing missing (NaN) values, and any new element needed for + successful array 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 @@ -294,9 +294,9 @@ def _get_frame_op_default_axis(name): axis : {0, 1, 'index', 'columns'} For Series input, axis to match Series index on fill_value : None or float value, default None - Fill existing missing (NaN) values, and any new element needed for - successful array alignment, with this value before computation. - If data in both corresponding DataFrame locations is missing + Fill existing missing (NaN) values, and any new element needed for + successful array alignment, with this value before computation. + If data in both corresponding DataFrame locations is missing the result will be missing level : int or name Broadcast across a level, matching Index values on the @@ -312,15 +312,15 @@ def _get_frame_op_default_axis(name): Examples -------- ->>> a = pd.Series([1, 1, 1, np.nan], index=['a', 'b', 'c', 'd']) ->>> b = pd.Series([1, np.nan, 1, np.nan], index=['a', 'b', 'c_', 'd']) +>>> a = pd.DataFrame([1, 1, 1, np.nan], index=['a', 'b', 'c', 'd']) +>>> b = pd.DataFrame([1, np.nan, 1, np.nan], index=['a', 'b', 'c_', 'd']) >>> a.add(b, fill_value=0) -a 2.0 -b 1.0 -c 1.0 -c_ 1.0 -d NaN -dtype: float64 + 0 +a 2.0 +b 1.0 +c 1.0 +c_ 1.0 +d NaN """ _flex_doc_FRAME = """ @@ -335,9 +335,9 @@ def _get_frame_op_default_axis(name): axis : {{0, 1, 'index', 'columns'}} For Series input, axis to match Series index on fill_value : None or float value, default None - Fill existing missing (NaN) values, and any new element needed for - successful array alignment, with this value before computation. - If data in both corresponding DataFrame locations is missing + Fill existing missing (NaN) values, and any new element needed for + successful array alignment, with this value before computation. + If data in both corresponding DataFrame locations is missing the result will be missing level : int or name Broadcast across a level, matching Index values on the @@ -353,8 +353,8 @@ def _get_frame_op_default_axis(name): Examples -------- ->>> a = pd.Series([1, 1, 1, np.nan], index=['a', 'b', 'c', 'd']) ->>> b = pd.Series([1, np.nan, 1, np.nan], index=['a', 'b', 'c_', 'd']) +>>> a = pd.DataFrame([1, 1, 1, np.nan], index=['a', 'b', 'c', 'd']) +>>> b = pd.DataFrame([1, np.nan, 1, np.nan], index=['a', 'b', 'c_', 'd']) >>> a.add(b, fill_value=0) 0 a 2.0 From 13f1f003cc5bd5210e0413895cf838c5894d5902 Mon Sep 17 00:00:00 2001 From: Hagai Hargil Date: Sun, 18 Feb 2018 09:24:00 +0200 Subject: [PATCH 5/9] Two-columned DataFrame --- pandas/core/ops.py | 56 +++++++++++++++++++++++++++++++++++----------- 1 file changed, 43 insertions(+), 13 deletions(-) diff --git a/pandas/core/ops.py b/pandas/core/ops.py index afda73fa667a4..d78e082f6dce4 100644 --- a/pandas/core/ops.py +++ b/pandas/core/ops.py @@ -270,7 +270,17 @@ def _get_frame_op_default_axis(name): Examples -------- >>> a = pd.Series([1, 1, 1, np.nan], index=['a', 'b', 'c', 'd']) +a 1.0 +b 1.0 +c 1.0 +d NaN +dtype: float64 >>> b = pd.Series([1, np.nan, 1, np.nan], index=['a', 'b', 'c_', 'd']) +a 1.0 +b NaN +c_ 1.0 +d NaN +dtype: float64 >>> a.add(b, fill_value=0) a 2.0 b 1.0 @@ -313,14 +323,24 @@ def _get_frame_op_default_axis(name): Examples -------- >>> a = pd.DataFrame([1, 1, 1, np.nan], index=['a', 'b', 'c', 'd']) ->>> b = pd.DataFrame([1, np.nan, 1, np.nan], index=['a', 'b', 'c_', 'd']) + 0 +a 1.0 +b 1.0 +c 1.0 +d NaN +>>> b = pd.DataFrame([[1, np.nan], [np.nan, 2], [1, np.nan], [np.nan, 2]], index=['a', 'b', 'c_', 'd']) + 0 1 +a 1.0 NaN +b NaN 2.0 +c_ 1.0 NaN +d NaN 2.0 >>> a.add(b, fill_value=0) - 0 -a 2.0 -b 1.0 -c 1.0 -c_ 1.0 -d NaN + 0 1 +a 2.0 NaN +b 1.0 2.0 +c 1.0 NaN +c_ 1.0 NaN +d NaN 2.0 """ _flex_doc_FRAME = """ @@ -354,14 +374,24 @@ def _get_frame_op_default_axis(name): Examples -------- >>> a = pd.DataFrame([1, 1, 1, np.nan], index=['a', 'b', 'c', 'd']) + 0 +a 1.0 +b 1.0 +c 1.0 +d NaN >>> b = pd.DataFrame([1, np.nan, 1, np.nan], index=['a', 'b', 'c_', 'd']) + 0 1 +a 1.0 NaN +b NaN 2.0 +c_ 1.0 NaN +d NaN 2.0 >>> a.add(b, fill_value=0) - 0 -a 2.0 -b 1.0 -c 1.0 -c_ 1.0 -d NaN + 0 1 +a 2.0 NaN +b 1.0 2.0 +c 1.0 NaN +c_ 1.0 NaN +d NaN 2.0 See also -------- From 1bf321cba979edd39559db337f0f72dfbf40c0e5 Mon Sep 17 00:00:00 2001 From: Hagai Hargil Date: Sun, 18 Feb 2018 19:53:11 +0200 Subject: [PATCH 6/9] dict constructor and rephrasing --- pandas/core/ops.py | 32 +++++++++++++++++++------------- 1 file changed, 19 insertions(+), 13 deletions(-) diff --git a/pandas/core/ops.py b/pandas/core/ops.py index d78e082f6dce4..0a232e248ef1d 100644 --- a/pandas/core/ops.py +++ b/pandas/core/ops.py @@ -256,7 +256,7 @@ def _get_frame_op_default_axis(name): 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 array alignment, with this value before computation. + 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 @@ -305,7 +305,7 @@ def _get_frame_op_default_axis(name): For Series input, axis to match Series index on fill_value : None or float value, default None Fill existing missing (NaN) values, and any new element needed for - successful array alignment, with this value before computation. + successful DataFrame alignment, with this value before computation. If data in both corresponding DataFrame locations is missing the result will be missing level : int or name @@ -322,20 +322,23 @@ def _get_frame_op_default_axis(name): Examples -------- ->>> a = pd.DataFrame([1, 1, 1, np.nan], index=['a', 'b', 'c', 'd']) - 0 +>>> a = pd.DataFrame([1, 1, 1, np.nan], index=['a', 'b', 'c', 'd'], + columns=['one']) + one a 1.0 b 1.0 c 1.0 d NaN ->>> b = pd.DataFrame([[1, np.nan], [np.nan, 2], [1, np.nan], [np.nan, 2]], index=['a', 'b', 'c_', 'd']) - 0 1 +>>> b = pd.DataFrame({'one': [1, np.nan, 1, np.nan], + 'two': [np.nan, 2, np.nan, 2]}, + index=['a', 'b', 'c_', 'd']) + one two a 1.0 NaN b NaN 2.0 c_ 1.0 NaN d NaN 2.0 >>> a.add(b, fill_value=0) - 0 1 + one two a 2.0 NaN b 1.0 2.0 c 1.0 NaN @@ -356,7 +359,7 @@ def _get_frame_op_default_axis(name): For Series input, axis to match Series index on fill_value : None or float value, default None Fill existing missing (NaN) values, and any new element needed for - successful array alignment, with this value before computation. + successful DataFrame alignment, with this value before computation. If data in both corresponding DataFrame locations is missing the result will be missing level : int or name @@ -373,20 +376,23 @@ def _get_frame_op_default_axis(name): Examples -------- ->>> a = pd.DataFrame([1, 1, 1, np.nan], index=['a', 'b', 'c', 'd']) - 0 +>>> a = pd.DataFrame([1, 1, 1, np.nan], index=['a', 'b', 'c', 'd'], + columns=['one']) + one a 1.0 b 1.0 c 1.0 d NaN ->>> b = pd.DataFrame([1, np.nan, 1, np.nan], index=['a', 'b', 'c_', 'd']) - 0 1 +>>> b = pd.DataFrame({'one': [1, np.nan, 1, np.nan], + 'two': [np.nan, 2, np.nan, 2]}, + index=['a', 'b', 'c_', 'd']) + one two a 1.0 NaN b NaN 2.0 c_ 1.0 NaN d NaN 2.0 >>> a.add(b, fill_value=0) - 0 1 + one two a 2.0 NaN b 1.0 2.0 c 1.0 NaN From fd89953b9fcd0521000212313cf08895ab87572f Mon Sep 17 00:00:00 2001 From: Hagai Hargil Date: Mon, 19 Feb 2018 10:06:40 +0200 Subject: [PATCH 7/9] Variables are now displayed before operation --- pandas/core/ops.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/pandas/core/ops.py b/pandas/core/ops.py index 0a232e248ef1d..1962abe7f572c 100644 --- a/pandas/core/ops.py +++ b/pandas/core/ops.py @@ -270,12 +270,14 @@ def _get_frame_op_default_axis(name): 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 dtype: float64 >>> b = pd.Series([1, np.nan, 1, np.nan], index=['a', 'b', 'c_', 'd']) +>>> b a 1.0 b NaN c_ 1.0 @@ -324,6 +326,7 @@ def _get_frame_op_default_axis(name): -------- >>> a = pd.DataFrame([1, 1, 1, np.nan], index=['a', 'b', 'c', 'd'], columns=['one']) +>>> a one a 1.0 b 1.0 @@ -332,6 +335,7 @@ def _get_frame_op_default_axis(name): >>> b = pd.DataFrame({'one': [1, np.nan, 1, np.nan], 'two': [np.nan, 2, np.nan, 2]}, index=['a', 'b', 'c_', 'd']) +>>> b one two a 1.0 NaN b NaN 2.0 @@ -378,6 +382,7 @@ def _get_frame_op_default_axis(name): -------- >>> a = pd.DataFrame([1, 1, 1, np.nan], index=['a', 'b', 'c', 'd'], columns=['one']) +>>> a one a 1.0 b 1.0 @@ -386,6 +391,7 @@ def _get_frame_op_default_axis(name): >>> b = pd.DataFrame({'one': [1, np.nan, 1, np.nan], 'two': [np.nan, 2, np.nan, 2]}, index=['a', 'b', 'c_', 'd']) +>>> b one two a 1.0 NaN b NaN 2.0 From af0aebeaa5728ded007834a56961da36dbb794b2 Mon Sep 17 00:00:00 2001 From: Hagai Hargil Date: Tue, 20 Feb 2018 20:05:40 +0200 Subject: [PATCH 8/9] Fixed bug with format and dict constructor --- pandas/core/ops.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/pandas/core/ops.py b/pandas/core/ops.py index 1962abe7f572c..179f2cca02f4d 100644 --- a/pandas/core/ops.py +++ b/pandas/core/ops.py @@ -332,8 +332,8 @@ def _get_frame_op_default_axis(name): b 1.0 c 1.0 d NaN ->>> b = pd.DataFrame({'one': [1, np.nan, 1, np.nan], - 'two': [np.nan, 2, np.nan, 2]}, +>>> b = pd.DataFrame(dict(one=[1, np.nan, 1, np.nan], + two=[np.nan, 2, np.nan, 2]), index=['a', 'b', 'c_', 'd']) >>> b one two @@ -388,8 +388,8 @@ def _get_frame_op_default_axis(name): b 1.0 c 1.0 d NaN ->>> b = pd.DataFrame({'one': [1, np.nan, 1, np.nan], - 'two': [np.nan, 2, np.nan, 2]}, +>>> b = pd.DataFrame(dict(one=[1, np.nan, 1, np.nan], + two=[np.nan, 2, np.nan, 2]), index=['a', 'b', 'c_', 'd']) >>> b one two @@ -476,7 +476,6 @@ def _make_flex_doc(op_name, typ): base_doc = _flex_doc_PANEL else: raise AssertionError('Invalid typ argument.') - doc = base_doc.format(desc=op_desc['desc'], op_name=op_name, equiv=equiv, reverse=op_desc['reverse']) return doc From 3192301df1957d1165ae25feaa51165e95aa0096 Mon Sep 17 00:00:00 2001 From: Hagai Hargil Date: Wed, 21 Feb 2018 08:54:01 +0200 Subject: [PATCH 9/9] Changes index from c_ to e --- pandas/core/ops.py | 68 +++++++++++++++++++++++----------------------- 1 file changed, 34 insertions(+), 34 deletions(-) diff --git a/pandas/core/ops.py b/pandas/core/ops.py index 179f2cca02f4d..5a715f3dceb76 100644 --- a/pandas/core/ops.py +++ b/pandas/core/ops.py @@ -276,19 +276,19 @@ def _get_frame_op_default_axis(name): c 1.0 d NaN dtype: float64 ->>> b = pd.Series([1, np.nan, 1, np.nan], index=['a', 'b', 'c_', 'd']) +>>> b = pd.Series([1, np.nan, 1, np.nan], index=['a', 'b', 'd', 'e']) >>> b -a 1.0 -b NaN -c_ 1.0 -d NaN +a 1.0 +b NaN +d 1.0 +e NaN dtype: float64 >>> a.add(b, fill_value=0) -a 2.0 -b 1.0 -c 1.0 -c_ 1.0 -d NaN +a 2.0 +b 1.0 +c 1.0 +d 1.0 +e NaN dtype: float64 See also @@ -334,20 +334,20 @@ def _get_frame_op_default_axis(name): d NaN >>> b = pd.DataFrame(dict(one=[1, np.nan, 1, np.nan], two=[np.nan, 2, np.nan, 2]), - index=['a', 'b', 'c_', 'd']) + index=['a', 'b', 'd', 'e']) >>> b - one two -a 1.0 NaN -b NaN 2.0 -c_ 1.0 NaN -d NaN 2.0 + 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 -c_ 1.0 NaN -d NaN 2.0 + one two +a 2.0 NaN +b 1.0 2.0 +c 1.0 NaN +d 1.0 NaN +e NaN 2.0 """ _flex_doc_FRAME = """ @@ -390,20 +390,20 @@ def _get_frame_op_default_axis(name): d NaN >>> b = pd.DataFrame(dict(one=[1, np.nan, 1, np.nan], two=[np.nan, 2, np.nan, 2]), - index=['a', 'b', 'c_', 'd']) + index=['a', 'b', 'd', 'e']) >>> b - one two -a 1.0 NaN -b NaN 2.0 -c_ 1.0 NaN -d NaN 2.0 + 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 -c_ 1.0 NaN -d NaN 2.0 + one two +a 2.0 NaN +b 1.0 2.0 +c 1.0 NaN +d 1.0 NaN +e NaN 2.0 See also --------