Skip to content

Commit

Permalink
Compat: pandas MultiIndex .labels to .codes (#103)
Browse files Browse the repository at this point in the history
Closes #99
  • Loading branch information
J535D165 authored Jul 12, 2019
1 parent a604560 commit 423a2eb
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
6 changes: 5 additions & 1 deletion recordlinkage/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,11 @@ def _dedup_index(self, df_a):
# Remove all pairs not in the lower triangular part of the matrix.
# This part can be inproved by not comparing the level values, but the
# level itself.
pairs = pairs[pairs.labels[0] > pairs.labels[1]]
try:
pairs = pairs[pairs.codes[0] > pairs.codes[1]]
except AttributeError:
# backwards compat pandas <24
pairs = pairs[pairs.labels[0] > pairs.labels[1]]

return pairs

Expand Down
5 changes: 5 additions & 0 deletions recordlinkage/utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import warnings
from functools import wraps
from packaging import version

import numpy

Expand Down Expand Up @@ -77,6 +78,10 @@ def func_wrapper(*args, **kwargs):

# Checks and conversions

def is_min_pandas_version(min_version):
"""Check if pandas version is larger or equal the version passed."""
return version.parse(pandas.__version__) >= version.parse(min_version)


def is_label_dataframe(label, df):
"""check column label existance"""
Expand Down
6 changes: 5 additions & 1 deletion tests/test_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import recordlinkage as rl
from recordlinkage import index_split
from recordlinkage.utils import is_min_pandas_version


def test_multiindex_split():
Expand All @@ -24,7 +25,10 @@ def test_multiindex_split():
ptm.assert_index_equal(result_index_chunk, expected_index_chunk)

assert len(result_index_chunk.levels) == 2
assert len(result_index_chunk.labels) == 2
if is_min_pandas_version("0.24.0"):
assert len(result_index_chunk.codes) == 2
else:
assert len(result_index_chunk.labels) == 2


def test_options():
Expand Down

0 comments on commit 423a2eb

Please sign in to comment.