Skip to content

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

Closed
wants to merge 22 commits into from
Closed
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
ada7cda
Added FrozenList subtraction
benthayer Feb 25, 2017
f6381a8
Removed whitespace
benthayer Feb 25, 2017
cd73faa
Updated whatsnew to reflect changes
benthayer Feb 25, 2017
2fad2f7
Added __isub__ and groupby example to docs
benthayer Feb 25, 2017
fdcfbbb
Added versionadded tag in docs and renamed test_inplace to test_inpla…
benthayer Feb 25, 2017
2e43849
Changed __sub__ to difference
benthayer Feb 26, 2017
cb95089
Depricated __add__ in favor of union
benthayer Feb 26, 2017
2ab85cb
Fixed issue number
benthayer Feb 26, 2017
6a2b48d
Added docstrings, depricated __iadd__, changed __add__ to use self.un…
benthayer Mar 1, 2017
fee7a7d
Merge branch 'master' into frozen-index
Mar 1, 2017
73564ab
Added FrozenList subtraction
benthayer Feb 25, 2017
0fc7e19
Removed whitespace
benthayer Feb 25, 2017
79dd958
Updated whatsnew to reflect changes
benthayer Feb 25, 2017
0ea8d21
Added __isub__ and groupby example to docs
benthayer Feb 25, 2017
cd7de26
Added versionadded tag in docs and renamed test_inplace to test_inpla…
benthayer Feb 25, 2017
ccd75c7
Changed __sub__ to difference
benthayer Feb 26, 2017
3d6cee5
Depricated __add__ in favor of union
benthayer Feb 26, 2017
66b3b91
Fixed issue number
benthayer Feb 26, 2017
6f6c140
Added docstrings, depricated __iadd__, changed __add__ to use self.un…
benthayer Mar 1, 2017
8dbde1e
Rebased to upstream/master
benthayer Mar 1, 2017
84ba405
Merge branch 'master' of github.com:pandas-dev/pandas into frozen-index
benthayer Mar 1, 2017
428a1b3
Added __iadd__ test, fixed whatsnew
benthayer Mar 1, 2017
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions doc/source/groupby.rst
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,16 @@ We could naturally group by either the ``A`` or ``B`` columns or both:
grouped = df.groupby('A')
grouped = df.groupby(['A', 'B'])

.. versionadded:: 0.20

If we also have a MultiIndex on columns ``A`` and ``B``, we can group by all
but the specified columns
Copy link
Contributor

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


.. ipython:: python

df.set_index(['A', 'B'])
Copy link
Contributor

Choose a reason for hiding this comment

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

this won't work as it returns a new object, instead something like

df2 = df.set_index(['A', 'B'])
grouped = df2.groupby(level=....)

grouped = df.groupby(level=df.index.names.difference(['B'])

These will split the DataFrame on its index (rows). We could also split by the
columns:

Expand Down
4 changes: 3 additions & 1 deletion doc/source/whatsnew/v0.20.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ Check the :ref:`API Changes <whatsnew_0200.api_breaking>` and :ref:`deprecations
New features
~~~~~~~~~~~~

- Added difference from FrozenLists (:issue:`15475`)
Copy link
Contributor

Choose a reason for hiding this comment

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

.difference() method for FrozenList


- 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`)

Expand Down Expand Up @@ -484,7 +486,7 @@ Deprecations
- ``DataFrame.astype()`` has deprecated the ``raise_on_error`` parameter in favor of ``errors`` (:issue:`14878`)
- ``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`)

- ``FrozenList.__add__`` has been depricated in favor of ``FrozenList.union`` (:issue: `15475`)
Copy link
Contributor

Choose a reason for hiding this comment

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

deprecated. Add for __iadd__ here as well

.. _whatsnew_0200.prior_deprecations:

Removal of prior version deprecations/changes
Expand Down
13 changes: 13 additions & 0 deletions pandas/indexes/frozen.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):

Expand All @@ -25,6 +27,7 @@ class FrozenList(PandasObject, list):
# typechecks

def __add__(self, other):
warnings.warn("__add__ is deprecated, use union(...)", FutureWarning)
if isinstance(other, tuple):
Copy link
Contributor

Choose a reason for hiding this comment

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

return self.union(other)

other = list(other)
return self.__class__(super(FrozenList, self).__add__(other))
Expand Down Expand Up @@ -80,6 +83,16 @@ def __repr__(self):
__setitem__ = __setslice__ = __delitem__ = __delslice__ = _disabled
pop = append = extend = remove = sort = insert = _disabled

def union(self, other):
if isinstance(other, tuple):
Copy link
Contributor

Choose a reason for hiding this comment

The 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))
Copy link
Contributor

@jreback jreback Feb 27, 2017

Choose a reason for hiding this comment

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

I think __iadd__ is also defined? if so, deprecate that as well (no replacement, just a deprecation)


def difference(self, other):
other = set(other)
Copy link
Contributor

Choose a reason for hiding this comment

The 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):

Expand Down
19 changes: 9 additions & 10 deletions pandas/tests/indexes/test_frozen.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,20 @@ def setUp(self):
self.container = FrozenList(self.lst)
self.klass = FrozenList

def test_add(self):
result = self.container + (1, 2, 3)
def test_union(self):
Copy link
Contributor

Choose a reason for hiding this comment

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

can you add a test for assert_produces_warning(FutureWarning) for the deprecation of __add__

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):
Expand Down