8000 API: rename MultiIndex.labels to MultiIndex.codes by topper-123 · Pull Request #23752 · pandas-dev/pandas · GitHub
[go: up one dir, main page]

Skip to content

API: rename MultiIndex.labels to MultiIndex.codes #23752

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Dec 5, 2018
Prev Previous commit
Next Next commit
Update labels -> codes in various locations
  • Loading branch information
topper-123 committed Dec 5, 2018
commit 34e7ec55d82b8d78a9e7803ddc8f355eb3fa7244
4 changes: 2 additions & 2 deletions asv_bench/benchmarks/multiindex_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ def setup(self):
levels = [np.arange(n),
tm.makeStringIndex(n).values,
1000 + np.arange(n)]
labels = [np.random.choice(n, (k * n)) for lev in levels]
self.mi = MultiIndex(levels=levels, labels=labels)
codes = [np.random.choice(n, (k * n)) for lev in levels]
self.mi = MultiIndex(levels=levels, codes=codes)

def time_duplicated(self):
self.mi.duplicated()
Expand Down
16 changes: 8 additions & 8 deletions asv_bench/benchmarks/stat_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ class FrameMultiIndexOps(object):

def setup(self, level, op):
levels = [np.arange(10), np.arange(100), np.arange(100)]
labels = [np.arange(10).repeat(10000),
np.tile(np.arange(100).repeat(100), 10),
np.tile(np.tile(np.arange(100), 100), 10)]
index = pd.MultiIndex(levels=levels, labels=labels)
codes = [np.arange(10).repeat(10000),
np.tile(np.arange(100).repeat(100), 10),
np.tile(np.tile(np.arange(100), 100), 10)]
index = pd.MultiIndex(levels=levels, codes=codes)
df = pd.DataFrame(np.random.randn(len(index), 4), index=index)
self.df_func = getattr(df, op)

Expand Down Expand Up @@ -67,10 +67,10 @@ class SeriesMultiIndexOps(object):

def setup(self, level, op):
levels = [np.arange(10), np.arange(100), np.arange(100)]
labels = [np.arange(10).repeat(10000),
np.tile(np.arange(100).repeat(100), 10),
np.tile(np.tile(np.arange(100), 100), 10)]
index = pd.MultiIndex(levels=levels, labels=labels)
codes = [np.arange(10).repeat(10000),
np.tile(np.arange(100).repeat(100), 10),
np.tile(np.tile(np.arange(100), 100), 10)]
index = pd.MultiIndex(levels=levels, codes=codes)
s = pd.Series(np.random.randn(len(index)), index=index)
self.s_func = getattr(s, op)

Expand Down
4 changes: 2 additions & 2 deletions doc/source/whatsnew/v0.24.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1102,9 +1102,9 @@ Deprecations

- :attr:`MultiIndex.labels` has been deprecated and replaced by :attr:`MultiIndex.codes`.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i don't this is valid any longer, you can remove the attr

The functionality is unchanged. This new name better reflects the natures of
these codes and makes the API more similar to the API for
these codes and makes the ``MultiIndex`` API more similar to the API for
:class:`CategoricalIndex`(:issue:`13443`).
Copy link
Contributor
@jreback jreback Dec 5, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this might be a doc-rendering problem, but can resolve

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure I understand.

As a consequence, other uses of the name ``labels`` have also been deprecated in ``MultiIndex`` and replaced with ``codes``:
As a consequence, other uses of the name ``labels`` in ``MultiIndex`` have also been deprecated and replaced with ``codes``:
- You should initialize a ``MultiIndex`` instance using a parameter named ``codes`` rather than ``labels``.
- :meth:`MultiIndex.set_labels` has been deprecated in favor of :meth:`MultiIndex.set_codes`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

period at the end.

- for method :meth:`MultiIndex.copy`, the ``labels`` parameter has been deprecated and replaced by a ``codes`` parameter.
Expand Down
12 changes: 5 additions & 7 deletions pandas/core/indexes/multi.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from pandas.compat import lrange, lzip, map, range, zip
from pandas.compat.numpy import function as nv
from pandas.errors import PerformanceWarning, UnsortedIndexError
from pandas.util._decorators import Appender, cache_readonly
from pandas.util._decorators import Appender, cache_readonly, deprecate_kwarg

