Skip to content

Commit 37fe2c4

Browse files
benthayerjreback
authored andcommitted
ENH: Added FrozenList difference setop
closes #15475 Author: Ben Thayer <benthayer2365@gmail.com> Author: bthayer2365 <bthayer2365@users.noreply.github.com> Closes #15506 from bthayer2365/frozen-index and squashes the following commits: 428a1b3 [Ben Thayer] Added __iadd__ test, fixed whatsnew 84ba405 [Ben Thayer] Merge branch 'master' of github.com:pandas-dev/pandas into frozen-index 8dbde1e [Ben Thayer] Rebased to upstream/master 6f6c140 [Ben Thayer] Added docstrings, depricated __iadd__, changed __add__ to use self.union() 66b3b91 [Ben Thayer] Fixed issue number 3d6cee5 [Ben Thayer] Depricated __add__ in favor of union ccd75c7 [Ben Thayer] Changed __sub__ to difference cd7de26 [Ben Thayer] Added versionadded tag in docs and renamed test_inplace to test_inplace_add for consistency 0ea8d21 [Ben Thayer] Added __isub__ and groupby example to docs 79dd958 [Ben Thayer] Updated whatsnew to reflect changes 0fc7e19 [Ben Thayer] Removed whitespace 73564ab [Ben Thayer] Added FrozenList subtraction fee7a7d [bthayer2365] Merge branch 'master' into frozen-index 6a2b48d [Ben Thayer] Added docstrings, depricated __iadd__, changed __add__ to use self.union() 2ab85cb [Ben Thayer] Fixed issue number cb95089 [Ben Thayer] Depricated __add__ in favor of union 2e43849 [Ben Thayer] Changed __sub__ to difference fdcfbbb [Ben Thayer] Added versionadded tag in docs and renamed test_inplace to test_inplace_add for consistency 2fad2f7 [Ben Thayer] Added __isub__ and groupby example to docs cd73faa [Ben Thayer] Updated whatsnew to reflect changes f6381a8 [Ben Thayer] Removed whitespace ada7cda [Ben Thayer] Added FrozenList subtraction
1 parent d92a759 commit 37fe2c4

File tree

4 files changed

+57
-12
lines changed

4 files changed

+57
-12
lines changed

Diff for: doc/source/groupby.rst

