0

Hi I am looking for a python command to set a timezone to a specific column.

Particularly my dataframe looks like below and I would like "to say" column "Date" is a date in Europe/Berlin timezone. I do not want to convert this time to Europe, this means 11:02:31 +2:00 is not correct. Do you have any idea to set this time to Europe/Berlin time?

   Index                Date  Stamp (s)     Value       Epoch
0      0 2016-07-06 11:02:31  0.1250000 0.0169273  1467802951
1      1 2016-07-06 11:02:32  1.1250000 0.0168724  1467802952
2      2 2016-07-06 11:02:33  2.1250000 0.0168620  1467802953
3      3 2016-07-06 11:02:34  3.1400000 0.0169068  1467802954
4      4 2016-07-06 11:02:35  4.1400000 0.0168702  1467802955

Greets

2 Answers 2

1

Use tz_localize.

  df = df.assign(Date=df['Date'].dt.tz_localize('Europe/Berlin'))

nb: I prefer to use assign to avoid trying to set data to a view.

Sign up to request clarification or add additional context in comments.

Comments

1
# core modules
from datetime import timezone, datetime

# 3rd party modules
import pandas as pd
import pytz

# create a dummy dataframe
df = pd.DataFrame({'date': [datetime(2018, 12, 30, 20 + i, 56)
                        for i in range(2)]},)
print(df)

# Convert the time to a timezone-aware datetime object
df['date'] = df['date'].dt.tz_localize(timezone.utc)
print(df)

# Convert the time from to another timezone
# The point in time does not change, only the associated timezone
my_timezone = pytz.timezone('Europe/Berlin')
df['date'] = df['date'].dt.tz_convert(my_timezone)
print(df)

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.