2014-04-02 19 views
9

Ich habe Daten:Converting Zeitzone Pandas Dataframe

       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 

Der Zeitstempel ist der GMT. Gibt es eine Möglichkeit, das nach Osten zu konvertieren?

Hinweis, wenn ich tun:

data.index 

ich Ausgang:

<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 

Antwort

17

den Index Localize (mit tz_localize) zu UTC (auf die Zeitstempel Zeitzone-bewusst zu machen) und dann nach Osten umwandeln (mit tz_convert):

import pytz 
eastern = pytz.timezone('US/Eastern') 
df.index = index.tz_localize(pytz.utc).tz_convert(eastern) 

Zum Beispiel:

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 = 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