2014-09-28 8 views
7

Sul seguente serie:Impossibile applicare i metodi su timestamp utilizzando Serie built-in

0 1411161507178 
1 1411138436009 
2 1411123732180 
3 1411167606146 
4 1411124780140 
5 1411159331327 
6 1411131745474 
7 1411151831454 
8 1411152487758 
9 1411137160544 
Name: my_series, dtype: int64 

Questo comando (convertire in timestamp, localizzare e convertire in EST) funziona:

pd.to_datetime(my_series, unit='ms').apply(lambda x: x.tz_localize('UTC').tz_convert('US/Eastern')) 

ma questo fallisce:

pd.to_datetime(my_series, unit='ms').tz_localize('UTC').tz_convert('US/Eastern') 

con:

TypeError         Traceback (most recent call last) 
<ipython-input-3-58187a4b60f8> in <module>() 
----> 1 lua = pd.to_datetime(df[column], unit='ms').tz_localize('UTC').tz_convert('US/Eastern') 

/Users/josh/anaconda/envs/py34/lib/python3.4/site-packages/pandas/core/generic.py in tz_localize(self, tz, axis, copy, infer_dst) 
    3492     ax_name = self._get_axis_name(axis) 
    3493     raise TypeError('%s is not a valid DatetimeIndex or PeriodIndex' % 
-> 3494         ax_name) 
    3495    else: 
    3496     ax = DatetimeIndex([],tz=tz) 

TypeError: index is not a valid DatetimeIndex or PeriodIndex 

e così fa questo:

my_series.tz_localize('UTC').tz_convert('US/Eastern') 

con:

--------------------------------------------------------------------------- 
TypeError         Traceback (most recent call last) 
<ipython-input-4-0a7cb1e94e1e> in <module>() 
----> 1 lua = df[column].tz_localize('UTC').tz_convert('US/Eastern') 

/Users/josh/anaconda/envs/py34/lib/python3.4/site-packages/pandas/core/generic.py in tz_localize(self, tz, axis, copy, infer_dst) 
    3492     ax_name = self._get_axis_name(axis) 
    3493     raise TypeError('%s is not a valid DatetimeIndex or PeriodIndex' % 
-> 3494         ax_name) 
    3495    else: 
    3496     ax = DatetimeIndex([],tz=tz) 

TypeError: index is not a valid DatetimeIndex or PeriodIndex 

Per quanto ho capito, il secondo approccio sopra (il primo che fallisce) dovrebbe funzionare . Perché fallisce?

+0

Io non sono sicuro perché il vostro primo metodo funziona infatti, come per il secondo la dichiarazione di errore è del tutto evidente, se in effetti l'indice era i valori Int64 poi la seconda opere approccio . – EdChum

risposta

5

tz_localize/tz_convert agire sull'INDICE dell'oggetto, non sui valori. È più semplice trasformarlo in un indice, quindi localizzarlo e convertirlo. Se poi si desidera una Serie di nuovo è possibile utilizzare to_series()

In [47]: pd.DatetimeIndex(pd.to_datetime(s,unit='ms')).tz_localize('UTC').tz_convert('US/Eastern') 
Out[47]: 
<class 'pandas.tseries.index.DatetimeIndex'> 
[2014-09-19 17:18:27.178000-04:00, ..., 2014-09-19 10:32:40.544000-04:00] 
Length: 10, Freq: None, Timezone: US/Eastern 
14

Come risposta di Jeff menziona, tz_localize() e tz_convert() agire sull'indice, non i dati. Questa è stata una grande sorpresa anche per me.

Poiché la risposta di Jeff è stata scritta, Pandas 0.15 ha aggiunto un nuovo accessorio Series.dt che aiuta il vostro caso d'uso. È ora possibile fare questo:

pd.to_datetime(my_series, unit='ms').dt.tz_localize('UTC').dt.tz_convert('US/Eastern') 
+0

Questo è fantastico! Non voglio impostare TimeStamp come indice, a volte potremmo avere due TimeStamp in quanto è davvero frustrante dover convertirlo in indice. – user40780

Problemi correlati