Skip to content

Fix series comparison operators when dealing with zero rank numpy arrays #13307

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 1 commit into from
Closed

Conversation

gliptak
Copy link
Contributor

@gliptak gliptak commented May 27, 2016

@gliptak
Copy link
Contributor Author

gliptak commented May 27, 2016

@@ -754,7 +754,8 @@ def wrapper(self, other, axis=None):
elif isinstance(other, pd.DataFrame): # pragma: no cover
return NotImplemented
elif isinstance(other, (np.ndarray, pd.Index)):
if len(self) != len(other):
lo = len(np.atleast_1d(other))
if lo != 1 and len(self) != lo:
Copy link
Contributor

Choose a reason for hiding this comment

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

why are you adding this check for != 1? this seems to be hiding thing. Further why don't you do:

other = np.atleast_1d(other) and be done?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

len(other) == 1 shouldn't throw the exception, as other is broadcasted by _constructor ...

Copy link
Contributor

Choose a reason for hiding this comment

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

much better to fix it than put this kind of conditional in

Copy link
Contributor Author

Choose a reason for hiding this comment

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

how do you see this changed differently?

Copy link
Contributor

Choose a reason for hiding this comment

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

exactly as I just said

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I tried, but that fails the (new) testcase

Would this work?

            try:
                return self._constructor(na_op(self.values, np.asarray(other)),
                                         index=self.index).__finalize__(self)
            except:
                raise ValueError('Lengths must match to compare')

Copy link
Contributor

Choose a reason for hiding this comment

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

no that's not a good thing to do, we don't like non-specific try:excepts.

you can specifically deal with it by something like:

if lib.isscalar(lib.item_from_zerodim(other)):

Copy link
Contributor Author

Choose a reason for hiding this comment

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

modified with this above

@jreback jreback added Bug Dtype Conversions Unexpected or buggy dtype conversions labels May 27, 2016
if len(self) != len(other):
raise ValueError('Lengths must match to compare')
if not lib.isscalar(lib.item_from_zerodim(other)):
if len(self) != len(other):
Copy link
Contributor

Choose a reason for hiding this comment

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

still to much nested if statements
maybe just try to convert a 0-dim ndarray to a scaler instead

Copy link
Contributor Author

Choose a reason for hiding this comment

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

there have to be two if statements, to determine scalar or not, and validate length being the same

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 Would you like this to be changed?

Copy link
Contributor

Choose a reason for hiding this comment

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

you can do this in an and. put a comment that need to care for zero-dim ndarrays

@codecov-io
Copy link

codecov-io commented May 28, 2016

Current coverage is 84.22%

Merging #13307 into master will decrease coverage by <.01%

@@             master     #13307   diff @@
==========================================
  Files           138        138          
  Lines         50721      50667    -54   
  Methods           0          0          
  Messages          0          0          
  Branches          0          0          
==========================================
- Hits          42723      42673    -50   
+ Misses         7998       7994     -4   
  Partials          0          0          

Powered by Codecov. Last updated by 0c6226c...b49a116

@@ -315,6 +315,6 @@ Bug Fixes
- Bug in ``groupby`` where ``apply`` returns different result depending on whether first result is ``None`` or not (:issue:`12824`)



- Bug in series comparison operators when dealing with zero rank numpy arrays (:issue:`13006`)

Copy link
Member

Choose a reason for hiding this comment

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

Use Series (surrounded by double-ticks) instead of "series" and change "numpy" to "NumPy"

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

@gliptak
Copy link
Contributor Author

gliptak commented May 31, 2016

@jreback Are you good with this PR? Thanks

@@ -264,6 +264,15 @@ def test_operators_timedelta64(self):
rs[2] += np.timedelta64(timedelta(minutes=5, seconds=1))
self.assertEqual(rs[2], value)

def test_operator_series_comparison_zerorank(self):
self.assert_series_equal((np.float64(0) > pd.Series([1, 2, 3])), (0.0 >
Copy link
Contributor

Choose a reason for hiding this comment

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

add the issue number as a comment

Copy link
Contributor Author

Choose a reason for hiding this comment

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

added

@gliptak
Copy link
Contributor Author

gliptak commented Jun 3, 2016

build is green

if len(self) != len(other):
# do not check length of zerorank array
if not lib.isscalar(lib.item_from_zerodim(other)) and \
len(self) != len(other):
raise ValueError('Lengths must match to compare')
Copy link
Member

@gfyoung gfyoung Jun 3, 2016

Choose a reason for hiding this comment

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

I've been nitpicked on this before, but @jreback is there a preference for backslash or parentheses in multi-line conditionals?

Copy link
Contributor

Choose a reason for hiding this comment

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

either one are fine really. I guess in this case parens might be nicer. I'll fix on the merge

@jreback jreback added this to the 0.18.2 milestone Jun 3, 2016
@jreback jreback closed this in 2061e9e Jun 3, 2016
@jreback
Copy link
Contributor

jreback commented Jun 3, 2016

thanks @gliptak

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Dtype Conversions Unexpected or buggy dtype conversions
Projects
None yet
Development

Successfully merging this pull request may close these issues.

BUG: Type error when comparing numpy scalar to pandas series
4 participants