close

How to convert UTC to EST with Python and take care of daylight saving automatically?

Hello Guys, How are you all? Hope You all Are Fine. Today We Are Going To learn about How to convert UTC to EST with Python and take care of daylight saving automatically in Python. So Here I am Explain to you all the possible Methods here.

Without wasting your time, Let’s start This Article.

How to convert UTC to EST with Python and take care of daylight saving automatically?

  1. How to convert UTC to EST with Python and take care of daylight saving automatically?

    If you have a pandas series with object datatype, you can first convert it into a DateTime series using pd.to_datetime()

  2. convert UTC to EST with Python and take care of daylight saving automatically

    If you have a pandas series with object datatype, you can first convert it into a DateTime series using pd.to_datetime()

Method 1

You’ll need to use the pytz module (available from PyPI):

import pytz
from datetime import datetime

est = pytz.timezone('US/Eastern')
utc = pytz.utc
fmt = '%Y-%m-%d %H:%M:%S %Z%z'

winter = datetime(2016, 1, 24, 18, 0, 0, tzinfo=utc)
summer = datetime(2016, 7, 24, 18, 0, 0, tzinfo=utc)

print winter.strftime(fmt)
print summer.strftime(fmt)

print winter.astimezone(est).strftime(fmt)
print summer.astimezone(est).strftime(fmt)

which will print:

2016-01-24 18:00:00 UTC+0000
2016-07-24 18:00:00 UTC+0000
2016-01-24 13:00:00 EST-0500
2016-07-24 14:00:00 EDT-0400

The reason why you’ll need to use 'US/Eastern' and not 'EST' is exemplified in the last two lines of output.

Method 2

If you have a pandas series with object datatype, you can first convert it into a DateTime series using pd.to_datetime()

df[col] = pd.to_datetime(your_series, format = '%Y-%m-%d %H:%M:%S', errors ='coerce')

Check if it is timezone aware or not by using series.dt.tz

df[col].dt.tz

If it’s not timezone aware, we should make it timezone aware by using series.dt.tz_localize(). Also, do read about the ambiguous and nonexistent parameters of this function

df[col] = your_series[col].dt.tz_localize('UTC')

Now convert this series into the required timezone by series.dt.tz_convert()

df[col] = your_series[col].dt.tz_convert('US/Eastern')

The above method will take care of daylight savings time. If you want to check more timezones you can pip install pytz and

import pytz
pytz.common_timezones

Summery

It’s all About this issue. Hope all Methods helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which Method worked for you? Thank You.

Also, Read