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.

The Singularity
  • 2,428
  • 3
  • 19
  • 48
  • 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. – FObersteiner Feb 06 '21 at 14:33

1 Answers1

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.

FObersteiner
  • 22,500
  • 8
  • 42
  • 72