Pandas version checks
-
[X] I have checked that this issue has not already been reported.
-
[X] I have confirmed this bug exists on the latest version of pandas.
-
[X] I have confirmed this bug exists on the main branch of pandas.
Reproducible Example
import pandas as pd
df1 = pd.DataFrame(
[
(20080813, "CLZ08", 1.5),
(20080813, "CLZ09", -0.4),
(20080813, "CLZ08", 1.5),
(20080814, "CLZ09", -0.4),
],
columns=["date", "asset", "value"],
).set_index(["date", "asset"])["value"]
df2 = pd.DataFrame(
[
(20080813, "CLZ08", 1.5),
(20080813, "CLZ09", -0.4),
(20080814, "CLZ08", 1.5),
(20080814, "CLZ09", -0.4),
],
columns=["date", "asset", "value"],
).set_index(["date", "asset"])["value"]
pd.testing.assert_series_equal(df1, df2, check_index_type=True, check_index=True)
Issue Description
Does not raise even though index is different
Expected Behavior
Raise because index is different
Installed Versions
Comment From: dicristina
For assert_series_equal
the default value for check_exact
is False
. assert_series_equal
goes on to call assert_index_equal
but it propagates its check_exact
value, which is False
in the example. This causes the index comparison to be done by assert_almost_equal
which does not raise because the values are in fact almost equal according to the default parameters.
The most obvious solution is to use check_exact=True
but you could also use a data type other than int
in the first index level if possible (like str
or datetime64
). Also you could use an rtol
smaller than the default like 4e-8
.
I recommend closing this issue.
Comment From: jaapaap79
Does it really make sense to have assert_almost_equal on the index regardless?
Comment From: dicristina
I guess that only for a small minority of tests would it be right to compare indices approximately but I cannot point out even one such test. One could argue for check_exact
to be True
by default for assert_series_equal
, as it is for assert_index_equal
.
Comment From: jaapaap79
Or perhaps a separate flag check_index_exact that defaults to True and gets passed through to assert_index_equal?
Comment From: topper-123
Thanks for the bug report, @jaapaap79. This is a duplicate of #40719. I suggest continuing the discussion there.