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 PR
  • Loading branch information
topper-123 committed Dec 5, 2018
commit 0e74c15c027a639c81d536b3065ef740812e4e38
4 changes: 2 additions & 2 deletions asv_bench/benchmarks/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -473,8 +473,8 @@ def setup(self):
n1 = 400
n2 = 250
index = MultiIndex(levels=[np.arange(n1), tm.makeStringIndex(n2)],
labels=[np.repeat(range(n1), n2).tolist(),
list(range(n2)) * n1],
codes=[np.repeat(range(n1), n2).tolist(),
list(range(n2)) * n1],
names=['lev1', 'lev2'])
arr = np.random.randn(n1 * n2, 3)
arr[::10000, 0] = np.nan
Expand Down
10 changes: 5 additions & 5 deletions asv_bench/benchmarks/join_merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,16 +115,16 @@ class Join(object):
def setup(self, sort):
level1 = tm.makeStringIndex(10).values
level2 = tm.makeStringIndex(1000).values
label1 = np.arange(10).repeat(1000)
label2 = np.tile(np.arange(1000), 10)
codes1 = np.arange(10).repeat(1000)
codes2 = np.tile(np.arange(1000), 10)
index2 = MultiIndex(levels=[level1, level2],
labels=[label1, label2])
codes=[codes1, codes2])
self.df_multi = DataFrame(np.random.randn(len(index2), 4),
index=index2,
columns=['A', 'B', 'C', 'D'])

