在Pandas DataFrame上应用TimeZoneFinder函数

时间:2017-06-12 14:47:30

标签: python-3.x pandas dataframe timezone apply

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)

您好,对Python很陌生,并努力让TimeZoneFinder为我工作。我想基于来自其他2列的地理定位将timezone_at()应用于TimeZone列。关于如何使这项工作的任何建议?

错误:

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().

1 个答案:

答案 0 :(得分:3)

你真的很亲密!我使用列作为随机函数输入并将其保存到新列的首选方法是this thread中评分最高的方法。根据它,你的问题可以这样解决:

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)

这会产生你想要的结果:

    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