Skip to content

Fixed bug: Dataframe.sort_values not raising ValueError for ascending-incompatible value and Series.sort_values raising ValueError for int value #42684

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 10 commits into from
Jul 30, 2021

Conversation

gaikwadrahul20
Copy link
Contributor

@gaikwadrahul20
Copy link
Contributor Author

Apologies for any issues/mistakes. This is my first ever pull request to open source community.

@gaikwadrahul20 gaikwadrahul20 changed the title Fixed bug: Dataframe.sort_values and Series.sort_values not raising ValueError for ascending-incompatible value Fixed bug: Dataframe.sort_values not raising ValueError for ascending-incompatible value and Series.sort_values raising ValueError for int value Jul 23, 2021
Copy link
Member

@phofl phofl left a comment

Choose a reason for hiding this comment

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

small comments

@gaikwadrahul20
Copy link
Contributor Author

Thanks for the review @phofl.
All the comments are addressed.

Copy link
Contributor

@attack68 attack68 left a comment

Choose a reason for hiding this comment

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

looks good but I think tests might benefit from restructuting

ser = Series([23, 7, 21])
expected = np.sort(ser.values)[::-1]

msg = 'For argument "ascending" expected type bool, received type str.'
Copy link
Contributor

Choose a reason for hiding this comment

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

would it not be better to separate the raises ValueError test and the functional tests?
Also, would it not be better to parametrize the functional tests as follows:

@pytest.mark.parametrize("ascending", [False, 0, 1, True])
def test_sort_values_validate_ascending(self, ascending):
    ...
    sorted_ser = ser.sort_values(ascending=ascending)
    ...
    if ascending:
        expected = expected[::-1]
    ...

Copy link
Contributor Author

@gaikwadrahul20 gaikwadrahul20 Jul 25, 2021

Choose a reason for hiding this comment

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

Thanks @attack68. I agree, code will look more clean and short. Pushed a change for this, please check.

Also, I was thinking, does it make more sense to parameterize test_sort_values_validate_ascending_for_value_error test function with msg as a parameter and move code from line 54 to 68 (code attached below) in this test?

        msg = 'For argument "ascending" expected type bool, received type NoneType.'
        with pytest.raises(ValueError, match=msg):
            ts.sort_values(ascending=None)
        msg = r"Length of ascending \(0\) must be 1 for Series"
        with pytest.raises(ValueError, match=msg):
            ts.sort_values(ascending=[])
        msg = r"Length of ascending \(3\) must be 1 for Series"
        with pytest.raises(ValueError, match=msg):
            ts.sort_values(ascending=[1, 2, 3])
        msg = r"Length of ascending \(2\) must be 1 for Series"
        with pytest.raises(ValueError, match=msg):
            ts.sort_values(ascending=[False, False])
        msg = 'For argument "ascending" expected type bool, received type str.'
        with pytest.raises(ValueError, match=msg):
            ts.sort_values(ascending="foobar")

@jreback jreback added the Error Reporting Incorrect or improved errors from pandas label Jul 25, 2021
@jreback jreback added this to the 1.4 milestone Jul 25, 2021
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. @attack68 merge when good

@gaikwadrahul20
Copy link
Contributor Author

Hi @attack68, what are your thoughts on previous comment of mine? if it looks good, is it good to merge?

@@ -227,6 +227,8 @@ Indexing
- Bug in :meth:`Series.loc` when with a :class:`MultiIndex` whose first level contains only ``np.nan`` values (:issue:`42055`)
- Bug in indexing on a :class:`Series` or :class:`DataFrame` with a :class:`DatetimeIndex` when passing a string, the return type depended on whether the index was monotonic (:issue:`24892`)
- Bug in indexing on a :class:`MultiIndex` failing to drop scalar levels when the indexer is a tuple containing a datetime-like string (:issue:`42476`)
- Bug in :meth:`DataFrame.sort_values` and :meth:`Series.sort_values` when passing an ascending value, failed to raise or incorrectly raising ``ValueError`` (:issue:`41634`)
- Bug in updating values of :class:`pandas.Series` using boolean index, created by using :meth:`pandas.DataFrame.pop` (:issue:`42530`)
Copy link
Contributor

Choose a reason for hiding this comment

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

this is an extraneous note (the 2nd) that likley came in during rebase

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@jreback I think this is also taken care by you.

@jreback jreback merged commit 722e789 into pandas-dev:master Jul 30, 2021
@jreback
Copy link
Contributor

jreback commented Jul 30, 2021

thanks @gaikwadrahul20

@gaikwadrahul20
Copy link
Contributor Author

Thanks a lot @jreback.

feefladder pushed a commit to feefladder/pandas that referenced this pull request Sep 7, 2021
…-incompatible value and Series.sort_values raising ValueError for int value (pandas-dev#42684)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Error Reporting Incorrect or improved errors from pandas
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ENH: Raise error on ascending='False'
4 participants