pandas comparing series with numpy float gives TypeError

b_pcakes

In pandas, you can do this:

>>> x = pd.DataFrame([[1,2,3,4], [3,4,5,6]], columns=list('abcd'))
>>> x
   a  b  c  d
0  1  2  3  4
1  3  4  5  6
>>> 2 < x.a
0    False
1     True
Name: a, dtype: bool

However, when I try it with it with a numpy float:

>>> np.float64(2) < x.a
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/simon/Documents/workspace/rent-my-rez/venv/lib/python2.7/site-packages/pandas/core/ops.py", line 741, in wrapper
    if len(self) != len(other):
TypeError: len() of unsized object

Is there some way around this (which doesn't involve casting the numpy float to a regular float), or some way I can monkey patch the Series class from pandas to implement reverse comparison? I've looked around in the source code for where comparison is implemented, but I couldn't find it, so a reference to the location in the code would be very helpful

(I am aware that it is easily fixed by changing the order of comparison, but I am asking this more out of interest, as I would like to understand the source code more)

mgilbert

This seems to be a known issue talked about here and fixed here, making it tough to find the source if you (like me) are running 0.18.0 and trying to find the equivalent lines on github. If you look on github at 0.18.0 instead of master you can see the relevant lines, e.g. line 739 here

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

pandas comparing series with numpy float gives TypeError

From Dev

Comparing pandas DataFrame to Series

From Dev

numpy matrix to pandas Series

From Dev

convert pandas float series to int

From Dev

Convert float Series into an integer Series in pandas

From Dev

Comparing pandas Series for equality when they contain nan?

From Java

ValueError when comparing two pandas Series

From Dev

Pandas comparing dataframe with series containing datetime

From Dev

TypeError: cannot convert the series to <class 'float'>

From Dev

Time Series using numpy or pandas

From Dev

TypeError: 'numpy.float64' object is not callable?

From Dev

TypeError: 'numpy.float64' object is not callable?

From Dev

Slicing a pandas series using a list of float slices

From Dev

scipy.optimize.newton gives TypeError: 'float' object is not callable

From Dev

Comparing two columns in pandas - error: The truth value of a Series is ambiguous

From Dev

TypeError: cannot concatenate 'str' and 'float' objects : pandas

From Dev

Pandas Series TypeError and ValueError when using datetime

From Dev

Pandas/Numpy: remove leading/trailing nan in a pandas series or numpy array

From Dev

Pandas series to numpy array conversion error

From Dev

time series data indexing using pandas or numpy

From Dev

How to handle Series and Array with pandas and numpy together?

From Dev

How to convert a numpy matrix to a pandas series?

From Dev

Pandas Series to_numpy() preserve names of indices

From Dev

pandas numpy series days getting shifted by 1

From Dev

Numpy - float32 gives different value from dtype="float32" in array

From Dev

TypeError: unsupported operand type(s) for ^: 'numpy.float64' and 'numpy.float64'

From Dev

how to convert pandas Series of Numpy object into a Numpy matrix?

From Dev

python "TypeError: 'numpy.float64' object cannot be interpreted as an integer"

From Dev

Scipy Newton with derivative: TypeError: 'numpy.float64' object is not callable

Related Related

  1. 1

    pandas comparing series with numpy float gives TypeError

  2. 2

    Comparing pandas DataFrame to Series

  3. 3

    numpy matrix to pandas Series

  4. 4

    convert pandas float series to int

  5. 5

    Convert float Series into an integer Series in pandas

  6. 6

    Comparing pandas Series for equality when they contain nan?

  7. 7

    ValueError when comparing two pandas Series

  8. 8

    Pandas comparing dataframe with series containing datetime

  9. 9

    TypeError: cannot convert the series to <class 'float'>

  10. 10

    Time Series using numpy or pandas

  11. 11

    TypeError: 'numpy.float64' object is not callable?

  12. 12

    TypeError: 'numpy.float64' object is not callable?

  13. 13

    Slicing a pandas series using a list of float slices

  14. 14

    scipy.optimize.newton gives TypeError: 'float' object is not callable

  15. 15

    Comparing two columns in pandas - error: The truth value of a Series is ambiguous

  16. 16

    TypeError: cannot concatenate 'str' and 'float' objects : pandas

  17. 17

    Pandas Series TypeError and ValueError when using datetime

  18. 18

    Pandas/Numpy: remove leading/trailing nan in a pandas series or numpy array

  19. 19

    Pandas series to numpy array conversion error

  20. 20

    time series data indexing using pandas or numpy

  21. 21

    How to handle Series and Array with pandas and numpy together?

  22. 22

    How to convert a numpy matrix to a pandas series?

  23. 23

    Pandas Series to_numpy() preserve names of indices

  24. 24

    pandas numpy series days getting shifted by 1

  25. 25

    Numpy - float32 gives different value from dtype="float32" in array

  26. 26

    TypeError: unsupported operand type(s) for ^: 'numpy.float64' and 'numpy.float64'

  27. 27

    how to convert pandas Series of Numpy object into a Numpy matrix?

  28. 28

    python "TypeError: 'numpy.float64' object cannot be interpreted as an integer"

  29. 29

    Scipy Newton with derivative: TypeError: 'numpy.float64' object is not callable

HotTag

Archive