self.key1 = np.tile(level1.take(label1), 10)
self.key2 = np.tile(level2.take(label2), 10)
self.key1 = np.tile(level1.take(codes1), 10)
self.key2 = np.tile(level2.take(codes2), 10)
self.df = DataFrame({'data1': np.random.randn(100000),
'data2': np.random.randn(100000),
'key1': self.key1,
Expand Down
6 changes: 3 additions & 3 deletions asv_bench/benchmarks/reindex.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@ class LevelAlign(object):
def setup(self):
self.index = MultiIndex(
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)])
codes=[np.arange(10).repeat(10000),
np.tile(np.arange(100).repeat(100), 10),
np.tile(np.tile(np.arange(100), 100), 10)])
self.df = DataFrame(np.random.randn(len(self.index), 4),
index=self.index)
self.df_level = DataFrame(np.random.randn(100, 4),
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 @@ -1104,8 +1104,8 @@ Deprecations
The functionality is unchanged. This new name better reflects the natures of
these codes and makes the 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 concequence, other uses of the name ``labels`` have also been deprecated in ``MultiIndex`` and replaced with ``codes``:
- You should initialize a MultiIndex instance using a parameter named ``codes`` rather than ``labels``.
As a consequence, other uses of the name ``labels`` have also been deprecated in ``MultiIndex`` 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.
- :meth:`DataFrame.to_stata`, :meth:`read_stata`, :class:`StataReader` and :class:`StataWriter` have deprecated the ``encoding`` argument. The encoding of a Stata dta file is determined by the file type and cannot be changed (:issue:`21244`)
Expand Down
4 changes: 2 additions & 2 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -3736,8 +3736,8 @@ def drop(self, labels=None, axis=0, index=None, columns=None,

>>> midx = pd.MultiIndex(levels=[['lama', 'cow', 'falcon'],
... ['speed', 'weight', 'length']],
... labels=[[0, 0, 0, 1, 1, 1, 2, 2, 2],
... [0, 1, 2, 0, 1, 2, 0, 1, 2]])
... codes=[[0, 0, 0, 1, 1, 1, 2, 2, 2],
... [0, 1, 2, 0, 1, 2, 0, 1, 2]])
>>> df = pd.DataFrame(index=midx, columns=['big', 'small'],
... data=[[45, 30], [200, 100], [1.5, 1], [30, 20],
... [250, 150], [1.5, 0.8], [320, 250],
Expand Down
8 changes: 4 additions & 4 deletions pandas/core/indexes/multi.py
Original file line number Diff line number Diff line change
Expand Up @@ -587,7 +587,7 @@ def codes(self):

@property
def labels(self):
warnings.warn(("labels was deprecated in version 0.24.0. "
warnings.warn((".labels was deprecated in version 0.24.0. "
"Use .codes instead."),
FutureWarning, stacklevel=2)
return self.codes
Expand Down Expand Up @@ -622,7 +622,7 @@ def _set_codes(self, codes, level=None, copy=False, validate=True,

def set_labels(self, labels, level=None, inplace=False,
verify_integrity=True):
warnings.warn(("set_labels was deprecated in version 0.24.0. "
warnings.warn((".set_labels was deprecated in version 0.24.0. "
"Use .set_codes instead."),
FutureWarning, stacklevel=2)
return self.set_codes(codes=labels, level=level, inplace=inplace,
Expand Down Expand Up @@ -1512,7 +1512,7 @@ def _sort_levels_monotonic(self):
--------

>>> i = pd.MultiIndex(levels=[['a', 'b'], ['bb', 'aa']],
labels=[[0, 0, 1, 1], [0, 1, 0, 1]])
codes=[[0, 0, 1, 1], [0, 1, 0, 1]])
>>> i
MultiIndex(levels=[['a', 'b'], ['bb', 'aa']],
labels=[[0, 0, 1, 1], [0, 1, 0, 1]])
Expand Down Expand Up @@ -1885,7 +1885,7 @@ def swaplevel(self, i=-2, j=-1):
Examples
--------
>>> mi = pd.MultiIndex(levels=[['a', 'b'], ['bb', 'aa']],
... labels=[[0, 0, 1, 1], [0, 1, 0, 1]])
... codes=[[0, 0, 1, 1], [0, 1, 0, 1]])
>>> mi
MultiIndex(levels=[['a', 'b'], ['bb', 'aa']],
labels=[[0, 0, 1, 1], [0, 1, 0, 1]])
Expand Down
4 changes: 2 additions & 2 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -3663,8 +3663,8 @@ def drop(self, labels=None, axis=0, index=None, columns=None,

>>> midx = pd.MultiIndex(levels=[['lama', 'cow', 'falcon'],
... ['speed', 'weight', 'length']],
... labels=[[0, 0, 0, 1, 1, 1, 2, 2, 2],
... [0, 1, 2, 0, 1, 2, 0, 1, 2]])
... codes=[[0, 0, 0, 1, 1, 1, 2, 2, 2],
... [0, 1, 2, 0, 1, 2, 0, 1, 2]])
>>> s = pd.Series([45, 200, 1.2, 30, 250, 1.5, 320, 1, 0.3],
... index=midx)
>>> s
Expand Down
1 change: 1 addition & 0 deletions pandas/tests/indexes/multi/test_copy.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ def test_labels_deprecated(idx):
with tm.assert_produces_warning(FutureWarning):
idx.copy(labels=codes)


def test_view(idx):
i_view = idx.view()
assert_multiindex_copied(i_view, idx)
Expand Down
4 changes: 2 additions & 2 deletions pandas/tests/indexes/multi/test_get_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ def test_set_levels_codes_names_bad_input(idx):
with pytest.raises(ValueError, match='Length of levels'):
idx.set_levels([levels[0]])

with tm.assert_raises_regex(ValueError, 'Length of codes'):
with pytest.raises(ValueError, match='Length of codes'):
idx.set_codes([codes[0]])

with pytest.raises(ValueError, match='Length of names'):
Expand All @@ -359,7 +359,7 @@ def test_set_levels_codes_names_bad_input(idx):
idx.set_levels(levels[0])

# shouldn't scalar data error, instead should demand list-like
with tm.assert_raises_regex(TypeError, 'list of lists-like'):
with pytest.raises(TypeError, match='list of lists-like'):
idx.set_codes(codes[0])

# shouldn't scalar data error, instead should demand list-like
Expand Down
16 changes: 10 additions & 6 deletions pandas/tests/io/test_feather.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,15 +100,19 @@ def test_rw_nthreads(self):
"the 'nthreads' keyword is deprecated, "
"use 'use_threads' instead"
)
with tm.assert_produces_warning(FutureWarning) as w:
# TODO: make the warning work with check_stacklevel=True
with tm.assert_produces_warning(
FutureWarning, check_stacklevel=False) as w:
self.check_round_trip(df, nthreads=2)
assert len(w) == 1
assert expected_warning in str(w[0])
# we have an extra FutureWarning because of #GH23752
assert any(expected_warning in str(x) for x in w)

with tm.assert_produces_warning(FutureWarning) as w:
# TODO: make the warning work with check_stacklevel=True
with tm.assert_produces_warning(
FutureWarning, check_stacklevel=False) as w:
self.check_round_trip(df, nthreads=1)
assert len(w) == 1
assert expected_warning in str(w[0])
# we have an extra FutureWarnings because of #GH23752
assert any(expected_warning in str(x) for x in w)

def test_rw_use_threads(self):
df = pd.DataFrame({'A': np.arange(100000)})
Expand Down