from pandas.core.dtypes.common import (
ensure_int64, ensure_platform_int, is_categorical_dtype, is_hashable,
Expand All @@ -31,8 +31,6 @@

from pandas.io.formats.printing import pprint_thing

from pandas.util._decorators import deprecate_kwarg

_index_doc_kwargs = dict(ibase._index_doc_kwargs)
_index_doc_kwargs.update(
dict(klass='MultiIndex',
Expand Down Expand Up @@ -655,19 +653,19 @@ def set_codes(self, codes, level=None, inplace=False,
>>> idx = pd.MultiIndex.from_tuples([(1, u'one'), (1, u'two'),
(2, u'one'), (2, u'two')],
names=['foo', 'bar'])
>>> idx.set_labels([[1,0,1,0], [0,0,1,1]])
>>> idx.set_codes([[1,0,1,0], [0,0,1,1]])
MultiIndex(levels=[[1, 2], [u'one', u'two']],
labels=[[1, 0, 1, 0], [0, 0, 1, 1]],
names=[u'foo', u'bar'])
>>> idx.set_labels([1,0,1,0], level=0)
>>> idx.set_codes([1,0,1,0], level=0)
MultiIndex(levels=[[1, 2], [u'one', u'two']],
labels=[[1, 0, 1, 0], [0, 1, 0, 1]],
names=[u'foo', u'bar'])
>>> idx.set_labels([0,0,1,1], level='bar')
>>> idx.set_codes([0,0,1,1], level='bar')
MultiIndex(levels=[[1, 2], [u'one', u'two']],
labels=[[0, 0, 1, 1], [0, 0, 1, 1]],
names=[u'foo', u'bar'])
>>> idx.set_labels([[1,0,1,0], [0,0,1,1]], level=[0,1])
>>> idx.set_codes([[1,0,1,0], [0,0,1,1]], level=[0,1])
MultiIndex(levels=[[1, 2], [u'one', u'two']],
labels=[[1, 0, 1, 0], [0, 0, 1, 1]],
names=[u'foo', u'bar'])
Expand Down
30 changes: 15 additions & 15 deletions pandas/tests/indexing/test_multiindex.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ def multiindex_dataframe_random_data():
"""DataFrame with 2 level MultiIndex with random data"""
index = MultiIndex(levels=[['foo', 'bar', 'baz', 'qux'], ['one', 'two',
'three']],
labels=[[0, 0, 0, 1, 1, 2, 2, 3, 3, 3],
[0, 1, 2, 0, 1, 1, 2, 0, 1, 2]],
codes=[[0, 0, 0, 1, 1, 2, 2, 3, 3, 3],
[0, 1, 2, 0, 1, 1, 2, 0, 1, 2]],
names=['first', 'second'])
return DataFrame(np.random.randn(10, 3), index=index,
columns=Index(['A', 'B', 'C'], name='exp'))
Expand All @@ -35,7 +35,7 @@ def multiindex_dataframe_random_data():
def single_level_multiindex():
"""single level MultiIndex"""
return MultiIndex(levels=[['foo', 'bar', 'baz', 'qux']],
labels=[[0, 1, 2, 3]], names=['first'])
codes=[[0, 1, 2, 3]], names=['first'])


@pytest.fixture
Expand Down Expand Up @@ -900,8 +900,8 @@ def test_frame_getitem_setitem_slice(

def test_frame_getitem_setitem_multislice(self):
levels = [['t1', 't2'], ['a', 'b', 'c']]
labels = [[0, 0, 0, 1, 1], [0, 1, 2, 0, 1]]
midx = MultiIndex(labels=labels, levels=levels, names=[None, 'id'])
codes = [[0, 0, 0, 1, 1], [0, 1, 2, 0, 1]]
midx = MultiIndex(codes=codes, levels=levels, names=[None, 'id'])
df = DataFrame({'value': [1, 2, 3, 7, 8]}, index=midx)

result = df.loc[:, 'value']
Expand Down Expand Up @@ -1044,9 +1044,9 @@ def test_xs_partial(self, multiindex_dataframe_random_data,

# ex from #1796
index = MultiIndex(levels=[['foo', 'bar'], ['one', 'two'], [-1, 1]],
labels=[[0, 0, 0, 0, 1, 1, 1, 1],
[0, 0, 1, 1, 0, 0, 1, 1], [0, 1, 0, 1, 0, 1,
0, 1]])
codes=[[0, 0, 0, 0, 1, 1, 1, 1],
[0, 0, 1, 1, 0, 0, 1, 1], [0, 1, 0, 1, 0, 1,
0, 1]])
df = DataFrame(np.random.randn(8, 4), index=index,
columns=list('abcd'))

Expand Down Expand Up @@ -1189,7 +1189,7 @@ def test_getitem_toplevel(self, multiindex_dataframe_random_data):

def test_getitem_setitem_slice_integers(self):
index = MultiIndex(levels=[[0, 1, 2], [0, 2]],
labels=[[0, 0, 1, 1, 2, 2], [0, 1, 0, 1, 0, 1]])
codes=[[0, 0, 1, 1, 2, 2], [0, 1, 0, 1, 0, 1]])

frame = DataFrame(np.random.randn(len(index), 4), index=index,
columns=['a', 'b', 'c', 'd'])
Expand All @@ -1211,8 +1211,8 @@ def test_getitem_setitem_slice_integers(self):

def test_getitem_int(self, multiindex_dataframe_random_data):
levels = [[0, 1], [0, 1, 2]]
labels = [[0, 0, 0, 1, 1, 1], [0, 1, 2, 0, 1, 2]]
index = MultiIndex(levels=levels, labels=labels)
codes = [[0, 0, 0, 1, 1, 1], [0, 1, 2, 0, 1, 2]]
index = MultiIndex(levels=levels, codes=codes)

frame = DataFrame(np.random.randn(6, 2), index=index)

Expand All @@ -1236,7 +1236,7 @@ def test_getitem_partial(
ymd = ymd.T
result = ymd[2000, 2]

expected = ymd.reindex(columns=ymd.columns[ymd.columns.labels[1] == 1])
expected = ymd.reindex(columns=ymd.columns[ymd.columns.codes[1] == 1])
expected.columns = expected.columns.droplevel(0).droplevel(0)
tm.assert_frame_equal(result, expected)

Expand Down Expand Up @@ -1279,12 +1279,12 @@ def test_fancy_slice_partial(

ymd = multiindex_year_month_day_dataframe_random_data
result = ymd.loc[(2000, 2):(2000, 4)]
lev = ymd.index.labels[1]
lev = ymd.index.codes[1]
expected = ymd[(lev >= 1) & (lev <= 3)]
tm.assert_frame_equal(result, expected)

def test_getitem_partial_column_select(self):
idx = MultiIndex(labels=[[0, 0, 0], [0, 1, 1], [1, 0, 1]],
idx = MultiIndex(codes=[[0, 0, 0], [0, 1, 1], [1, 0, 1]],
levels=[['a', 'b'], ['x', 'y'], ['p', 'q']])
df = DataFrame(np.random.rand(3, 2), index=idx)

Expand Down Expand Up @@ -1582,7 +1582,7 @@ def test_frame_getitem_not_sorted2(self):
df2_original = df2.copy()

df2.index.set_levels(['b', 'd', 'a'], level='col1', inplace=True)
df2.index.set_labels([0, 1, 0, 2], level='col1', inplace=True)
df2.index.set_codes([0, 1, 0, 2], level='col1', inplace=True)
assert not df2.index.is_lexsorted()
assert not df2.index.is_monotonic

Expand Down
8 changes: 4 additions & 4 deletions pandas/tests/io/parser/test_header.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ def test_header_multi_index_common_format_malformed1(all_parsers):
columns=MultiIndex(levels=[[u("a"), u("b"), u("c")],
[u("r"), u("s"), u("t"),
u("u"), u("v")]],
labels=[[0, 0, 1, 2, 2], [0, 1, 2, 3, 4]],
codes=[[0, 0, 1, 2, 2], [0, 1, 2, 3, 4]],
names=[u("a"), u("q")]))
data = """a,a,a,b,c,c
q,r,s,t,u,v
Expand All @@ -255,7 +255,7 @@ def test_header_multi_index_common_format_malformed2(all_parsers):
columns=MultiIndex(levels=[[u("a"), u("b"), u("c")],
[u("r"), u("s"), u("t"),
u("u"), u("v")]],
labels=[[0, 0, 1, 2, 2], [0, 1, 2, 3, 4]],
codes=[[0, 0, 1, 2, 2], [0, 1, 2, 3, 4]],
names=[None, u("q")]))

data = """,a,a,b,c,c
Expand All @@ -272,10 +272,10 @@ def test_header_multi_index_common_format_malformed3(all_parsers):
expected = DataFrame(np.array(
[[3, 4, 5, 6], [9, 10, 11, 12]], dtype="int64"),
index=MultiIndex(levels=[[1, 7], [2, 8]],
labels=[[0, 1], [0, 1]]),
codes=[[0, 1], [0, 1]]),
columns=MultiIndex(levels=[[u("a"), u("b"), u("c")],
[u("s"), u("t"), u("u"), u("v")]],
labels=[[0, 1, 2, 2], [0, 1, 2, 3]],
codes=[[0, 1, 2, 2], [0, 1, 2, 3]],
names=[None, u("q")]))
data = """,a,a,b,c,c
q,r,s,t,u,v
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/io/parser/test_index_col.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,5 +148,5 @@ def test_multi_index_naming_not_all_at_beginning(all_parsers):
expected = DataFrame({"Unnamed: 2": ["c", "d", "c", "d"]},
index=MultiIndex(
levels=[['a', 'b'], [1, 2, 3, 4]],
labels=[[0, 0, 1, 1], [0, 1, 2, 3]]))
codes=[[0, 0, 1, 1], [0, 1, 2, 3]]))
tm.assert_frame_equal(result, expected)
0