1

I have a dataframe with EPOCH Time in nanoseconds that I would like to convert to Eastern Standard Time format. I use something like:

df['EST'] = pd.to_datetime(df['EPOCH'], unit="ns", utc=True).dt.tz_convert('US/Eastern')

Returns for example:

05-24-2021 13:05:53.631347-04:00

I would like to get rid of the -04:00 so I do something like:

df['EST'] = pd.to_datetime(df['EPOCH'], unit="ns", utc=True).dt.tz_convert('US/Eastern').dt.strftime('%m-%d-%Y %H:%M:%S.%f')

Output is now something like (which is my desired output):

05-24-2021 13:05:53.631347

Is this the best way to do this? Or is there a more efficient way to do it? It seems like I am doing a lot of conversion, so not sure if its the most efficient.

user1179317
  • 2,693
  • 3
  • 34
  • 62
  • 1
    If string datatype is what you need, that's the way to go I'd say. If you want to preserve datetime datatype, just without the time zone information, you can convert to None (`.dt.tz_covert(None)`). – FObersteiner May 24 '21 at 17:58
  • Actually, if I do that it converts it back to the UTC format -- same as before converting to EST – user1179317 May 24 '21 at 18:06
  • I did. It converted it back to UTC. (ie pd.to_datetime(df['EPOCH'], unit="ns", utc=True).dt.tz_convert('US/Eastern').dt.tz_convert(None) ) – user1179317 May 24 '21 at 18:31
  • 1
    oh man sorry, I mean `.dt.tz_localize(None)`, not `tz_convert` - sorry for the confusion! – FObersteiner May 24 '21 at 19:17
  • you might also want to check out [Convert pandas timezone-aware DateTimeIndex to naive timestamp, but in certain timezone](https://stackoverflow.com/q/16628819/10197418) – FObersteiner May 24 '21 at 19:48
  • 1
    Ah thanks, `tz_localize(None)` works great. And 5x faster than converting with strftime – user1179317 May 24 '21 at 20:38

0 Answers0