'Remove "plus" sing from date

How can I convert timestamp with timezone into normal "datetime" type? When I print the df.dtypes of my df I get "object" as a type for this column not a "date time"

I have data frame:

| Resource | timestamp                       |
| -------- | --------------                  |
| 112      | 2011-10-01 00:38:44.546000+02:00|
| 113      | 2011-10-02 00:38:44.546000+02:00|
| 112      | 2011-10-03 00:38:44.546000+02:00|
| 115      | 2011-10-04 00:38:44.546000+02:00|
| 114      | 2011-10-05 00:38:44.546000+02:00|

I used

df_log['time:timestamp'] = df_log['time:timestamp'].apply(lambda x: dt.strftime(dt.fromisoformat(str(x)),'%Y-%m-%d.%H:%M:%S'))

It has removed the '+' sign and gave me the date and time part, but I feel I am doing it wrong because i don't have good knowledge from datetime processing! My goal is to just get date and time like "2011-10-01 00:38:44" and the type be "datetime" not object as I am still getting!



Solution 1:[1]

After the line of code you have. Try:

df_log['time:timestamp'] = datetime.strptime(df_log['time:timestamp'], '%Y-%m-%d.%H:%M:%S')

Solution 2:[2]

After spending some time, these lines solved my problem:

  • Apply the timezone to the column and convert timestamp to date and time only:

    df['timestamp'] = pd.to_datetime(df['timestamp'], utc=True).dt.tz_convert(None)
    
  • Remove the timezone without applying it to the column: (My favorite)

    df['timestamp'] = pd.to_datetime(df['timestamp']).apply(lambda t: t.replace(tzinfo=None))
    

    OR

    df['timestamp'] = pd.to_datetime(df['timestamp']).apply(lambda d: d.strftime('%Y-%m-%d %H:%M:%S'))
    

You should iterate through each row by using apply function with a lambda expression to use replace function because replace function work with pandas.tslib.Timestampobject not with pandas.core.series.Series. Otherwise, you will end up with this error: TypeError: replace() got an unexpected keyword argument 'tzinfo'

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Juan Camilo Rivera Palacio
Solution 2