+10
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,16 @@ We could naturally group by either the ``A`` or ``B`` columns or both:
126126
grouped = df.groupby('A')
127127
grouped = df.groupby(['A', 'B'])
128128
129+
.. versionadded:: 0.20
130+
131+
If we also have a MultiIndex on columns ``A`` and ``B``, we can group by all
132+
but the specified columns.
133+
134+
.. ipython:: python
135+
136+
df2 = df.set_index(['A', 'B'])
137+
grouped = df2.groupby(level=df2.index.names.difference(['B'])
138+
129139
These will split the DataFrame on its index (rows). We could also split by the
130140
columns:
131141

Diff for: doc/source/whatsnew/v0.20.0.txt

+2
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ New features
2828

2929
- Integration with the ``feather-format``, including a new top-level ``pd.read_feather()`` and ``DataFrame.to_feather()`` method, see :ref:`here <io.feather>`.
3030
- ``.str.replace`` now accepts a callable, as replacement, which is passed to ``re.sub`` (:issue:`15055`)
31+
- ``FrozenList`` has gained the ``.difference()`` setop method (:issue:`15475`)
3132

3233

3334

@@ -534,6 +535,7 @@ Deprecations
534535
- ``Series.sortlevel`` and ``DataFrame.sortlevel`` have been deprecated in favor of ``Series.sort_index`` and ``DataFrame.sort_index`` (:issue:`15099`)
535536
- importing ``concat`` from ``pandas.tools.merge`` has been deprecated in favor of imports from the ``pandas`` namespace. This should only affect explict imports (:issue:`15358`)
536537
- ``Series/DataFrame/Panel.consolidate()`` been deprecated as a public method. (:issue:`15483`)
538+
- ``FrozenList`` addition (new object and inplace) have been deprecated in favor of the ``.union()`` method. (:issue: `15475`)
537539

538540
.. _whatsnew_0200.prior_deprecations:
539541

Diff for: pandas/indexes/frozen.py

+21-3
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
from pandas.types.cast import _coerce_indexer_dtype
1414
from pandas.formats.printing import pprint_thing
1515

16+
import warnings
17+
1618

1719
class FrozenList(PandasObject, list):
1820

@@ -25,11 +27,14 @@ class FrozenList(PandasObject, list):
2527
# typechecks
2628

2729
def __add__(self, other):
30+
warnings.warn("__add__ is deprecated, use union(...)", FutureWarning)
31+
return self.union(other)
32+
33+
def __iadd__(self, other):
34+
warnings.warn("__iadd__ is deprecated, use union(...)", FutureWarning)
2835
if isinstance(other, tuple):
2936
other = list(other)
30-
return self.__class__(super(FrozenList, self).__add__(other))
31-
32-
__iadd__ = __add__
37+
return super(FrozenList, self).__iadd__(other)
3338

3439
# Python 2 compat
3540
def __getslice__(self, i, j):
@@ -80,6 +85,19 @@ def __repr__(self):
8085
__setitem__ = __setslice__ = __delitem__ = __delslice__ = _disabled
8186
pop = append = extend = remove = sort = insert = _disabled
8287

88+
def union(self, other):
89+
"""Returns a FrozenList with other concatenated to the end of self"""
90+
if isinstance(other, tuple):
91+
other = list(other)
92+
return self.__class__(super(FrozenList, self).__add__(other))
93+
94+
def difference(self, other):
95+
"""Returns a FrozenList with the same elements as self, but with elements
96+
that are also in other removed."""
97+
other = set(other)
98+
temp = [x for x in self if x not in other]
99+
return self.__class__(temp)
100+
83101

84102
class FrozenNDArray(PandasObject, np.ndarray):
85103

Diff for: pandas/tests/indexes/test_frozen.py

+24-9
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,35 @@ def setUp(self):
1515
self.klass = FrozenList
1616

1717
def test_add(self):
18-
result = self.container + (1, 2, 3)
18+
q = FrozenList([1])
19+
with tm.assert_produces_warning(FutureWarning,
20+
check_stacklevel=False):
21+
q = q + [2, 3]
22+
expected = FrozenList([1, 2, 3])
23+
self.check_result(q, expected)
24+
25+
def test_iadd(self):
26+
q = FrozenList([1])
27+
with tm.assert_produces_warning(FutureWarning,
28+
check_stacklevel=False):
29+
q += [2, 3]
30+
expected = FrozenList([1, 2, 3])
31+
self.check_result(q, expected)
32+
33+
def test_union(self):
34+
result = self.container.union((1, 2, 3))
1935
expected = FrozenList(self.lst + [1, 2, 3])
2036
self.check_result(result, expected)
2137

22-
result = (1, 2, 3) + self.container
23-
expected = FrozenList([1, 2, 3] + self.lst)
38+
def test_difference(self):
39+
result = self.container.difference([2])
40+
expected = FrozenList([1, 3, 4, 5])
2441
self.check_result(result, expected)
2542

26-
def test_inplace(self):
27-
q = r = self.container
28-
q += [5]
29-
self.check_result(q, self.lst + [5])
30-
# other shouldn't be mutated
31-
self.check_result(r, self.lst)
43+
def test_difference_dupe(self):
44+
result = FrozenList([1, 2, 3, 2]).difference([2])
45+
expected = FrozenList([1, 3])
46+
self.check_result(result, expected)
3247

3348

3449
class TestFrozenNDArray(CheckImmutable, CheckStringMixin, tm.TestCase):

0 commit comments

Comments
 (0)