user12625679
user12625679

Reputation: 696

Remove +00:00 (UTC offset) from timestamp in Python/pandas

I would like to remove the +00:00 from the timestamp below. I used the code below to remove the T and Z from the timestamp but the dtype is still datetime64[ns, UTC] and ideally I would like to convert it to datetime64[ns]

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

Timestamp_column before transformation:

2020-07-10T14:12:39.000Z    

Output:

2020-07-10 14:12:39+00:00   

Upvotes: 2

Views: 8910

Answers (2)

FObersteiner
FObersteiner

Reputation: 25544

you could tz_convert to None:

import pandas as pd

df = pd.DataFrame({'Timestamp_column': ['2020-07-10T14:12:39.000Z']})

df['Timestamp_column'] = pd.to_datetime(df['Timestamp_column']).dt.tz_convert(None)

# df['Timestamp_column']
# 0   2020-07-10 14:12:39
# Name: Timestamp_column, dtype: datetime64[ns]

Upvotes: 6

bigbounty
bigbounty

Reputation: 17358

In [50]: df
Out[50]:
                       date
0  2020-07-10T14:12:39.000Z

In [51]: df.dtypes
Out[51]:
date    object
dtype: object

In [52]: df["new_date"] = pd.to_datetime(df["date"], format="%Y-%m-%dT%H:%M:%S.%fZ")

In [53]: df
Out[53]:
                       date            new_date
0  2020-07-10T14:12:39.000Z 2020-07-10 14:12:39

In [54]: df.dtypes
Out[54]:
date                object
new_date    datetime64[ns]
dtype: object

Upvotes: 0

Related Questions