1

1 I have a CSV containing a column data["timestamps1"] of GMT-timezone strings in this format:

2020-02-28T12:53:47.167Z

2 I want to get a new column with strings of the timezone in another tz-time which is Europe/Berlin, the format doesn't matter that much, e.g.

2020-02-28 13:53:47

How can i do that? I tried already parsing dates

import pandas as pd
from datetime import datetime
from pytz import timezone
path = "timestamps.csv"                           
data = pd.read_csv(path, sep=";", parse_dates= ["timestamps1"])

data['timestamps1_new'] = data['timestamps1'].dt.tz_localize('GMT').dt.tz_convert('Europe/Berlin')

Then i get the error "Already tz-aware, use tz_convert to convert." When I do not parse dates, i get "Can only use .dt accessor with datetimelike values". Even when I manipulate the string in this way, the error occurs:

data["timestamps1"] = data["timestamps1"].str[:-5]
data["timestamps1"] = data.timestamps1.replace("T"," ",regex=True)

Here are some example data:

data = {'timestamps': ['2020-11-28T13:14:57.463Z','2020-11-28T13:14:57.603Z','2020-11-28T13:14:57.618Z']}
data = pd.DataFrame(data=data)

Thanks a lot!

1
  • your format 2020-02-28T12:53:47.167Z (ISO8601) automatically gets parsed to aware datetime (tzinfo set, UTC). so you simply need to convert: data['timestamps1_new'] = data['timestamps1'].dt.tz_convert('Europe/Berlin') Commented Apr 20, 2021 at 14:11

1 Answer 1

1

pandas parses ISO 8601 format with Z denoting UTC directly to aware datetime (it "knows" it's in UTC...):

import pandas as pd

data = {'timestamps': ['2020-11-28T13:14:57.463Z','2020-11-28T13:14:57.603Z','2020-11-28T13:14:57.618Z']}
data = pd.DataFrame(data=data)

# you skip this step by setting 'parse_dates' in read_csv
data['timestamps'] = pd.to_datetime(data['timestamps'])

print(data['timestamps'])
0   2020-11-28 13:14:57.463000+00:00
1   2020-11-28 13:14:57.603000+00:00
2   2020-11-28 13:14:57.618000+00:00
Name: timestamps, dtype: datetime64[ns, UTC]

So you can convert directly:

data['timeGermany'] = data['timestamps'].dt.tz_convert('Europe/Berlin')

print(data['timeGermany'])
0   2020-11-28 14:14:57.463000+01:00
1   2020-11-28 14:14:57.603000+01:00
2   2020-11-28 14:14:57.618000+01:00
Name: timeGermany, dtype: datetime64[ns, Europe/Berlin]
Sign up to request clarification or add additional context in comments.

1 Comment

That's exactly it, I get it, thanks a lot!

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.