반응형
데이터가 있습니다.
Symbol bid ask
Timestamp
2014-01-01 21:55:34.378000 EUR/USD 1.37622 1.37693
2014-01-01 21:55:40.410000 EUR/USD 1.37624 1.37698
2014-01-01 21:55:47.210000 EUR/USD 1.37619 1.37696
2014-01-01 21:55:57.963000 EUR/USD 1.37616 1.37696
2014-01-01 21:56:03.117000 EUR/USD 1.37616 1.37694
타임 스탬프는 GMT입니다. 그것을 동부로 변환하는 방법이 있습니까?
내가 할 때 참고 :
data.index
출력을 얻습니다.
<class 'pandas.tseries.index.DatetimeIndex'>
[2014-01-01 21:55:34.378000, ..., 2014-01-01 21:56:03.117000]
Length: 5, Freq: None, Timezone: None
해결 방법
인덱스 ( tz_localize
사용)를 UTC (타임 스탬프 시간대 인식)로 현지화 한 다음 동부로 변환 ( tz_convert
사용) :
import pytz
eastern = pytz.timezone('US/Eastern')
df.index = df.index.tz_localize(pytz.utc).tz_convert(eastern)
예를 들면 :
import pandas as pd
import pytz
index = pd.date_range('20140101 21:55', freq='15S', periods=5)
df = pd.DataFrame(1, index=index, columns=['X'])
print(df)
# X
# 2014-01-01 21:55:00 1
# 2014-01-01 21:55:15 1
# 2014-01-01 21:55:30 1
# 2014-01-01 21:55:45 1
# 2014-01-01 21:56:00 1
# [5 rows x 1 columns]
print(df.index)
# <class 'pandas.tseries.index.DatetimeIndex'>
# [2014-01-01 21:55:00, ..., 2014-01-01 21:56:00]
# Length: 5, Freq: 15S, Timezone: None
eastern = pytz.timezone('US/Eastern')
df.index = df.index.tz_localize(pytz.utc).tz_convert(eastern)
print(df)
# X
# 2014-01-01 16:55:00-05:00 1
# 2014-01-01 16:55:15-05:00 1
# 2014-01-01 16:55:30-05:00 1
# 2014-01-01 16:55:45-05:00 1
# 2014-01-01 16:56:00-05:00 1
# [5 rows x 1 columns]
print(df.index)
# <class 'pandas.tseries.index.DatetimeIndex'>
# [2014-01-01 16:55:00-05:00, ..., 2014-01-01 16:56:00-05:00]
# Length: 5, Freq: 15S, Timezone: US/Eastern
참조 페이지 https://stackoverflow.com/questions/22800079
반응형
'파이썬' 카테고리의 다른 글
파이썬 PyQt : 콤보 박스를 아이템의 텍스트 (제목)를 알고있는 아이템으로 설정하는 방법 (0) | 2020.12.17 |
---|---|
파이썬 curl 예제를 pycurl로 변환 (0) | 2020.12.17 |
파이썬 Python Win32com Outlook으로 이메일 기능을 명확하게 문서화 (0) | 2020.12.17 |
파이썬 완료하는 데 너무 오래 걸리는 경우 시간 초과 기능 (0) | 2020.12.17 |
파이썬 datetime과 Pandas Timestamp 객체 간 변환 (0) | 2020.12.17 |
댓글