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:
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)
tz_localize(None) method can be applied to the dataframe column to remove the timezone information.
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.
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')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With