11

I have a column (non-index column) that has datetimes inside it. For example, the first five entries look something like this:

[Timestamp('2018-11-15 19:57:55'),
 Timestamp('2018-11-15 19:59:46'),
 Timestamp('2018-11-15 20:00:59'),
 Timestamp('2018-11-15 20:01:41'),
 Timestamp('2018-11-15 20:01:54')]

I want to convert the entries from UTC to the Pacific timezone. Assuming the column is called times I am currently doing the following:

times.dt.tz_localize('GMT').dt.tz_convert('America/Los_Angeles')

While this successfully converts the column from UTC to PST, the output has extraneous components that I do not want. It looks like the following:

[Timestamp('2018-11-15 11:57:55-0800', tz='America/Los_Angeles'),
 Timestamp('2018-11-15 11:59:46-0800', tz='America/Los_Angeles'),
 Timestamp('2018-11-15 12:00:59-0800', tz='America/Los_Angeles'),
 Timestamp('2018-11-15 12:01:41-0800', tz='America/Los_Angeles'),
 Timestamp('2018-11-15 12:01:54-0800', tz='America/Los_Angeles')]

How do I remove or ignore the -0800 from the timestamps? Thanks!

FObersteiner
  • 22,500
  • 8
  • 42
  • 72
Jane Sully
  • 3,137
  • 10
  • 48
  • 87
  • I looked at that, but their output was in the form `16:56:00-05:00`, similar to the issue I brought up. I don't know what they `05:00` represents or why that happens, but I don't want that in my output. – Jane Sully Dec 11 '18 at 22:41
  • Possible duplicate of [Converting time zone pandas dataframe](https://stackoverflow.com/questions/22800079/converting-time-zone-pandas-dataframe) – david Dec 12 '18 at 03:01

1 Answers1

21

Just add a last step of .tz_localize(None):

import pandas as pd
d = pd.Series(['2018-11-15 19:57:55', '2018-11-15 19:59:46'])
d = pd.to_datetime(d)
d
0   2018-11-15 19:57:55
1   2018-11-15 19:59:46
dtype: datetime64[ns]

d_pacific_tz_aware = d.dt.tz_localize("GMT").dt.tz_convert('America/Los_Angeles')
d_pacific_tz_aware
0   2018-11-15 11:57:55-08:00
1   2018-11-15 11:59:46-08:00
dtype: datetime64[ns, America/Los_Angeles]


d_pacific_tz_naive = d.dt.tz_localize("GMT").dt.tz_convert('America/Los_Angeles').dt.tz_localize(None)
d_pacific_tz_naive
0   2018-11-15 11:57:55
1   2018-11-15 11:59:46
dtype: datetime64[ns]
Neroksi
  • 1,301
  • 1
  • 12
  • 20