30

I have two datetime columns which are naive when I read them into memory but which are in US/Eastern actually. I simply want to convert both of these columns to US/Central.

I found a method which works but it seems like I am doing a workaround. I changed my call_start and call_end columns to be named 'start' and 'end' instead so I don't end up with duplicate column names. I then created a separate datetimeindex for each of these columns and reset the index.

aht.set_index(pd.DatetimeIndex(aht['start']).tz_localize('US/Eastern').tz_convert('US/Central'), inplace = True, drop = True)
aht.index.names = ['call_start']
aht = aht.reset_index()
aht.set_index(pd.DatetimeIndex(aht['end']).tz_localize('US/Eastern').tz_convert('US/Central'), inplace = True, drop = True)
aht.index.names = ['call_end']
aht = aht.reset_index()

I end up getting:

                 call_end                  call_start              start                 end
2016-01-13 06:05:01-06:00   2016-01-13 06:02:00-06:00   01/13/2016 07:02    01/13/2016 07:05
2016-01-13 06:07:00-06:00   2016-01-13 06:03:16-06:00   01/13/2016 07:03    01/13/2016 07:07
2016-01-13 06:09:13-06:00   2016-01-13 06:06:02-06:00   01/13/2016 07:06    01/13/2016 07:09
2016-01-13 06:17:51-06:00   2016-01-13 06:06:20-06:00   01/13/2016 07:06    01/13/2016 07:17

Is this the best method? All other data is in central time so I just want to make sure that this file is too so when I merge files together it makes more sense. I do not care about having the actual timezone stamp there though - is there a way to easily strip it after I created my new columns?

prp
  • 914
  • 1
  • 9
  • 24
trench
  • 5,075
  • 12
  • 50
  • 80

1 Answers1

59

You don't need to do the roundtrip to DatetimeIndex, as these methods are avaliable for a Series (column) as well through the dt accessor:

aht['call_start'] = aht['start'].dt.tz_localize('US/Eastern').dt.tz_convert('US/Central')

And the same for end.
To remove the timezone information but keep it in the local time, you do another .dt.tz_localize(None) afterwards (see this question: https://stackoverflow.com/a/34687479/653364)

Community
  • 1
  • 1
joris
  • 133,120
  • 36
  • 247
  • 202
  • Well I am glad you answered - I literally tried the DatetimeIndex method because of this comment by you haha. I never messed with DatetimeIndexes before. I'll try the accessor now. "Note: you can't use tz_localize or tz_convert directly on a column, only on an index – joris Sep 3 '14 at 22:41 " – trench Jan 14 '16 at 13:14
  • 2
    Situation has changed since then :-) The datetime accessor was added in pandas 0.15, exactly to more easily access these DatetimeIndex methods for datetime data in a Series. – joris Jan 14 '16 at 13:32