Skip to content

BUG: unexpected assign by a single-element list (GH19474) #20732

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 2 commits into from
Apr 21, 2018
Merged

BUG: unexpected assign by a single-element list (GH19474) #20732

merged 2 commits into from
Apr 21, 2018

Conversation

kittoku
Copy link
Contributor

@kittoku kittoku commented Apr 18, 2018

I thought it is a straightforward way to evaluate all of df.loc[0, ['A']] = ['X'], df.loc[0, ['A', 'B']] = ['X', 'Y'], ...(more-columns case) the same way in else block at line 590 of pandas/core/indexing.py.

Please let me know bad or ungrammatical points.

@@ -752,3 +753,21 @@ def convert_nested_indexer(indexer_type, keys):
index=pd.MultiIndex.from_product(keys))

tm.assert_series_equal(result, expected)

@pytest.mark.parametrize(
'indexer,value', itertools.product(
Copy link
Member

Choose a reason for hiding this comment

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

Can you use two instances of parametrize instead of itertools.product?

@pytest.mark.parametrize('indexer', [...])
@pytest.mark.parametrize('value', [...])
def test_loc_setitem_with_scalar_index(self, indexer, value):

df.loc[0, indexer] = value
result = df.loc[0, 'A']

if not is_scalar(result) and result == 'Z':
Copy link
Member

Choose a reason for hiding this comment

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

I think it'd be more direct to just do assert result == 'Z'.

Usually in tests you just assert what you want the result to be, instead of catching what you don't want and writing an informative error message like you would in user facing code. If the test does fail, pytest will automatically give details similar to the message you wrote.

Copy link
Contributor Author

@kittoku kittoku Apr 19, 2018

Choose a reason for hiding this comment

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

Thank you for comments @jschendel

I rewrote codes, but kept is_scalar(result). Without it, if result is np.array(['Z']) , we cannot catch error because of broadcasting.

BTW, I should have written the conditional statement as not is_scalar(result) and not result == 'Z'. Sorry.

@codecov
Copy link

codecov bot commented Apr 19, 2018

Codecov Report

❗ No coverage uploaded for pull request base (master@23bc217). Click here to learn what that means.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff            @@
##             master   #20732   +/-   ##
=========================================
  Coverage          ?   91.84%           
=========================================
  Files             ?      153           
  Lines             ?    49295           
  Branches          ?        0           
=========================================
  Hits              ?    45276           
  Misses            ?     4019           
  Partials          ?        0
Flag Coverage Δ
#multiple 90.23% <100%> (?)
#single 41.89% <0%> (?)
Impacted Files Coverage Δ
pandas/core/indexing.py 93.08% <100%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 23bc217...bfa2f67. Read the comment docs.

Copy link
Contributor

@jreback jreback left a comment

Choose a reason for hiding this comment

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

lgtm. style changes. ping on green.

@@ -1111,6 +1111,7 @@ Indexing
- Bug in :meth:`DataFrame.first_valid_index` and :meth:`DataFrame.last_valid_index` in presence of entire rows of NaNs in the middle of values (:issue:`20499`).
- Bug in :class:`IntervalIndex` where some indexing operations were not supported for overlapping or non-monotonic ``uint64`` data (:issue:`20636`)
- Bug in ``Series.is_unique`` where extraneous output in stderr is shown if Series contains objects with ``__ne__`` defined (:issue:`20661`)
- Bug in ``.loc`` where assigned value to ``DataFrame`` is unexpectedly listed (:issue:`19474`)
Copy link
Contributor

Choose a reason for hiding this comment

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

Bug in .loc assignment with a single-element list-like incorrectly assigns as a list.

@@ -532,7 +532,8 @@ def setter(item, v):

def can_do_equal_len():
""" return True if we have an equal len settable """
if not len(labels) == 1 or not np.iterable(value):
if not len(labels) == 1 or not np.iterable(value) \
Copy link
Contributor

Choose a reason for hiding this comment

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

use parens rather than \ to break the line

@@ -752,3 +753,18 @@ def convert_nested_indexer(indexer_type, keys):
index=pd.MultiIndex.from_product(keys))

tm.assert_series_equal(result, expected)

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 move to the end of the tests that start with test_loc_setitem_

@@ -11,6 +11,7 @@
from pandas import Series, DataFrame, Timestamp, date_range, MultiIndex, Index
from pandas.util import testing as tm
from pandas.tests.indexing.common import Base
from pandas.core.dtypes.common import is_scalar
Copy link
Contributor

Choose a reason for hiding this comment

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

use from pandas.api.types.is_scalar (its the same, just user facing)

@jreback jreback added Bug Indexing Related to indexing on series/frames, not to indexes themselves labels Apr 19, 2018
@jreback jreback added this to the 0.23.0 milestone Apr 19, 2018

@pytest.mark.parametrize(
'indexer', [['A'], slice(None, 'A', None), np.array(['A'])])
@pytest.mark.parametrize(
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 also test this with .iloc

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated. Please review.

@jreback jreback merged commit 54470f3 into pandas-dev:master Apr 21, 2018
@jreback
Copy link
Contributor

jreback commented Apr 21, 2018

thanks @kittoku keep em coming!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Indexing Related to indexing on series/frames, not to indexes themselves
Projects
None yet
Development

Successfully merging this pull request may close these issues.

BUG: unexpected assign by a single-element list
3 participants