-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
Added FrozenList subtraction #15506
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
Added FrozenList subtraction #15506
Changes from 20 commits
ada7cda
f6381a8
cd73faa
2fad2f7
fdcfbbb
2e43849
cb95089
2ab85cb
6a2b48d
fee7a7d
73564ab
0fc7e19
79dd958
0ea8d21
cd7de26
ccd75c7
3d6cee5
66b3b91
6f6c140
8dbde1e
84ba405
428a1b3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,6 +26,8 @@ Check the :ref:`API Changes <whatsnew_0200.api_breaking>` and :ref:`deprecations | |
New features | ||
~~~~~~~~~~~~ | ||
|
||
- Added ``.difference()`` method for FrozenLists (:issue:`15475`) | ||
|
||
- Integration with the ``feather-format``, including a new top-level ``pd.read_feather()`` and ``DataFrame.to_feather()`` method, see :ref:`here <io.feather>`. | ||
- ``.str.replace`` now accepts a callable, as replacement, which is passed to ``re.sub`` (:issue:`15055`) | ||
|
||
|
@@ -534,6 +536,7 @@ Deprecations | |
- ``Series.sortlevel`` and ``DataFrame.sortlevel`` have been deprecated in favor of ``Series.sort_index`` and ``DataFrame.sort_index`` (:issue:`15099`) | ||
- 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`) | ||
- ``Series/DataFrame/Panel.consolidate()`` been deprecated as a public method. (:issue:`15483`) | ||
- ``FrozenList.__add__`` has been depricated in favor of ``FrozenList.union`` (:issue: `15475`) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. deprecated. Add for |
||
|
||
.. _whatsnew_0200.prior_deprecations: | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,8 @@ | |
from pandas.types.cast import _coerce_indexer_dtype | ||
from pandas.formats.printing import pprint_thing | ||
|
||
import warnings | ||
|
||
|
||
class FrozenList(PandasObject, list): | ||
|
||
|
@@ -25,11 +27,12 @@ class FrozenList(PandasObject, list): | |
# typechecks | ||
|
||
def __add__(self, other): | ||
if isinstance(other, tuple): | ||
other = list(other) | ||
return self.__class__(super(FrozenList, self).__add__(other)) | ||
warnings.warn("__add__ is deprecated, use union(...)", FutureWarning) | ||
return self.union(other) | ||
|
||
__iadd__ = __add__ | ||
def __iadd__(self, other): | ||
warnings.warn("__iadd__ is deprecated, use union(...)", FutureWarning) | ||
return self.union(other) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is technically wrong; its an in-place modify, and doesn't return anything. Can you add a test for validate this? maybe
might work |
||
|
||
# Python 2 compat | ||
def __getslice__(self, i, j): | ||
|
@@ -80,6 +83,19 @@ def __repr__(self): | |
__setitem__ = __setslice__ = __delitem__ = __delslice__ = _disabled | ||
pop = append = extend = remove = sort = insert = _disabled | ||
|
||
def union(self, other): | ||
"""Returns a FrozenList with other concatenated to the end of self""" | ||
if isinstance(other, tuple): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you add a doc-string |
||
other = list(other) | ||
return self.__class__(super(FrozenList, self).__add__(other)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think |
||
|
||
def difference(self, other): | ||
"""Returns a FrozenList with the same elements as self, but with elements | ||
that are also in other removed.""" | ||
other = set(other) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same here |
||
temp = [x for x in self if x not in other] | ||
return self.__class__(temp) | ||
|
||
|
||
class FrozenNDArray(PandasObject, np.ndarray): | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,20 +15,22 @@ def setUp(self): | |
self.klass = FrozenList | ||
|
||
def test_add(self): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you add a test for |
||
result = self.container + (1, 2, 3) | ||
self.assert_produces_warning(FutureWarning) | ||
|
||
def test_union(self): | ||
result = self.container.union((1, 2, 3)) | ||
expected = FrozenList(self.lst + [1, 2, 3]) | ||
self.check_result(result, expected) | ||
|
||
result = (1, 2, 3) + self.container | ||
expected = FrozenList([1, 2, 3] + self.lst) | ||
def test_difference(self): | ||
result = self.container.difference([2]) | ||
expected = FrozenList([1, 3, 4, 5]) | ||
self.check_result(result, expected) | ||
|
||
def test_inplace(self): | ||
q = r = self.container | ||
q += [5] | ||
self.check_result(q, self.lst + [5]) | ||
# other shouldn't be mutated | ||
self.check_result(r, self.lst) | ||
def test_difference_dupe(self): | ||
result = FrozenList([1, 2, 3, 2]).difference([2]) | ||
expected = FrozenList([1, 3]) | ||
self.check_result(result, expected) | ||
|
||
|
||
class TestFrozenNDArray(CheckImmutable, CheckStringMixin, tm.TestCase): | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
add a versionadded tag here