0

I have a pandas dataframe with the following data:

    PointId      Time                  Value
0  Brabrand1_1  2020-10-02 23:58:14+02  0.9754
1  Brabrand1_1  2020-02-23 21:03:00+01  2.1414
2  Brabrand1_1  2020-02-23 21:33:00+01  2.1406
3  Brabrand1_1  2019-01-29 18:03:32+01  1.0390
4  Brabrand1_1  2019-01-29 18:33:32+01  1.0390
5  Brabrand1_1  2019-01-29 19:03:32+01  1.0390
6  Brabrand1_1  2019-01-29 19:33:32+01  1.0399
7  Brabrand1_1  2019-01-29 20:03:32+02  1.0000

I want the Time data to be UTC, ignoring the +01, +02 ... whats a good way of removing the +01 or +02... is it best to convert to string and remove or is there a more appropirate way of converting to datetime and then converting to utc? Thanks

3 Answers3

1

You can change the time zone without changing the time with:

import dateutil.tz

new_datetime = old_datetime.replace(tzinfo=dateutil.tz.tzutc())

For your dataframe, you can use:

df['Time'] = df['Time'].apply(lambda old_datetime: old_datetime.replace(tzinfo=dateutil.tz.tzutc()))

If you are using python 3.9, you can use zoneinfo instead of dateutil.tz. You should avoid using pytz, although it wouldn't cause any problems in this situation.

https://blog.ganssle.io/articles/2018/03/pytz-fastest-footgun.html

egrubbs
  • 331
  • 1
  • 5
1

If you want your data to be in UTC:

pd.to_datetime(df['Time']).dt.tz_convert('UTC')

If you want to remove tzinfo:

df['Time'].dt.tz_localize(None)

So to drop tzinfo and convert to UTC:

pd.to_datetime(df['Time']).dt.tz_localize(None).dt.tz_convert('UTC')
0

Thanks for the help, I used what you people said and had to dig a bit more but got the answer by doing this:

df['Time'] = pandas.to_datetime(df['Time'], utc=True)
df.Time.dt.tz_convert('UTC')