Skip to content
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

BUG: Type error when comparing numpy scalar to pandas series #13006

Closed
rocurley opened this issue Apr 27, 2016 · 3 comments
Closed

BUG: Type error when comparing numpy scalar to pandas series #13006

rocurley opened this issue Apr 27, 2016 · 3 comments
Labels
Bug Dtype Conversions Unexpected or buggy dtype conversions
Milestone

Comments

@rocurley
Copy link

Hi,

I'm getting strange results when comparing a numpy float to a pandas series. It seems to think that the numpy float is some sort of array...

import numpy as np
import pandas as pd
np.float64(0) < pd.Series([1,2,3],dtype=np.float64)

Expected Output

0    True
1    True
2    True
dtype: bool

Actual Output

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-3-0803a1472416> in <module>()
      1 import numpy as np
      2 import pandas as pd
----> 3 np.float64(0) < pd.Series([1,2,3],dtype=np.float64)
        global np.float64 = <type 'numpy.float64'>
        global pd.Series = <class 'pandas.core.series.Series'>
        global dtype = undefined

/usr/local/lib/python2.7/dist-packages/pandas/core/ops.pyc in wrapper(self=0    1.0
1    2.0
2    3.0
dtype: float64, other=array(0.0), axis=None)
    737             return NotImplemented
    738         elif isinstance(other, (np.ndarray, pd.Index)):
--> 739             if len(self) != len(other):
        global len = undefined
        self = 0    1.0
1    2.0
2    3.0
dtype: float64
        other = array(0.0)
    740                 raise ValueError('Lengths must match to compare')
    741             return self._constructor(na_op(self.values, np.asarray(other)),

TypeError: len() of unsized object


output of pd.show_versions()

INSTALLED VERSIONS
------------------
commit: None
python: 2.7.6.final.0
python-bits: 64
OS: Linux
OS-release: 4.3.0-040300-generic
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8

pandas: 0.18.0
nose: None
pip: 8.1.1
setuptools: 20.3.1
Cython: None
numpy: 1.11.0
scipy: 0.17.0
statsmodels: None
xarray: None
IPython: 4.0.0
sphinx: None
patsy: None
dateutil: 2.5.3
pytz: 2015.7
blosc: None
bottleneck: None
tables: None
numexpr: None
matplotlib: 1.5.0
openpyxl: None
xlrd: None
xlwt: None
xlsxwriter: None
lxml: None
bs4: None
html5lib: None
httplib2: 0.8
apiclient: None
sqlalchemy: None
pymysql: None
psycopg2: None
jinja2: 2.8
boto: None
@jreback
Copy link
Contributor

jreback commented Apr 27, 2016

well a np.float64(0) IS an array, its a 0-dim one. You generally will never need to use numpy scalars like this. I guess this is a bug.

try

In [35]: 0 < pd.Series([1,2,3],dtype=np.float64)
Out[35]: 
0    True
1    True
2    True
dtype: bool

@jreback jreback added Bug Difficulty Novice Dtype Conversions Unexpected or buggy dtype conversions labels Apr 27, 2016
@jreback jreback added this to the Next Major Release milestone Apr 27, 2016
@jreback jreback changed the title Type error when comparing numpy number to pandas series BUG: Type error when comparing numpy scalar to pandas series Apr 27, 2016
@rocurley
Copy link
Author

A slightly more realistic example would be:

(np.array([0,1,2])[0]) < pd.Series([0,1,2])

@kawochen
Copy link
Contributor

Note that pd.Series([1,2,3],dtype=np.float64) > np.float64(0) works, so we can look at how the two code paths differ.

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 a pull request may close this issue.

3 participants