diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 5200ad0ba0d23..16196ed5b5fb4 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -3740,7 +3740,7 @@ def lookup(self, row_labels, col_labels): # ---------------------------------------------------------------------- # Reindexing and alignment - def _reindex_axes(self, axes, level, limit, tolerance, method, fill_value, copy): + def _reindex_axes(self, axes, level, limit, tolerance, method, fill_value, copy, allow_dups): frame = self columns = axes["columns"] @@ -3752,7 +3752,7 @@ def _reindex_axes(self, axes, level, limit, tolerance, method, fill_value, copy) index = axes["index"] if index is not None: frame = frame._reindex_index( - index, method, copy, level, fill_value, limit, tolerance + index, method, copy, level, fill_value, limit, tolerance, allow_dups ) return frame @@ -3766,6 +3766,7 @@ def _reindex_index( fill_value=np.nan, limit=None, tolerance=None, + allow_dups=False ): new_index, indexer = self.index.reindex( new_index, method=method, level=level, limit=limit, tolerance=tolerance @@ -3774,7 +3775,7 @@ def _reindex_index( {0: [new_index, indexer]}, copy=copy, fill_value=fill_value, - allow_dups=False, + allow_dups=allow_dups ) def _reindex_columns( diff --git a/pandas/core/generic.py b/pandas/core/generic.py index a135f567fe6f4..98e17a98f0db5 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -3829,7 +3829,7 @@ def _is_view(self): """Return boolean indicating if self is view of another array """ return self._data.is_view - def reindex_like(self, other, method=None, copy=True, limit=None, tolerance=None): + def reindex_like(self, other, method=None, copy=True, limit=None, tolerance=None, allow_dups=False): """ Return an object with matching indices as other object. @@ -3930,6 +3930,7 @@ def reindex_like(self, other, method=None, copy=True, limit=None, tolerance=None copy=copy, limit=limit, tolerance=tolerance, + allow_dups=allow_dups ) return self.reindex(**d) @@ -4528,6 +4529,7 @@ def reindex(self, *args, **kwargs): limit = kwargs.pop("limit", None) tolerance = kwargs.pop("tolerance", None) fill_value = kwargs.pop("fill_value", None) + allow_dups = kwargs.pop("allow_dups", False) # Series.reindex doesn't use / need the axis kwarg # We pop and ignore it here, to make writing Series/Frame generic code @@ -4562,7 +4564,7 @@ def reindex(self, *args, **kwargs): # perform the reindex on the axes return self._reindex_axes( - axes, level, limit, tolerance, method, fill_value, copy + axes, level, limit, tolerance, method, fill_value, copy, allow_dups ).__finalize__(self) def _reindex_axes(self, axes, level, limit, tolerance, method, fill_value, copy): @@ -4583,7 +4585,7 @@ def _reindex_axes(self, axes, level, limit, tolerance, method, fill_value, copy) {axis: [new_index, indexer]}, fill_value=fill_value, copy=copy, - allow_dups=False, + allow_dups=allow_dups, ) return obj diff --git a/pandas/util/testing.py b/pandas/util/testing.py index a34fdee227afc..e5f6aaaef9b66 100644 --- a/pandas/util/testing.py +++ b/pandas/util/testing.py @@ -1233,6 +1233,7 @@ def assert_frame_equal( check_datetimelike_compat=False, check_categorical=True, check_like=False, + check_dups_index=False, obj="DataFrame", ): """ @@ -1344,7 +1345,7 @@ def assert_frame_equal( ) if check_like: - left, right = left.reindex_like(right), right + left, right = left.reindex_like(right,allow_dups=check_dups_index), right # index comparison assert_index_equal(