0

I have the following date time in Pandas read from a JSON file

datetime    
0   2021-02-04 09:15:00+05:30   
1   2021-02-04 09:16:00+05:30   
2   2021-02-04 09:17:00+05:30   
3   2021-02-04 09:18:00+05:30   
4   2021-02-04 09:19:00+05:30   

I tried using the following snippet to parse the date time but it made no difference.

data = pd.read_json ('dataset.json', convert_dates=['datetime'])

Here's my expected output

datetime    
0   2021-02-04 14:45:00 
1   2021-02-04 14:46:00 
2   2021-02-04 14:47:00 
3   2021-02-04 14:48:00 
4   2021-02-04 14:49:00 

Please Advise.

1
  • note that 2021-02-04 09:15:00+05:30 means 9:15 local time, 5:30 hours behind UTC. converting that to 14:45 means you want to add the UTC offset again - which honestly doesn't make much sense to me. Commented Feb 6, 2021 at 14:33

1 Answer 1

1

you can add the UTC offset as a timedelta to the existing datetime, localized to None:

df['localtime'] = pd.Timedelta(minutes=df['datetime'].dt.tz._minutes) + df['datetime'].dt.tz_localize(None)

df 
                   datetime           localtime
0 2021-02-04 09:15:00+05:30 2021-02-04 14:45:00
1 2021-02-04 09:16:00+05:30 2021-02-04 14:46:00
2 2021-02-04 09:17:00+05:30 2021-02-04 14:47:00
3 2021-02-04 09:18:00+05:30 2021-02-04 14:48:00
4 2021-02-04 09:19:00+05:30 2021-02-04 14:49:00

see also Convert pandas timezone-aware DateTimeIndex to naive timestamp, but in certain timezone.

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

2 Comments

There seems to be an issue with the tz._minutes
@Luke ...and that would be? ^^

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.