python - Pandas change timezone for forex DataFrame -


how change timezone in pandas dataframe?

i wrote simple code read 1min eurusd data (datetime/open/high/low/close/vol). sample data in est timezone, need convert utc.

df.tz_convert(pytz.timezone('utc')) 

is failing. error

typeerror: cannot convert tz-naive timestamps, use tz_localize localize

sample data dat_ascii_eurusd_m1_smallsample.csv file:

20160103 170000;1.087010;1.087130;1.087010;1.087130;0 20160103 170100;1.087120;1.087120;1.087120;1.087120;0 20160103 170200;1.087080;1.087220;1.087080;1.087220;0 20160104 000100;1.087830;1.087840;1.087640;1.087640;0 20160104 000200;1.087640;1.088220;1.087640;1.088220;0 20160104 000300;1.088220;1.088220;1.088040;1.088050;0 20160105 000000;1.082270;1.082270;1.082160;1.082160;0 20160105 000100;1.082160;1.082160;1.082130;1.082140;0 20160105 000200;1.082150;1.082240;1.082150;1.082240;0

import pandas pd import pytz  filename = "dat_ascii_eurusd_m1_smallsample.csv"  df = pd.read_csv(filename, sep=";", names=['datetime','open','high','low','close','vol'],                  parse_dates = [0], index_col = 'datetime')  df.tz_localize(pytz.timezone('us/eastern')) df.tz_convert(pytz.timezone('utc'))  print(df) 

you should use:

df = df.tz_localize(pytz.timezone('us/eastern')) df = df.tz_convert(pytz.timezone('utc')) 

since tz_localize not in-place operation, instead returns new dataframe.


Comments

Popular posts from this blog

asynchronous - C# WinSCP .NET assembly: How to upload multiple files asynchronously -

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -