pythonpandasdatetimetimezone

How can I convert from string, apply the timezone offset, make naive and convert back to string a Pandas Series Index in Python?


I have a Pandas time-series object with an Index like this:

Index(['2023-05-31T00:05:00+0300', '2023-05-31T00:06:00+0300',
       ...       
       '2023-09-15T13:48:00+0300', '2023-09-15T13:49:00+0300'],
      dtype='object', length=76106)

and I need to convert into this:

Index(['2023-05-30T21:05:00', '2023-05-30T21:06:00',
       ...       
       '2023-09-15T10:48:00', '2023-09-15T10:49:00'],
      dtype='object', length=76106)

What I am trying to do, effectively, is convert the string to datetime, subtract the timezone offset, make it naive and convert it back to string.

I know how to do this in a lot of (somewhat complicated) steps which will involve converting the series to dictionary, converting the (datetime) keys one by one, making the dictionary a new series, but is there a way to do this in few steps, probably in-place?

Any help will be greatly appreciated.


Solution

  • You can convert the index to datetimes with pd.to_datetime, converting the timezone to UTC by specifying utc=True, and then format the result using strftime:

    idx = pd.Index(['2023-05-31T00:05:00+0300', '2023-05-31T00:06:00+0300','2023-09-15T13:48:00+0300', '2023-09-15T13:49:00+0300'])
    
    idx = pd.to_datetime(idx, utc=True).strftime('%Y-%m-%dT%H:%M:%S')
    

    Output:

    Index(['2023-05-30T21:05:00', '2023-05-30T21:06:00', '2023-09-15T10:48:00',
           '2023-09-15T10:49:00'],
          dtype='object')