Search code examples
pythonpandasdataframefloating-pointpandas-loc

Pandas df.loc comparing-floats-condition never works


df[['gc_lat', 'gc_lng']] = df[['gc_lat', 'gc_lng']].apply(pd.to_numeric, errors='ignore')
df_realty[['lat', 'lng']] = df_realty[['lat', 'lng']].apply(pd.to_numeric, errors='ignore')
 for index, row in df.iterrows():
        gc_lat = float(df.get_value(index,'gc_lat'))
        gc_lng = float(df.get_value(index, 'gc_lng'))
        latmax = gc_lat + 1/110.574*radius_km
        latmin = gc_lat - 1/110.574*radius_km
        longmax = gc_lng + 1/111.320*radius_km*cos(df.get_value(index,'gc_lat'))
        longmin = gc_lng - 1/111.320*radius_km*cos(df.get_value(index,'gc_lat'))
        print(latmax, latmin, longmax, longmin)
        print (gc_lat)
        print (gc_lng)
        print (df_realty.shape)
        subset = df_realty.loc[(df_realty['lat']<latmax) & (df_realty['lat']>latmin) & (df_realty['lng']>longmin) & (df_realty['lng'] <longmax)]
        print (subset.shape)
        print ('subset selected!')

prints

59.12412758664786 59.03369041335215 37.88659685779323 37.960157142206775
59.078909
37.923377
(290584, 3)
(0, 3)
subset selected!

So I am trying to split Dataframe to subsets, but the condition I put in df.loc never works!

The data in df_realty is OK, already tested.

It seems like I have to explict some type casts, but i've already made one (pd.to_numeric)

Any suggestions?


Solution

  • Found a solution

    The problem was that the longmax sometimes became smaller than longmin because cos sometimes returns negative float.

    puting abs() in front of cosinus solved the problem