본문 바로가기
분석/파이썬 Python

Python : 위도.경도로 TimeZone 구하기

by 여우요원 2020. 3. 10.
반응형

 

Python에서 위도와 경도 값으로 TimeZone 구하기

 

먼저 'timezonefinder' 라는 패키지를 설치하여야 한다.

pip install timezonefinder

사용법은 아래와 같이 간단하다.

from timezonefinder import TimezoneFinder

tf = TimezoneFinder()
latitude, longitude = 52.5061, 13.358
tf.timezone_at(lng=longitude, lat=latitude) # returns 'Europe/Berlin'

 

* 만일 아래와 같이 DataFrame에 위.경도의 값이 있다고 하면 

from timezonefinder import TimezoneFinder

my_func = TimezoneFinder().timezone_at
df['tz'] = df_users.apply(lambda x: my_func(lng=x['longitude'], lat=x['latitude']),axis=1)

timezone을 구할 수 있다.

 

[결과]

 

 

* 만일 아래와 같은 DataFrame 이 있다고 하면,

'utc' 컬럼을 datetime 타입으로 변환한 후에 Localize를 할 수 있다.

df['utc'] = pd.to_datetime(df['utc'])

df['local_datetime'] = df.apply(lambda x: x.utc.tz_localize(tz = "UTC").tz_convert(x.tz).tz_localize(None), axis = 1)

[결과]

반응형