Series.compare(other: Series, align_axis: Axis = 1, keep_shape: bool = False, keep_equal: bool = False, result_names: Suffixes = ('self', 'other')) DataFrame | Series[source]#

Compare to another Series and show the differences.

  • other (Series) – Object to compare with.

  • align_axis ({0 or 'index', 1 or 'columns'}, default 1) –

    Determine which axis to align the comparison on.

    • 0, or ‘index’Resulting differences are stacked vertically

      with rows drawn alternately from self and other.

    • 1, or ‘columns’Resulting differences are aligned horizontally

      with columns drawn alternately from self and other.

  • keep_shape (bool, default False) – If true, all rows and columns are kept. Otherwise, only the ones with different values are kept.

  • keep_equal (bool, default False) – If true, the result keeps values that are equal. Otherwise, equal values are shown as NaNs.

  • result_names (tuple, default ('self', 'other')) –

    Set the dataframes names in the comparison.

    New in version 1.5.0(pandas).


If axis is 0 or ‘index’ the result will be a Series. The resulting index will be a MultiIndex with ‘self’ and ‘other’ stacked alternately at the inner level.

If axis is 1 or ‘columns’ the result will be a DataFrame. It will have two columns namely ‘self’ and ‘other’.

Return type

Series or DataFrame

See also


Compare with another DataFrame and show differences.


Matching NaNs will not appear as a difference.


>>> s1 = pd.Series(["a", "b", "c", "d", "e"])  
>>> s2 = pd.Series(["a", "a", "c", "b", "e"])  

Align the differences on columns

>>> s1.compare(s2)  
  self other
1    b     a
3    d     b

Stack the differences on indices

>>> s1.compare(s2, align_axis=0)  
1  self     b
   other    a
3  self     d
   other    b
dtype: object

Keep all original rows

>>> s1.compare(s2, keep_shape=True)  
  self other
0  NaN   NaN
1    b     a
2  NaN   NaN
3    d     b
4  NaN   NaN

Keep all original rows and also all original values

>>> s1.compare(s2, keep_shape=True, keep_equal=True)  
  self other
0    a     a
1    b     a
2    c     c
3    d     b
4    e     e


This method has not been implemented yet. Xorbits will try to execute it with pandas.

This docstring was copied from pandas.core.series.Series.