2
from timezonefinder import TimezoneFinder
import pandas as pd

tf = TimezoneFinder()
df = pd.DataFrame({'latitude': [-22.540556,-22.950556,-22.967778], 'longitude': [-43.149167,-43.230833,-43.234444], 'timezone': [0,0,0]})
TimeZone = tf.timezone_at(lng=df['longitude'], lat=df['latitude'])
df['timezone'].apply(TimeZone)

print(df)

Hello, new to Python and struggling to get TimeZoneFinder to work for me. I want to apply timezone_at() to the TimeZone column based on geolocations from 2 other columns. Any advice on how to make this work?

Error:

Traceback (most recent call last):
  File "C:/Users/mhembree/PycharmProjects/Python/Test Column Add.py", line 17, in <module>
    TimeZone = tf.timezone_at(lng=df['longitude'], lat=df['latitude'])
  File "C:\Program Files (x86)\Python 3.5\lib\site-packages\timezonefinder\functional.py", line 27, in wrapper
    return func(*args, **kwargs)
  File "C:\Program Files (x86)\Python 3.5\lib\site-packages\timezonefinder\timezonefinder.py", line 483, in timezone_at
    if lng > 180.0 or lng < -180.0 or lat > 90.0 or lat < -90.0:
  File "C:\Program Files (x86)\Python 3.5\lib\site-packages\pandas\core\generic.py", line 955, in __nonzero__
    .format(self.__class__.__name__))
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
Marjan Moderc
  • 2,747
  • 23
  • 44
  • 1
    Your latitude and longitude are arrays rather than single values. It appears you have three separate coordinates in your data. Why? – Matt Johnson-Pint Jun 12 '17 at 16:23
  • 1
    Thanks for the reply, the use case is that i have a table of locations with lat and long. I want to populate the timezone column using a function. latitude longitude timezone -22.540556 -43.149167 0 -22.950556 -43.230833 0 -22.967778 -43.234444 0 I realize that what i am probably passing to the function is a string of coordinates that is being regarded as 1 value and causing my error. Must i use a loop and iterate through each row or is it possible to use apply()? – user4462287 Jun 12 '17 at 16:44
  • Edited your question to tag pandas and dataframe. I'm not an expert in this area, so I'll let someone else actually answer this question, or you can answer it yourself if you figure it out. [This tutorial](https://chrisalbon.com/python/pandas_apply_operations_to_dataframes.html) seems to indicate that you are not defining the function using the correct syntax. – Matt Johnson-Pint Jun 12 '17 at 17:29

1 Answers1

3

You were quite close actually! My preferred way of using columns as input to the random function and saving it into a new column is the most highly rated one in this thread. According to it, your problem could be solved like this:

from timezonefinder import TimezoneFinder
import pandas as pd

my_func = TimezoneFinder().timezone_at  #Note the no parenthesis on the function call!
df = pd.DataFrame({'latitude': [-22.540556,-22.950556,-22.967778], 'longitude': [-43.149167,-43.230833,-43.234444], 'timezone': [0,0,0]})
df['timezone'] = df.apply(lambda x: my_func(lng=x['longitude'], lat=x['latitude']),axis=1)

That would produce the result that you are after:

    latitude  longitude           timezone
0 -22.540556 -43.149167  America/Sao_Paulo
1 -22.950556 -43.230833  America/Sao_Paulo
2 -22.967778 -43.234444  America/Sao_Paulo
Marjan Moderc
  • 2,747
  • 23
  • 44