0

I have a datetime column EST

EST
2017-01-01 19:00:00-05:00
2017-01-01 19:01:00-05:00
2017-01-01 19:02:00-05:00
2017-01-01 19:03:00-05:00
2017-01-01 19:05:00-05:00
2017-01-01 19:06:00-05:00
....
2017-12-31 23:01:00-05:00

Here you can see that there is trailing -05:00 on every time. This trailing -05:00 was added when I converted from GMT to EST.

How do I remove -05:00?

The reason is because when I do:

df['EST'] = pd.DatetimeIndex(df['EST']')

I get this error:

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

Could you please help me solve this?

2 Answers 2

2

We can remove the -05:00 by using Series.dt.tz_localize and set it to None. Then we can use pd.to_datetime:

df['EST'] = pd.to_datetime(df['EST']).dt.tz_localize(None)
                    EST
0   2017-01-01 19:00:00
1   2017-01-01 19:01:00
2   2017-01-01 19:02:00
3   2017-01-01 19:03:00
4   2017-01-01 19:05:00
5   2017-01-01 19:06:00
6   2017-12-31 23:01:00

If EST is your index, use:

df.index = pd.DatetimeIndex(df['EST']).dt.tz_localize(None)
Sign up to request clarification or add additional context in comments.

10 Comments

This works but is this optimal solution in such cases?
what is the difference between pd.DatetimeIndex(df['EST']') and pd.to_datetime(df['EST'])? since I have been using pd.DatetimeIndex
I assume that 'EST' is a column in this case and not your index. If it is your index, you are correct we have to use pd.DatetimeIndex, see my edit. @floss
I used pd.to_datetime(df['EST']).dt.tz_localize(None) and I got this error: ValueError: Array must be all same time zone ( and ) ValueError: Tz-aware datetime.datetime cannot be converted to datetime64 unless utc=True
I can confirm that for the example dataset your provided. If that generalizes to your whole dataset, then it works correctly. @floss
|
1

I think you may use .dt.tz_localize with option None assume your EST column is already datetime dtype

Sample data:

df['EST']:
0   2017-01-01 19:00:00-05:00
1   2017-01-01 19:01:00-05:00
2   2017-01-01 19:02:00-05:00
3   2017-01-01 19:03:00-05:00
4   2017-01-01 19:05:00-05:00
5   2017-01-01 19:06:00-05:00
Name: EST, dtype: datetime64[ns, US/Eastern]

Output with .dt.tz_localize

df['EST'].dt.tz_localize(None)

Out[2386]:
0   2017-01-01 19:00:00
1   2017-01-01 19:01:00
2   2017-01-01 19:02:00
3   2017-01-01 19:03:00
4   2017-01-01 19:05:00
5   2017-01-01 19:06:00
Name: EST, dtype: datetime64[ns]

3 Comments

When I do df['EST'].dt.tz_localize(None), I get this error AttributeError: Can only use .dt accessor with datetimelike values
@floss: it means your df['EST'] is not datetime dtype. You need convert it to datetime using pd.to_datetime or construct direct datetimeindex using pd.DatetimeIndex and specify tz info. However, the final output will reflect the -05:00 into the time. It means it will be 00:00:xx and date increases 1 day
my df['EST'] is in the proper format but when I converted from GMT to EST, I got the trailing offset value -05:00.

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.