Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting timezones from pandas Timestamps

I have the following in a dataframe:

> df['timestamps'].loc[0]
Timestamp('2014-09-02 20:24:00')

I know the timezone (I think it is GMT) it uses and would like to convert the entire column to EST. How can I do that in Pandas?

For reference, I found these other threads:

  • Changing a unix timestamp to a different timezone
  • Pandas: Read Timestamp from CSV in GMT then resample

but they work with datetime timestamps. E.g.:

> datetime.datetime.fromtimestamp(df['timestamps'].loc[0], tz=None)
returns:

TypeError                                 Traceback (most recent call last)
----> 2 datetime.datetime.fromtimestamp(ts, tz=None)

TypeError: an integer is required (got type Timestamp)
like image 880
Amelio Vazquez-Reina Avatar asked Sep 03 '14 20:09

Amelio Vazquez-Reina


People also ask

How remove timezone from a timestamp column in a pandas DataFrame?

tz_localize(None) method can be applied to the dataframe column to remove the timezone information.

Is pandas timestamp the same as datetime?

Timestamp is the pandas equivalent of python's Datetime and is interchangeable with it in most cases. It's the type used for the entries that make up a DatetimeIndex, and other timeseries oriented data structures in pandas.


1 Answers

Just use tz_convert method.

Lets say you have a Timestamp object:

   stamp = Timestamp('1/1/2014 16:20', tz='America/Sao_Paulo')
   new_stamp = stamp.tz_convert('US/Eastern')

If you are interested in converting date ranges:

   range = date_range('1/1/2014', '1/1/2015', freq='S', tz='America/Sao_Paulo')
   new_range = range.tz_convert('US/Eastern')

For large time series:

   import numpy as np
   ts = Series(np.random.randn(len(range)), range)
   new_ts = ts.tz_convert('US/Eastern')

As stated in another answer, if your data does not have a timezone set, you'll need to tz_localize it:

   data.tz_localize('utc')
like image 62
rhlobo Avatar answered Oct 18 '22 00:10

rhlobo