Cauder
Cauder

Reputation: 2597

How to convert a pandas datetime column from UTC to EST

There is another question that is eleven years old with a similar title.

I have a pandas dataframe with a column of datetime.time values.

val    time
a      12:30:01.323
b      12:48:04.583
c      14:38:29.162

I want to convert the time column from UTC to EST.

I tried to do dataframe.tz_localize('utc').tz_convert('US/Eastern') but it gave me the following error: RangeIndex Object has no attribute tz_localize

Upvotes: 2

Views: 6561

Answers (3)

Haleemur Ali
Haleemur Ali

Reputation: 28253

to_datetime accepts an argument utc (bool) which, when true, coerces the timestamp to utc.

to_datetime returns a DateTimeIndex, which has a method tz_convert. this method will convert tz-aware timestamps from one timezeone to another.

So, this transformation could be concisely written as

df = pd.DataFrame(
       [['a', '12:30:01.323'],
        ['b', '12:48:04.583'],
        ['c', '14:38:29.162']],
       columns=['val', 'time']
)
df['time'] = pd.to_datetime(df.time, utc=True, format='%H:%M:%S.%f') 
# convert string to timezone aware field ^^^
df['time'] = df.time.dt.tz_convert('EST').dt.time
# convert timezone, discarding the date part ^^^

This produces the following dataframe:

  val             time
0   a  07:30:01.323000
1   b  07:48:04.583000
2   c  09:38:29.162000

This could also be a 1-liner as below:

pd.to_datetime(df.time, utc=True, format='%H:%M:%S.%f').dt.tz_convert('America/New_York').dt.time

Upvotes: 5

Saiyam Jain
Saiyam Jain

Reputation: 11

list_temp = []
for row in df['time_UTC']:
    list_temp.append(Timestamp(row, tz = 'UTC').tz_convert('US/Eastern'))
df['time_EST'] = list_temp

Upvotes: 1

not_speshal
not_speshal

Reputation: 23146

tz_localize and tz_convert work on the index of the DataFrame. So you can do the following:

  1. convert the "time" to Timestamp format
  2. set the "time" column as index and use the conversion functions
  3. reset_index()
  4. keep only the time

Try:

dataframe["time"] = pd.to_datetime(dataframe["time"],format="%H:%M:%S.%f")
output = (dataframe.set_index("time")
                   .tz_localize("utc")
                   .tz_convert("US/Eastern")
                   .reset_index()
          )
output["time"] = output["time"].dt.time

>>> output
              time val
0  15:13:12.349211   a
1  15:13:13.435233   b
2  15:13:14.345233   c

Upvotes: 3

Related Questions