1

(Not a duplicate question)

In this question, I am using Python3 and Pandas

In my raw dataset, the datetime was in GMT

datetime format: 2017-01-01 00:00:00

I converted the GMT datetime into EST by doing this:

df['Gmt time'] = df['Gmt time'].dt.tz_localize('GMT').dt.tz_convert('America/New_York')

The new datetime now look like this:

2016-12-31 19:00:00-05:00

now when I do:

df['Gmt time'] = pd.DatetimeIndex(df['Gmt time'])

I get this error:

TypeError: [datetime.datetime(2016, 12, 31, 19, 0, tzinfo=tzoffset(None, -18000))

I tried to fix it by doing this:

df['Gmt time'] = pd.DatetimeIndex(df['Gmt time'], tz='EST') to speficy that the datetime is in EST

That is not working. How do I fix it? I need the end 'datetime' to be in EST

2
  • What does "but it is not working" mean? You will not get this error if everything else is correct imo. Commented Jul 3, 2019 at 0:28
  • Updated my question: The DF is correct. But because of datetime having an offset value, the DatetimeIndex is not working. I need somehow to remove the offset value or make the DF consider the datetime is correct. Commented Jul 3, 2019 at 0:34

1 Answer 1

3

Try this one out using pytz, its saving your data as Pacific and converting to Eastern, you can use the same and convert from GMT to any time zone.

import pandas as pd
import pytz
data = ['2017-01-01 00:00:00']
datatf= pd.to_datetime(data)
my_timestamp = dt.datetime.now()
new_timezone = pytz.timezone("US/Pacific")
old_timezone = pytz.timezone("US/Eastern")
my_timestamp_in_new_timezone = old_timezone.localize(datatf[0]).astimezone(new_timezone)
print(my_timestamp_in_new_timezone )
df = pd.DataFrame()
df['Gmt time'] = [my_timestamp_in_new_timezone]
df['Gmt time'] = pd.DatetimeIndex(df['Gmt time'])
print(df)

Result :

               Gmt time
0 2016-12-31 21:00:00-08:00
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.