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)
datetime's fromtimestamp is actually from a POSIX timestamp i.e. ms from 1970-1-1 GMT
I think maybe is an issue as I'm in PST timezone.
This is different from pandas Timestamp (although under the hood that is ns from 1970-1-1).
To convert a Timestamp/datetime64 column use tz_convert (if the are tz naive, i.e. don't have a timezone yet, you'll need to tz_localize first):
See the time-zone-handling section of the docs.
Just use
tz_convert
method.Lets say you have a Timestamp object:
If you are interested in converting date ranges:
For large time series:
As stated in another answer, if your data does not have a timezone set, you'll need to
tz_localize
it: