Skip to content

Commit 4ed8313

Browse files
HagaiHargiljreback
authored andcommitted
DOC: Clarify and add fill_value example in arithmetic ops (#19675)
1 parent 842d350 commit 4ed8313

File tree

1 file changed

+90
-7
lines changed

1 file changed

+90
-7
lines changed

pandas/core/ops.py

+90-7
Original file line numberDiff line numberDiff line change
@@ -408,8 +408,10 @@ def _get_op_name(op, special):
408408
----------
409409
other : Series or scalar value
410410
fill_value : None or float value, default None (NaN)
411-
Fill missing (NaN) values with this value. If both Series are
412-
missing, the result will be missing
411+
Fill existing missing (NaN) values, and any new element needed for
412+
successful Series alignment, with this value before computation.
413+
If data in both corresponding Series locations is missing
414+
the result will be missing
413415
level : int or name
414416
Broadcast across a level, matching Index values on the
415417
passed MultiIndex level
@@ -418,6 +420,30 @@ def _get_op_name(op, special):
418420
-------
419421
result : Series
420422
423+
Examples
424+
--------
425+
>>> a = pd.Series([1, 1, 1, np.nan], index=['a', 'b', 'c', 'd'])
426+
>>> a
427+
a 1.0
428+
b 1.0
429+
c 1.0
430+
d NaN
431+
dtype: float64
432+
>>> b = pd.Series([1, np.nan, 1, np.nan], index=['a', 'b', 'd', 'e'])
433+
>>> b
434+
a 1.0
435+
b NaN
436+
d 1.0
437+
e NaN
438+
dtype: float64
439+
>>> a.add(b, fill_value=0)
440+
a 2.0
441+
b 1.0
442+
c 1.0
443+
d 1.0
444+
e NaN
445+
dtype: float64
446+
421447
See also
422448
--------
423449
Series.{reverse}
@@ -433,8 +459,10 @@ def _get_op_name(op, special):
433459
axis : {0, 1, 'index', 'columns'}
434460
For Series input, axis to match Series index on
435461
fill_value : None or float value, default None
436-
Fill missing (NaN) values with this value. If both DataFrame locations are
437-
missing, the result will be missing
462+
Fill existing missing (NaN) values, and any new element needed for
463+
successful DataFrame alignment, with this value before computation.
464+
If data in both corresponding DataFrame locations is missing
465+
the result will be missing
438466
level : int or name
439467
Broadcast across a level, matching Index values on the
440468
passed MultiIndex level
@@ -446,6 +474,33 @@ def _get_op_name(op, special):
446474
Returns
447475
-------
448476
result : DataFrame
477+
478+
Examples
479+
--------
480+
>>> a = pd.DataFrame([1, 1, 1, np.nan], index=['a', 'b', 'c', 'd'],
481+
columns=['one'])
482+
>>> a
483+
one
484+
a 1.0
485+
b 1.0
486+
c 1.0
487+
d NaN
488+
>>> b = pd.DataFrame(dict(one=[1, np.nan, 1, np.nan],
489+
two=[np.nan, 2, np.nan, 2]),
490+
index=['a', 'b', 'd', 'e'])
491+
>>> b
492+
one two
493+
a 1.0 NaN
494+
b NaN 2.0
495+
d 1.0 NaN
496+
e NaN 2.0
497+
>>> a.add(b, fill_value=0)
498+
one two
499+
a 2.0 NaN
500+
b 1.0 2.0
501+
c 1.0 NaN
502+
d 1.0 NaN
503+
e NaN 2.0
449504
"""
450505

451506
_flex_doc_FRAME = """
@@ -460,8 +515,10 @@ def _get_op_name(op, special):
460515
axis : {{0, 1, 'index', 'columns'}}
461516
For Series input, axis to match Series index on
462517
fill_value : None or float value, default None
463-
Fill missing (NaN) values with this value. If both DataFrame
464-
locations are missing, the result will be missing
518+
Fill existing missing (NaN) values, and any new element needed for
519+
successful DataFrame alignment, with this value before computation.
520+
If data in both corresponding DataFrame locations is missing
521+
the result will be missing
465522
level : int or name
466523
Broadcast across a level, matching Index values on the
467524
passed MultiIndex level
@@ -474,6 +531,33 @@ def _get_op_name(op, special):
474531
-------
475532
result : DataFrame
476533
534+
Examples
535+
--------
536+
>>> a = pd.DataFrame([1, 1, 1, np.nan], index=['a', 'b', 'c', 'd'],
537+
columns=['one'])
538+
>>> a
539+
one
540+
a 1.0
541+
b 1.0
542+
c 1.0
543+
d NaN
544+
>>> b = pd.DataFrame(dict(one=[1, np.nan, 1, np.nan],
545+
two=[np.nan, 2, np.nan, 2]),
546+
index=['a', 'b', 'd', 'e'])
547+
>>> b
548+
one two
549+
a 1.0 NaN
550+
b NaN 2.0
551+
d 1.0 NaN
552+
e NaN 2.0
553+
>>> a.add(b, fill_value=0)
554+
one two
555+
a 2.0 NaN
556+
b 1.0 2.0
557+
c 1.0 NaN
558+
d 1.0 NaN
559+
e NaN 2.0
560+
477561
See also
478562
--------
479563
DataFrame.{reverse}
@@ -545,7 +629,6 @@ def _make_flex_doc(op_name, typ):
545629
base_doc = _flex_doc_PANEL
546630
else:
547631
raise AssertionError('Invalid typ argument.')
548-
549632
doc = base_doc.format(desc=op_desc['desc'], op_name=op_name,
550633
equiv=equiv, reverse=op_desc['reverse'])
551634
return doc

0 commit comments

Comments
 (0)