Type error: cannot convert the series to <class 'float'>

16,443

The problem is quite trivial,

  1. You're using a Pandas.DataFrame. Now when you slice it rep_points['lat'], you get a Pandas.Series.
  2. The gmplot.scatter() is expecting an iterable of floats not a series of floats.
  3. Now if you convert your Pandas.Series to a list by using rep_points['lat'].tolist() It'll start working

Below is your updated code:

rep_points = pd.read_csv(r'C:\Users\carrot\Desktop\ss.csv', dtype=float)
latitude_collection = rep_points['lat'].tolist()
longitude_collection = rep_points['long'].tolist()

gmap = gmplot.GoogleMapPlotter(latitude_collection[0], longitude_collection[0], 11)
gmap.plot(min(latitude_collection), min(longitude_collection).lng)
gmap.scatter(latitude_collection,longitude_collection,c='aquamarine')
gmap.circle(latitude_collection,longitude_collection, 100, color='yellow')  
gmap.draw("user001_clus_time.html")

Other things that helped to point it out:

  1. type(rep_points['lat']) is a Pandas.Series
  2. type(rep_points['lat'][0]) is a Numpy.Float
  3. to iterate over a Pandas.Series you need to use iteritems
Share:
16,443

Related videos on Youtube

AkankshaC
Author by

AkankshaC

Updated on June 04, 2022

Comments

  • AkankshaC
    AkankshaC almost 2 years
          lat        long       time
     0  39.991861  116.344372   2.823611
     1  39.979768  116.310597  22.263056
     2  31.235001  121.470624  13.141667
     3  31.248822  121.460637   1.805278
    

    The above is a dataframe rep_points. When i run the code below, it gives an error

    Type error: cannot convert the series to <class 'float'> 
    

    in the line where circle is made.

    gmap = gmplot.GoogleMapPlotter(rep_points['lat'][0], rep_points['long'][0], 11)
    gmap.plot(df_min.lat, df_min.lng)
    gmap.scatter(rep_points['lat'],rep_points['long'],c='aquamarine')
    gmap.circle(rep_points['lat'],rep_points['long'], 100, color='yellow')  
    gmap.draw("user001_clus_time.html")
    

    How should i resolve this error? Ive tried using

    rep_pints['lat'].astype(float) 
    

    and

    rep_pints['long'].astype(float) 
    

    but it didnt work well

    • jezrael
      jezrael about 6 years
      So rep_points = rep_points.apply(pd.to_numeric, errors='coerce') or rep_points = rep_points.apply(pd.to_numeric, errors='coerce').dropna() for remove rows with missing values (created by converting non numeric values) should working
    • iam.Carrot
      iam.Carrot about 6 years
      @jezrael That's not even the same error. I am not sure that's gonna help
    • jezrael
      jezrael about 6 years
      @iam.Carrot - OK, so reopened.
  • AkankshaC
    AkankshaC about 6 years
    If i use the above code i get the error TypeError: zip argument #1 must support iteration in line gmap.plot @iam.Carrot
  • iam.Carrot
    iam.Carrot about 6 years
    I referred to this. can you please share your stack trace
  • AkankshaC
    AkankshaC about 6 years
    latitude_collection = rep_points['lat'].tolist() longitude_collection = rep_points['long'].tolist() gmap = gmplot.GoogleMapPlotter(latitude_collection[0], longitude_collection[0], 11) gmap.plot(min(latitude_collection),min(longitude_collection)‌​) gmap.scatter(latitude_collection,longitude_collection,c='aqu‌​amarine') #gmap.heatmap(rep_points['lat'],rep_points['long']) gmap.circle(latitude_collection,longitude_collection, 100, color='yellow') gmap.draw("user001_clus_time.html") @iam.Carrot
  • iam.Carrot
    iam.Carrot about 6 years
    @AkankshaC I need the stack trace not the code. When an exception is thrown then what is the error message and what threw the error.
  • iam.Carrot
    iam.Carrot about 6 years
    @AkankshaC Please use the question to put in your StackTrace Use the edit question. Also, Why do you have gmap.marker(rep_points['lat'],rep_points['lon'],title="Clust‌​er") This code should not be there
  • AkankshaC
    AkankshaC about 6 years
    ------------------------------------------------------------‌​--------------- TypeError Traceback (most recent call last) <ipython-input-188-7a522e6fc5f3> in <module>() 49 50 gmap = gmplot.GoogleMapPlotter(latitude_collection[0], longitude_collection[0], 11) ---> 51 gmap.plot(min(latitude_collection),min(longitude_collection)‌​) 52 gmap.scatter(latitude_collection,longitude_collection,c='aqu‌​amarine') 53
  • AkankshaC
    AkankshaC about 6 years
    ~/anaconda3/lib/python3.6/site-packages/gmplot/gmplot.py in plot(self, lats, lngs, color, c, **kwargs) 118 kwargs.setdefault("color", color) 119 settings = self._process_kwargs(kwargs) --> 120 path = zip(lats, lngs) 121 self.paths.append((path, settings)) 122 TypeError: zip argument #1 must support iteration
  • iam.Carrot
    iam.Carrot about 6 years
    @AkankshaC can you please update the question with this stack trace? It's tough to figure out things with this formatting. Also, the issue is the zip function the lats is not a collection. Where is the zip in the code?