Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

understanding math errors in pandas dataframes

I'm trying to generate a new column in a pandas dataframe from other columns and am getting some math errors that I don't understand. Here is a snapshot of the problem and some simplifying diagnostics...

I can generate a data frame that looks pretty good:

import pandas
import math as m

data = {'loc':['1','2','3','4','5'],
        'lat':[61.3850,32.7990,34.9513,14.2417,33.7712],
        'lng':[-152.2683,-86.8073,-92.3809,-170.7197,-111.3877]}
frame = pandas.DataFrame(data)

frame

Out[15]:
lat lng loc
0    61.3850    -152.2683    1
1    32.7990     -86.8073    2
2    34.9513     -92.3809    3
3    14.2417    -170.7197    4
4    33.7712    -111.3877    5
5 rows × 3 columns

I can do simple math (i.e. degrees to radians):

In [32]:
m.pi*frame.lat/180.

Out[32]:
0    1.071370
1    0.572451
2    0.610015
3    0.248565
4    0.589419
Name: lat, dtype: float64

But I can't convert from degrees to radians using the python math library:

 In [33]:
 m.radians(frame.lat)

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-33-99a986252f80> in <module>()
----> 1 m.radians(frame.lat)

/Users/user/anaconda/lib/python2.7/site-packages/pandas/core/series.pyc in wrapper(self)
     72             return converter(self.iloc[0])
     73         raise TypeError(
---> 74             "cannot convert the series to {0}".format(str(converter)))
     75     return wrapper
     76 

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

And can't even convert the values to floats to try to force it to work:

In [34]:

float(frame.lat)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-34-3311aee92f31> in <module>()
----> 1 float(frame.lat)

/Users/user/anaconda/lib/python2.7/site-packages/pandas/core/series.pyc in wrapper(self)
     72             return converter(self.iloc[0])
     73         raise TypeError(
---> 74             "cannot convert the series to {0}".format(str(converter)))
     75     return wrapper
     76 

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

I'm sure there must be a simple explanation and would appreciate your help in finding it. Thanks!

like image 803
user3654387 Avatar asked May 19 '14 23:05

user3654387


People also ask

How do you check for missing values in pandas?

In order to check missing values in Pandas DataFrame, we use a function isnull() and notnull(). Both function help in checking whether a value is NaN or not. These function can also be used in Pandas Series in order to find null values in a series.

How do you know if two pandas DataFrames are equal?

Pandas DataFrame: equals() function The equals() function is used to test whether two objects contain the same elements. This function allows two Series or DataFrames to be compared against each other to see if they have the same shape and elements. NaNs in the same location are considered equal.


1 Answers

math functions such as math.radians expect a numeric value such as a float, not a sequence such as a pandas.Series.

Instead, you could use numpy.radians, since numpy.radians can accept an array as input:

In [95]: np.radians(frame['lat'])
Out[95]: 
0    1.071370
1    0.572451
2    0.610015
3    0.248565
4    0.589419
Name: lat, dtype: float64

Only Series of length 1 can be converted to a float. So while this works,

In [103]: math.radians(pd.Series([1]))
Out[103]: 0.017453292519943295

in general it does not:

In [104]: math.radians(pd.Series([1,2]))
TypeError: cannot convert the series to <type 'float'>

math.radians is calling float on its argument. Note that you get the same error calling float on pd.Series([1,2]):

In [105]: float(pd.Series([1,2]))
TypeError: cannot convert the series to <type 'float'>
like image 110
unutbu Avatar answered Oct 27 '22 11:10

unutbu