Repository: pandas-dev/pandas

Issue:
BUG: ValueError in Series.divmod 
#### Code Sample, a copy-pastable example if possible

```python
import pandas as pd
import numpy as np
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', 'd', 'e'])

##Working:
divmod(a,b)

##Fails:
a.divmod(b)

```
#### Problem description

divmod(series_a,series_b) works as expected, but series_a.divmod(b) returns the following error:

```
>>> a.divmod(b)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/danlaw/Projects/pandas/pandas/core/ops.py", line 1892, in flex_wrapper
    return self._binop(other, op, level=level, fill_value=fill_value)
  File "/Users/danlaw/Projects/pandas/pandas/core/series.py", line 2522, in _binop
    result = self._constructor(result, index=new_index, name=name)
  File "/Users/danlaw/Projects/pandas/pandas/core/series.py", line 250, in __init__
    .format(val=len(data), ind=len(index)))
ValueError: Length of passed values is 2, index implies 4
```
#### Expected Output
```python
(a    0.0
b    0.0
c    NaN
d    NaN
e    NaN
dtype: float64, a    1.0
b    1.0
c    NaN
d    NaN
e    NaN
dtype: float64)
```

#### Output of ``pd.show_versions()``

<details>
commit: 221be3b4adde0f45927803b1c593b56d4678faeb
python: 3.7.2.final.0
python-bits: 64
OS: Darwin
OS-release: 16.7.0
machine: x86_64
processor: i386
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8
LOCALE: en_US.UTF-8

pandas: 0.25.0.dev0+200.g221be3b4a
pytest: 4.3.0
pip: 19.0.3
setuptools: 40.8.0
Cython: 0.29.5
numpy: 1.16.2
scipy: 1.2.1
pyarrow: 0.11.1
xarray: 0.11.3
IPython: 7.3.0
sphinx: 1.8.4
patsy: 0.5.1
dateutil: 2.8.0
pytz: 2018.9
blosc: None
bottleneck: 1.2.1
tables: 3.4.4
numexpr: 2.6.9
feather: None
matplotlib: 3.0.2
openpyxl: 2.6.0
xlrd: 1.2.0
xlwt: 1.3.0
xlsxwriter: 1.1.5
lxml.etree: 4.3.1
bs4: 4.7.1
html5lib: 1.0.1
sqlalchemy: 1.2.18
pymysql: None
psycopg2: None
jinja2: 2.10
s3fs: 0.2.0
fastparquet: 0.2.1
pandas_gbq: None
pandas_datareader: None
gcsfs: None
</details>



Discussion:
I will look at this issue.

Resolving patch:
diff --git a/pandas/core/ops.py b/pandas/core/ops.py
--- a/pandas/core/ops.py
+++ b/pandas/core/ops.py
@@ -1660,7 +1660,7 @@ def _construct_result(left, result, index, name, dtype=None):
     not be enough; we still need to override the name attribute.
     """
     out = left._constructor(result, index=index, dtype=dtype)
-
+    out = out.__finalize__(left)
     out.name = name
     return out
 
@@ -1668,10 +1668,11 @@ def _construct_result(left, result, index, name, dtype=None):
 def _construct_divmod_result(left, result, index, name, dtype=None):
     """divmod returns a tuple of like indexed series instead of a single series.
     """
-    constructor = left._constructor
     return (
-        constructor(result[0], index=index, name=name, dtype=dtype),
-        constructor(result[1], index=index, name=name, dtype=dtype),
+        _construct_result(left, result[0], index=index, name=name,
+                          dtype=dtype),
+        _construct_result(left, result[1], index=index, name=name,
+                          dtype=dtype),
     )
 
 
diff --git a/pandas/core/series.py b/pandas/core/series.py
--- a/pandas/core/series.py
+++ b/pandas/core/series.py
@@ -2527,6 +2527,7 @@ def _binop(self, other, func, level=None, fill_value=None):
         -------
         Series
         """
+
         if not isinstance(other, Series):
             raise AssertionError('Other operand must be Series')
 
@@ -2543,13 +2544,13 @@ def _binop(self, other, func, level=None, fill_value=None):
 
         with np.errstate(all='ignore'):
             result = func(this_vals, other_vals)
+
         name = ops.get_op_result_name(self, other)
-        result = self._constructor(result, index=new_index, name=name)
-        result = result.__finalize__(self)
-        if name is None:
-            # When name is None, __finalize__ overwrites current name
-            result.name = None
-        return result
+        if func.__name__ in ['divmod', 'rdivmod']:
+            ret = ops._construct_divmod_result(self, result, new_index, name)
+        else:
+            ret = ops._construct_result(self, result, new_index, name)
+        return ret
 
     def combine(self, other, func, fill_value=None):
         """


Test patch:
diff --git a/pandas/tests/series/test_operators.py b/pandas/tests/series/test_operators.py
--- a/pandas/tests/series/test_operators.py
+++ b/pandas/tests/series/test_operators.py
@@ -741,6 +741,21 @@ def test_op_duplicate_index(self):
         expected = pd.Series([11, 12, np.nan], index=[1, 1, 2])
         assert_series_equal(result, expected)
 
+    def test_divmod(self):
+        # GH25557
+        a = Series([1, 1, 1, np.nan], index=['a', 'b', 'c', 'd'])
+        b = Series([2, np.nan, 1, np.nan], index=['a', 'b', 'd', 'e'])
+
+        result = a.divmod(b)
+        expected = divmod(a, b)
+        assert_series_equal(result[0], expected[0])
+        assert_series_equal(result[1], expected[1])
+
+        result = a.rdivmod(b)
+        expected = divmod(b, a)
+        assert_series_equal(result[0], expected[0])
+        assert_series_equal(result[1], expected[1])
+
 
 class TestSeriesUnaryOps(object):
     # __neg__, __pos__, __inv__